Skip to content

Instantly share code, notes, and snippets.

@adarsh1021
Last active August 29, 2018 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adarsh1021/b3e482e09ece67ad538e7d5f1c08ac53 to your computer and use it in GitHub Desktop.
Save adarsh1021/b3e482e09ece67ad538e7d5f1c08ac53 to your computer and use it in GitHub Desktop.

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!

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?".
    Welcome Intent

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
    training phrases
  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. parameter

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:
'use strict';
// Import the Dialogflow module from the Actions on Google client library.
const {dialogflow} = require('actions-on-google');
// Import the firebase-functions package for deployment.
const functions = require('firebase-functions');

// Instantiate the Dialogflow client.
const app = dialogflow({debug: true});

// Handle the Dialogflow intent named 'favorite day'.
// The intent collects a parameter named 'date'.
app.intent('favorite day', (conv, {date}) => {
    // Write your own logic to generate the random number here
    // The parameter date is actually the date of the next coming day specified by the user
    // If you are interested to see it, replace the luckyNumber variable with date variable in the conv.close() method
    const d = new Date(date);
    const luckyNumber = Math.floor(Math.random()*d.getDate())+1;
    // Respond with the user's lucky number and end the conversation.
    conv.close('Your lucky number is ' + luckyNumber);
});

// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

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.
    click on simulate

  2. Type "Talk to my text app" and hit enter.
    simulator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment