Creating Conversations for Google Assistant

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 !

Invocation

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.

  1. In the intents page of the Dialogflow console, click on Default Welcome Intent.
  2. Under the Responses section, delete all the default responses, by clicking the trash icon next to it.
  3. Now click on Enter a text responsee and type "Welcome! What is your favorite day of the week?".
Image for post
Image for post

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

  1. In the Dialogflow Console, in the left navigation, click on Intents and then click on Create Intent.
  2. Name your intent “favorite day”, in the name field at the top.
  3. 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
  • Wednesday
Image for post
Image for post
  1. 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 date and we will use its value extracted from the user, to determine the lucky number and give back a response.
  2. Check the Required box of the date parameter. A new column called Prompts will show up. Click on Define prompts and 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 date parameter. So by any chance, if Dialogflow was unable to extract the date parameter from what the user said, it will explicitly ask the user for a value for the parameter.
Image for post
Image for post

Fulfillment

  1. Now skip the Responses section, since we will be calculating the response based on the date parameter. 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 Save to save this intent.
  2. In the left navigation, click on Fulfillment. Enable the Inline Editor. We will write the logic for our function here.
  3. Replace the code in index.js with the following:

The code is implemented in JavaScript. The webhook uses the Actions on Google Node.js client library to respond to HTTP requests that the Assistant sends to your webhook. The library allows you to create a 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 :

  • A Dialogflow Conversation object - 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 Parameters object - This is a JavaScript Object representation of the parameter values collected in the related intent. Here the parameter object was date.

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.

Click on Deploy button. This wil deploy your code to the cloud.

Testing your Actions

  1. Open the Actions Console and select your project. Click on Simulator in the left navigation.
Image for post
Image for post

2. Type “Talk to my text app” and hit enter.

Image for post
Image for post

You should now be in the converstion you just created! Enter your favorite day and your Action will generate your lucky number!

Image for post
Image for post

Originally published at https://github.com/chasinginfinity/actionsongoogle.

Watch the video version at https://youtu.be/n6ApN7NIRCk

Written by

I make websites and teach machines to predict stuff. I also make YouTube videos — https://www.youtube.com/adarshmenon

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store