Understanding and building Dialogflow agents to handle custom intents

The  reason why we need Dialogflow is to build a conversational experience do some arbitrary actions like calling an external API and fetching the result. Google assistant is an artificially intelligent virtual assistant and so it is responsible for understanding natural language.
First, google assistant matches an agent according to the information it receives from a user(action intent). For example, if someone says "fix an appointment for eye checkup" matches to some agent then that agent will be invoked in the following conversation.
The following figure shows a very clear picture of what happens under the hood when we try to invoke an action.

After being associated with the action the respective agent will be responsible to provide a conversational experience to the user.
Having understood the basic process behind google assistant and dialogflow integrations, its time to turn the attention towards building an action.
First, create a new agent for your project.

After that we will be redirected to the following page which contains two default intents and option to create new custom intents.

Every intent has these sections to customise the user experience based on the type of project to be built.

Understanding each part is essential for us to build a clean interface for the user.
  1. Contexts: This part of an intent lets our agent to know the context of the intent. In other words, this can be used to share information between various intents. For example, the agent might want to know in what context the user is speaking. A user asks the agent about temperature in New Delhi. "What is the temperature in Delhi?". Our agent replies with the required information. After that user asks temperature in Mumbai : "What about Mumbai?". The latter question does not mean anything when left alone but after the former, the agent knows the context i.e., temperature in this case. Have a look at the docs:  https://cloud.google.com/dialogflow/docs/contexts-overview 
  2. Events: Alternate way to trigger the intent instead of matched keywords or phrases. Consider for example an API call can trigger a custom event which in turn trigger one or more intents. Have a look at the docs : https://cloud.google.com/dialogflow/docs/events-overview
  3. Training Phrases: These are the phrases which are provided so as to be matched with user inputs to trigger the intent. Its good if we write more of such phrases because the AI mechanism gets a better grasp of the inputs that way and user can trigger the intent easily. Have a look at the docs : https://cloud.google.com/dialogflow/docs/intents-training-phrases
  4. Action and parameters: These are the variables that we extract from users input and store for a possible later use. These parameters can be used to make a custom API request or sending a custom message to the user. Have a look at the docs:   https://cloud.google.com/dialogflow/docs/intents-actions-parameters
  5. Responses: The result which the user gets as a result of the current intent being matched(or triggered by an event). This maybe a text, spoken, or a media rich response which the user receives. A limitation of this is that this can be used only to generate static responses in most cases.However, you can use parameters captured in these responses as well to make them somewhat dynamic. Have a look at the docs: https://cloud.google.com/dialogflow/docs/intents-responses
  6.  Fulfillment: The limitation in responses is dealt here. You can execute custom code, send dynamic responses, and make API calls for the intent being matched. The image below gives a very good idea. Have a look at the docs: https://cloud.google.com/dialogflow/docs/fulfillment-overview
Let us build a simple action in which the agent says a joke to the user.* For this we will make an API get request.

First, we create a new intent and name the intent as joke.
After that, we add some training phrases as below.
Now, we skip to Fulfillment and turn on "Enable webhook call for this intent".
Save that and move to Fulfillment.
Switch to package.json in the editor and axios(to make API request).

Add the following function code to index.js.
Write "intentMap.set('joke', jokehandler);" after intentMap declaration and include axios in index.js.
That's it and your action is ready.
Please comment if you liked the post.

*Note: You will need to configure a billing account to build google actions.

Comments

Popular posts from this blog

Create and deploy a Discord Bot

Three ways to deploy Node.Js applications quickly