In the last tutorial, we understood why voice is the future, and I showed you how to create your first Action. Today we will see how you can create or design a conversation in Dialogflow. We will create intents that can recognize the user utterances, define custom entities and implement a fulfillment, which can respond to the user. To demonstrate all this we will build an app that tells your lucky number based on your favorite day of the week. (Yes, its silly, but this will help explain the concepts easily)
So lets start!
PS: This is the written version of this YouTube video. Watch it if you prefer that !
Before we begin building the app, lets understand what an invocation is. Invocations are used to invoke or select the action you want to talk to. There are two types of invocations:
Explicit Invocation This is when the user explicitly tells the Assistant they want to talk to a particular action. For example, “Hey Google, talk to Random Words”.
Implicit Invocation This is when the Assistant chooses to invoke your action, without the user specifying it by name. For example, “Hey Google, tell me a random word”, invokes the action Random Words.
After you have created a new Action, and you are in the Dialogflow console, click on the
Create button to create a new agent. This Dialogflow agent will be processing the user's utterances and giving responses based on our program.
The Welcome Intent
The Welcome Intent is the first intent that gets invoked when a user wants to talk to your Action. In this section, we will modify the default welcome intent, so that it greets the user and asks for his/her favorite day of the week.
- In the intents page of the Dialogflow console, click on
Default Welcome Intent.
- Under the Responses section, delete all the default responses, by clicking the trash icon next to it.
- Now click on
Enter a text responseeand type "Welcome! What is your favorite day of the week?".
The role of the Welcome Intent is over here. Now the user will respond to this with his/her favorite day of the week. We will create another intent to listen to this and generate a response.
Create your own Intent
- In the Dialogflow Console, in the left navigation, click on
Intentsand then click on
- Name your intent “favorite day”, in the name field at the top.
- Under the Training phrases, add a few examples of how the user might express this intent. The examples you provide are used to train a machine learning model which allows the agent to match future inputs to the appropriate intent. Here we will add the following examples:
- My favorite day is Saturday
- I like Friday
- I love Monday
- Sunday is my favorite
- Now Dialogflow should automatically pickup the day name in the training phrase, and highlight it. Under Actions and parameters you should also see a new parameter is added called
date. Parameters represents values that you want to extract from the user's phrases. It is basically like a variable, that dialogflow automatically extracts from the training phrase. Here the parameter is
dateand we will use its value extracted from the user, to determine the lucky number and give back a response.
- Check the Required box of the
dateparameter. A new column called Prompts will show up. Click on
Define promptsand enter "What is your favorite day?" as a prompt. You can also add multiple variants of the same question in the prompts. We define this prompt because we cannot move any further without having the
dateparameter. So by any chance, if Dialogflow was unable to extract the
dateparameter from what the user said, it will explicitly ask the user for a value for the parameter.
- Now skip the Responses section, since we will be calculating the response based on the
dateparameter. Under the Fulfillment section, turn on Enable webhook call for this intent. This allows the response to the user to be dynamically generated. This is done by passing the parameter value to a function, where we write the logic to construct a response for the user. This function is run in the cloud. Click on
Saveto save this intent.
- In the left navigation, click on
Enablethe Inline Editor. We will write the logic for our function here.
- Replace the code in
index.jswith the following:
DialogflowApp object, which acts as a wrapper for the Dialogflow API.
In this case, an
app variable of the
DialogflowApp object type is created. The
app.intent() function is used to declare a callback to handle the 'favorite day' intent.
The callback receives two important arguments :
Dialogflow Conversationobject - This is a client library abstraction of the state of the dialog, and includes properties which represent values of the incoming request to our webhook, such as the current active dialogflow contexts, the surface capabilities of the user device, etc..
- A Dialogflow
Then the lucky number can be generated with any logic you like. Here I have used the product of a random number and the date of the day specified by the user. Then we use the
close method of the Conversation object parameter(
conv) to respond to the user and end the conversation.
Deploy button. This wil deploy your code to the cloud.
Testing your Actions
- Open the Actions Console and select your project. Click on
Simulatorin the left navigation.
2. Type “Talk to my text app” and hit enter.
You should now be in the converstion you just created! Enter your favorite day and your Action will generate your lucky number!