Akshay Nandwana
Jul 10 · 8 min read

Welcome to the third article of the series. Hope you had great fun creating trivia game and win cool Google Swags. If not, please give a read to the previous article and Win the Prizes.

In this article, we are going to create an action called Count Characters. This action is a conversational type where the assistant will ask you to provide a word and then it will count the characters in it.

This will give us a brief understanding of how assistant asks for user input, how to play with user input data, what is Dialogflow, phrases, slot-filling and system entities. We are also going to write some code in javascript.

Let’s begin with the terminologies

Action: The phrase we use to start a specific program which is built by developers to perform certain thing is called the action. It is an interaction of Google Assistant. Action is a combination of specific Intent and a fulfilment.

For example — “Hey Google, talk to Google IO 19”

Intent: Something which the user wants to do.

For example — find a recipe, book a table, purchase a movie ticket

Fulfilment: It is something which handles the intent and its corresponding Action.

For example — App, service, conversation and any logic which we build

Actions Project: Project we create in actions console.

Actions Console: Platform or a tool where we create/develop actions.

Dialogflow: It is a web tool for developers to create conversations and other Google assistant experiences. It uses Machine Learning to understand what users are saying.

NLU: Natural Language Understanding, how software can understand user input.

Parameters: In Dialogflow, Any important word, phrase, or value said by the user.

Dialogflow Intent: What the user wants.

For example — book a cab, here the Dialogflow understand what the user wants.

Dialogflow webhook (fulfilment): Webhook is an HTTP callback. Here, we implement webhook as fulfilment. It contains logic to handle the intent.

Training phrase (Dialogflow): We as developers input some phrases about how a user can trigger our intent.

For example — “I need a taxi, I want to go somewhere by road, Please book a taxi for me”

Entities (Dialogflow): This will tell Dialogflow how to parse parameter from user queries.

Good Work!! 👍 Now, you had a better understanding of most of the terminologies, Let’s see some behind the scenes.

The action runs over the internet. Every logic building or any processing happens in the cloud. User input to an assistant, assistant send an HTTP request to specific Action, Action responds with an HTTP to the assistant and then, at last, the assistant with the use of fulfilment give a proper response to the user.

Let’s start the development

Go to Actions Console, Create a new project, give it a name “Count Characters”, Click on Create Project.

Select the Conversation Template. We had selected the Trivia one in our previous article.

Now, Select the Develop tab on top and choose Action from the sidebar. Click on “Add your first Action” and click on build. It will take you to the Dialogflow console.

Google provides us with Dialogflow, which helps us in understanding user input, extracting the important words from it, and how to return the responses. Everything is done under a Dialogflow agent.

Source: https://dialogflow.com/docs/agents

Agent check which intent to go for. An agent is the NLU modules.

Create an Agent. We will see Count-Characters and a create button on the right side. Click on create and our agent will be created.

And, we will be redirected to the intent section on the same page. Now, we have to create intent. On the screen, we can see two things under intent.

  1. Default fallback intent
  2. Default welcome intent

We will create a welcome intent here, Click on Default Welcome Intent.

Here, we will many many options which will be needed to create an intent.

As for now, we will go to the Response section and delete all pre-loaded texts and create our own welcome message “Welcome to Count Characters. Please input a word whose length you want to know” and click on Save.

It’s time to try wheater assistant is responding with our own welcome message or not.

Click on Integrations in the left sidebar, click on Google Assistant and click on test.

This will open a new tab in your browser. This is our testing area. Click on Talk to my test app.


Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com


Good Work!! 👍

Now to take the input from the user, we need to create a new intent. Click on “Create Intent” and then add the name. “input word”.

Now, we need to add some training phrases. I am taking an example of all the music artist.

Here, we see that all the music artist name gets highlighted. And, if we see in the action and parameter section. music-artist get’s created with the entity name of “@sys.music-artist”. This entity is a pre-defined entity which we are using here.

Now, check the Required checkbox and enter a prompt which we want our users to ask. Click on Save.

Now, we need webhook. Go to the fulfilment section on the bottom of this page and enable the webhook and click on save.

