How to Develop a Google Assistant Action using Node.js

Botanalytics
7 min readJul 18, 2018

--

In this tutorial, we will implement a custom action for Google Assistant by using nodejs, npm, Dialogflow, Google Actions and Google Firebase. This skill is basically a Favourite Music Genre example. With this tutorial you will be able to create a custom action for Google Assistant, implement functionality by using nodejs and try your custom action from Google Assistant Action Simulator.

This tutorial contains materials from different resources that can be seen on Resources section.

Prerequisites

  • A web browser, such as Chrome.
  • A suitable Node.js development environment. For this tutorial Node.js version should be greater than or equal 8.11.3 and NPM version should be greater than or equal 6.0.0.
  • An Actions on Google account. This is required for creating the custom action project for Google Assistant.
  • A Dialogflow account.
  • A Firebase account.

Checking Google Permission Settings

  • Go to the Activity Controls page.
  • Sign in with your Google account, if you have not already done so.
  • Ensure that the following permissions are enabled:
  1. Web & App Activity
  2. Device Information
  3. Voice & Audio Activity

Installing the Firebase Command-line Interface

To install or upgrade the CLI run the following npm command:

npm -g install firebase-tools

To verify that the CLI has been installed correctly, open a terminal and run:

firebase --version

Authorize the Firebase CLI by running:

firebase login

Write your crendentials for firebase in order to develop and deploy your firebase functions.

Creating an Actions Project

  • Type in a project name, like actions-tutorial. This name is for your own internal reference; later on, you can set an external name for your project.
  • Click Create Project.
  • Rather than pick a category, click Skip on the upper-right corner.
  • Click Build > Actions in the left navigation menu.
  • Click Add your first Action.
  • On the Custom intent card, click Build. This will open the Dialogflow Console in another tab.

Creating a Dialogflow Agent

  • After following the steps above, you should already be in the Dialogflow Console with your Actions project name at the top. You may need to authorize Dialogflow to use your Google account, and accept the Terms of Service.
  • Click Create.

Starting a Conversation

Users start the conversation with your Actions through invocation. (An example of an explicit invocation is a phrase like “Hey Google, talk to HelloWorldApp”.) The Assistant then tries to match the user’s input to one of your Actions.

Creating a Welcome Intent

By default, Dialogflow creates a welcome intent. You will modify the welcome intent that users trigger when they say “Hey Google, talk to my test app”.

  • Delete all of the other text responses by clicking the trash icon next to each one.
  • Under the Responses section, click Enter a text response and type “Welcome! What is your favorite music genre?”
  • Click Save. You should see a “Intent saved” notification pop up.

Testing the Welcome Intent

The Actions Console provides a web interface for testing called the simulator. This interface lets you simulate hardware devices and their settings.

  • In the Dialogflow Console left navigation, click on Integrations. Then, click on Google Assistant > Integration Settings.
  • Click Test to update your Actions project and load it into the Actions Console simulator, you can leave the ‘Auto-preview changes’ option is enabled.
  • To test your Action in the simulator, type “Talk to my test app” into the Input field and hit enter.
  • Notice that when your Action’s welcome intent is invoked, the Assistant responds with the greeting message and prompts the user to provide their favorite color.

Adding a Custom Entity

  • Click Create entity and call it “musicGenre”.
  • Add the following entries, then click Save:
  1. rock’n roll
  2. punk
  3. rap
  4. hip hop
  5. alternative
  6. jazz
  7. blues
  8. country
  9. reggae
  10. latin
  11. indie

Creating a Dialogflow Intent

  • In the Dialogflow Console, close the Google Assistant integration settings page if you have it opened. In the left navigation, click on Intents then click on Create Intent.
  • In the intent name field at the top, type in “favorite music genre” as the name of your new intent. Click Save.
  • Under Training phrases, add the following user expressions by typing in the phrases below then clicking Enter on the keyboard:
  1. rock’n roll is my favorite
  2. punk is my favorite genre
  3. i love rap
  4. alternative
  5. my favorite genre is jazz
  6. i simply love blues
  7. i’m crazy about country
  8. i’m in love with latin
  9. i am a reggae person
  • Under Action and parameters, click Manage Parameters And Action.
  • Next, create the prompt text to ask the user for their favorite music genre. You should see a line with “musicGenre” in the Parameter Name field. This parameter entry is automatically created by Dialogflow because it is able to auto-annotate words and phrases it recognizes as entities in the training phrases. On that line:
  • Check the Required box.
  • Click Define prompts…
  • Enter “What’s your favorite music genre?” as a prompt.
  • Click Close.
  • Under the Fulfillment section in this page (not the left navigation), click Enable Fulfillment. You may need to expand the section by clicking on the down pointing arrow.
  • Turn on Enable webhook call for this intent.
  • Click Save.

Building Your Webhook Code

  • Create a folder on your local computer, you can name it anyway you like.
  • Go to that folder.
  • First configure the directory with firebase:

firebase init

  • Select only Functions.
  • Choose our test project for default Firebase project for this directory.
  • Choose javascript.
  • For this tutorial, we do not need for linter so choose n for eslint question.
  • Install dependencies by saying yes for firebase dependency install.
  • After everything, you should see Firebase initialization completed! on terminal!
  • Go to functions directory and install actions-on-google npm package using below command:

npm install --save actions-on-google

  • Open index.js file on that directory and write these:
'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});
app.intent('favorite music genre', (conv, {musicGenre}) => {
const luckyNumber = musicGenre.length;
// 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);
  • Return to parent folder which contains functions directory and firebase.json.
  • Run the following command in the terminal to deploy your webhook to Firebase.

firebase deploy

  • After a few minutes, you should see “Deploy complete!” indicating that you’ve successfully deployed your webhook to Firebase.

Retrieving the Deployment URL

  • Open the Firebase Console.
  • Select your Actions project from the list of options.
  • Navigate to Develop > Functions on the left navigation bar.
  • Under the Dashboard tab, you should see an entry for “dialogflowFirebaseFulfillment” with a URL under Event. Copy this URL.

Setting the URL in Dialogflow

  • Open the Dialogflow Console.
  • Navigate to Fulfillment on the left navigation.
  • Enable Webhook.
  • Paste the URL you copied from the Firebase dashboard if it doesn’t already appear.
  • Click Save.

Verifying Project Setup

  • In the Dialogflow Console left navigation, click on Integrations > Google Assistant.
  • Make sure Auto-preview changes is enabled and click Test to update your Actions project.
  • The Actions Console simulator loads your Actions project. To test your Action, type “Talk to my test app” into the Input field and hit enter.
  • You should see a response: “Welcome! What is your favorite music genre?”
  • Type “jazz”.
  • You should see a response: “Your lucky number is 4.”

You need to track your bot’s performance on a regular basis, keep an eye on how it is performing to make it hit the charts.

Finally, connect your skill to Botanalytics for getting your free analytics for Google Assistant Actions. You cannot improve what you don’t measure, right? You can use our official Node.js library to easily integrate Botanalytics.

References

Clap to recommend this story to others!

--

--

Botanalytics
Botanalytics

Written by Botanalytics

Botanalytics is the state-of-the-art analytics platform for conversational AI. https://botanalytics.co

No responses yet