Till now, our assistant can take the input from the user and extract any music artist name and sends to the fulfilment for further.

Now, we will take the response from the fulfilment and add our logic of counting characters.

Go to Fulfilment on the left side in the sidebar and enable the Inline Editor and Click on Save.

If you get an error in Enabling Inline Editor saying something about Google Cloud Resources like below

Source: Google Codelab

Simply, go to your firebase console, open your project, go to the storage section from the left sidebar and click on “Get Started”. Now, come back to your Dialogflow console and refresh the page and enable the inline editor.

Now, we are going to write some code in Javascript in Dialogflow’s Inline Editor. This code will run on the cloud using Firebase Cloud Functions.

The webhook code using the Actions on Google Node.js client library to respond to HTTP requests. Assistant sends the request to the webhook. Using this library, we can create a Dialogflow object which helps to use the Dialogflow API.

// Firstly, we will import the Dialogflow module from the Actions on Google client Node.js library.const {dialogflow} = require('actions-on-google');

Deploy the Firebase Function

// Then, we will import the firebase-functions package for deployment.const functions = require('firebase-functions');

Create app Instance

// Create an app instance
const app = dialogflow();
// To log the raw JSON payload from the user request or response
const app = dialogflow({
debug: true
});

In the lifecycle of the user interaction with the assistant, we are using the client library to handle all our request and responses. We can create functions which act as the handler is of three types:

  1. Intent Events
  2. Error Events — use app instance’s catch function
  3. Fallback Events — use app instance’s fallback function

Set Handler for Intents

We will call the intent() method. This method required two arguments, one is the name of the intent you had created in Dailogflow Console and second is the handler function.

Suppose, if we have multiple intents, we don’t require to set the handler for every intent. Dialogflow’s built-in response handler will automatically handle intents without implementing our own handler functions.

For Example — Here, we called an intent method using app instance. “conv” argument used to call ask method which will send the inner string as a response to the user.

app.intent('Default Welcome Intent', (conv) => {
conv.ask('How are you?');
});

We learnt about the parameters in terminologies. When the user sends any request to the Dialogflow, Parameters get extracted and it can be accessed using params variable.

For Example —

app.intent('Default Welcome Intent', (conv, params) => {
conv.ask(`How are you, ${params.name}?`);
});

There is a concept in Javascript called “Destructing”. This means that it will give the properties of an object or an array to the variable. So, using this destructive power we can re-write our above code.

app.intent('Default Welcome Intent', (conv, {name}) => {
conv.ask(`How are you, ${name}?`);
});
Source: https://developers.google.com/actions/reference/nodejsv2/overview

Now, get back to our Count Characters. We need to get the music artist name from the data provided by the user and count its length and send it back to the user.

app.intent('input word', (conv, {music_artist}) => {    const music_artist_name_length = music_artist.length;    conv.close(`Number of Characters in ${music_artist} is ` + music_artist_name_length);});

“music_artist” is the parameter name we had given in the Action and Parameter section.

The close method is the closing message your action will tell the user and then it will end the conversation.

Now, we need to make our app object publicly accessible webhook. This will help to handle our HTTP post request.

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

Done all your code and click on Deploy. It will take a couple of seconds to get the deploy process done. Time to test your assistant.

Congratulations 🥇You had created a conversation action for Google Assistant. Now, as you had a better understanding, you can create more actions like this.

In the next article, we are going to take things on our local machine, will use the command line, create some entities, will play with sounds and also get to know more about our user.

Don’t forget to create and publish your action worldwide in order to Win a Google Home and an exclusive Google T-Shirt before 31st July.

If you find this reading useful, please click the 👏 button and share to help others find it! Feel free to leave a comment 💬 below.
Have feedback? Let’s be friends on
Instagram.

Voice Tech Podcast

Voice technology interviews & articles. Learn from the experts.

Akshay Nandwana

Written by

Former Android Engineer at GetMeAnApp, MindOrks | Less Human, More Being | Local Guide | Storyteller. http://www.akshayn.app

Voice Tech Podcast

Voice technology interviews & articles. Learn from the experts.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade