Search results for

All search results
Best daily deals

Affiliate links on Android Authority may earn us a commission. Learn more.

Build your own Action for Google Assistant

Google has an entire platform dedicated to helping you extend the functionality of the Google Assistant. Here's how to build your own Google Assistant Actions!
By

Published onJune 22, 2018

Google Home Mini

If you’ve ever chatted with a Google Assistant speaker, you might know how frustrating it is to be told “I’m sorry, I don’t know how to help with that yet.” Luckily you don’t have to wait for someone to implement a missing feature — you can do it yourself! Google has an entire platform dedicated to helping you extend the functionality of Google Assistant by defining custom Actions.

Related: Google Assistant routines

Before you get going, you’ll want to check out the Google Assistant Actions directory, since there’s a reasonable chance that someone’s already addressed your needs. You might still have a better or different implementation that makes sense.

Let’s look at how to build a complete Google Assistant Action. By the end of this article, you’ll have created an Action that asks the user various questions, parses their responses, and then extracts specific pieces of information, which it then uses to personalize the conversation and drive it forward.

What we’re going to build

We’ll be building a bad joke generator action that learns the user’s name, and finds out whether they want to hear a cheesy joke about dogs or cats.

When designing an Action, it’s a good idea to map out all the different routes the conversation can take, so here’s what we’ll be building:

Creating an Actions project and a Dialogflow agent

Every single Action requires the following:

  • An Actions project- This is where you’ll manage, test and publish your Action, and perform admin tasks like guiding your Action through the publication process.
  • A Dialogflow agent- This is a web-based service you’ll use to integrate your Action with the Google Assistant. You’ll use this agent to define how users interact with your Action, the parameters Dialogflow should extract, and how the conversation should progress.

To create these components:

  • Head over to the Actions on Google Developer Console and log in with your Google account. If this is your first visit, then follow the instructions to create an Actions on Google account.
  • When prompted, select Add/import project.
  • Give the project a name; I’m using “BadJokeGenerator.”
  • Click Create Project.
  • Rather than pick a category, select Skip.
  • In the Console’s left-hand menu, select Actions.
  • Select Add your first action.
  • Choose the language(s) in which your Actions directory listing should be displayed. Click Update.
  • Select Custom intent, followed by Build.
  • The Dialogflow Console will launch in a new tab. Review this information, and if you’re happy to proceed, then create your Dialogflow agent by clicking Create.

Welcome the user to your Action

Every conversation has to start somewhere! Whenever you create a Dialogflow agent, a Welcome intent is generated automatically, which represents the entry point into your Action.

You define how your Action responds to user input via Dialogflow intents. It can respond in two ways:

  • Static. If your Action always responds in the same way, you can provide a canned response as plain text.
  • Dynamic. You can use a webhook, also known as a fulfilment, to figure out the relevant response and send it back to the Google Assistant, and ultimately to the user.

Your responses should guide the user on what to say next, so I’m going to welcome the user to our application, and then ask for their name. Since this is a static response, we can supply it as plain text:

  • Select Intents from the Console’s left-hand menu.
  • Position your cursor over the Default Welcome intent text, and give it a click. This launches Dialogflow’s intent editor.
  • Scroll to the editor’s Response section.
  • Delete all the pre-populated stock responses, by hovering over each response and then clicking the Trash icon.
  • Click the Add responses button, and select Text response.
  • Enter this message: “Hi, welcome to Bad Joke Generator. What’s your name?”
  • Click Save.

Language training: Define your conversation’s grammar

Next, we need to make sure our Dialogflow agent can identify which part of the user’s response is the required name parameter. This means providing examples of all the different ways that someone might provide their name.

When it comes to understanding and processing language, Dialogflow’s natural learning understanding (NLU) engine does a lot of the heavy lifting, so you don’t have to list every potential response. However, the more training phrases you provide, the greater your chances of a successful match, so try to be as thorough as possible.

To train your agent:

  • In the Console’s left-hand menu, select the little + that appears alongside Intents.
  • At the very top of your screen, give this intent the title create_name.
  • Click to expand the Actions and parameters section.
  • In the Parameter name section, enter “name.”
  • Next, we need to define an entity, which will be responsible for extracting parameter values from the user input. Dialogflow has a pre-defined “name” entity you can use, so start typing @sys.given-name, and then select it from the subsequent dropdown when it appears.
  • Make sure the conversation doesn’t progress until Dialogflow has learned the user’s name, by selecting Required.
  • Click Define prompts and provide a few follow-up phrases for the Assistant to repeat until it gets the necessary information, such as “Sorry, I didn’t quite catch your name! Can you repeat it?” If at any point the user gives an unexpected response, our Action will cycle through these prompts, until it manages to extract the correct parameter.
  • When you’re happy with the phrases you’ve entered, click Close.
  • Click to expand the Training phrases section.
  • In the Add user expression field, enter “My name is John” and then press the Enter key.

By default, Dialogflow should recognize “John” as the required parameter, and assign it to the @sys.given-name entity.

Repeat this process for variations on this phrase, such as “John is my name,” “I’m called John,” and “John Smith.”

If Dialogflow ever fails to assign @sys.given-name to “John,” then you can create this assignment manually:

  • Highlight the word “John.”
  • Select @sys.given-name:name from the dropdown.

Create and deploy your webhook

Now that our agent can recognize the name parameter, let’s put this information to good use! You can address the user by name, by creating a Dialogflow webhook:

  • Still in the intent editor, click to expand the Fulfilment section.
  • Select Enable fulfilment.
  • Drag the Enable webhook call for this intent slider to the On position.
  • Save your changes, by scrolling to the top of the screen and then clicking Save.

Next, create the webhook using Dialogflow’s Inline Editor:

  • Select Fulfilment from the Console’s left-hand menu.
  • Push the Inline Editor slider to the On position.
  • Make sure the index.js tab is selected, and then copy/paste the following:
Code
'use strict';

//Import the Dialogflow module from the Actions on Google client library//

const {dialogflow} = require('actions-on-google');

//Import the firebase-functions package//

const functions = require('firebase-functions');

//Instantiate the Dialogflow client//

const app = dialogflow({debug: true});

//Handle the create_name intent//

   app.intent('create_name', (conv, {name}) => {

//Construct the conversational response//

    conv.ask('Nice to meet you ' + name + '. Would you like to hear a joke?');
});

//Set the DialogflowApp object to handle the HTTPS POST request//

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
  • Deploy your webhook, by clicking the Deploy button. Dialogflow will now provision and deploy your webhook code on a managed environment, using Cloud Functions for Firebase. This may take a few minutes, depending on the speed of your Internet connection.

In the above code, “name” refers to the parameter we defined in the intent editor.

Test your Action

You can put your project to the test, using the Actions Simulator:

  • Make sure you’ve enabled the necessary permissions, by heading to the Activity controls page and checking that the Web & App Activity, Device Information and Voice & Audio Activity sliders are all set to On.
  • Back in the Dialogflow console, select Integrations from the left-hand menu.
  • Find the Google Assistant card, and select Integration settings.
  • If you encounter a Check auto-preview setting dialog, leave Auto-preview changes enabled.
  • Select Test. Dialogflow will now upload your agent and launch the Actions Simulator in a new tab. In the Suggested input field, type “Talk to my test app” and press the Enter key on your keyboard.
  • Type your name, and press Enter. Dialogflow should now address you by name!

Keep the conversation going with follow-up intents

Since we asked a question, we need to be able to handle the answer! Let’s create two follow-up intents to handle a “Yes” and “No” response:

  • Select Intents from the left-hand menu.
  • Hover over the create_name text, and select Add follow-up intent when it appears.
  • Select Yes from the dropdown menu.
  • Repeat the above steps, but this time select No.

You can now edit these intents. Let’s start with “no”:

  • Select the create_name – no intent.
  • Click to expand the Responses section.
  • Enter the following static response: “Okay, see you next time!”
  • Since we’ve said our goodbyes, find the Set this intent as end of conversation slider, and drag it to the On position.
  • Scroll to the top of the screen, and then click Save.

Now we need to edit the “yes” intent:

  • Select Intents from the left-hand menu.
  • Select the create_name – yes intent.
  • Expand the Responses section.
  • Enter the following response: “Would you like to hear a bad joke about cats or dogs?”
  • Click Save.

Creating a custom entity

So far, we’ve stuck with Dialogflow’s ready-made system entities, such as @sys.given-name, but you can also create your own entities. Since there currently isn’t a @sys.cat or @sys.dog entity, we’ll need to define them as custom entities:

  • Select Entities from the left-hand menu.
  • Click the Create entity button.
  • Name this entity catOrDog.
  • Select Click here to add entity.
  • Under Enter reference value, type “Cat.”
  • Similar to training phrases, you need to enter a few synonyms representing the different ways that users may indicate they want to hear a joke about cats. After typing each synonym, press Enter.
  • Select the subsequent Click here to edit entry field.
  • Enter “Dog” as the reference value, and then add some synonyms.
  • Click Save.

Using your custom entities

You apply these custom entities to your intents, in exactly the same way as system-defined entities:

  • In the left-hand menu, select Intents.
  • Click Create intent.
  • Name this intent “Dog or cat joke.”
  • Under Training phrases, enter “Dog” and “Cat.” Dialogflow should recognize these values, and map them to your catOrDog entity.
  • Click Save.

Unleash your best bad jokes!

Our final task is to start inflicting bad jokes on the user:

  • Select Intents from the left-hand menu.
  • Click to expand the create_name intent.
  • Hover over the create_name – yes follow-up intent, and then select Add follow-up intent.
  • Select Custom from the dropdown.
  • Select your intent, which launches the intent editor.
  • The automatically-generated intent name is pretty long, so let’s change it to “Dog.”
  • Under parameter name, type “Dog.”
  • In Entity, start typing “catOrDog,” and then select it from the dropdown when it appears.
  • Under Value, type “Dog.”
  • Enter training phrases like “I want to hear a dog joke,” “tell me a bad joke about dogs,” or “dogs.”
  • In the Text response section, type your most cringeworthy canine joke. I’m using “What do you call a large dog that meditates? Aware wolf.”
  • Nobody will want to continue talking to our Action after such a terrible joke, so enable the Set this intent as end of conversation slider.
  • Click Save.

Repeat the above steps, to create your cat intent, and that’s all there is to it!

The only thing left to do is fire up the Actions Simulator and see how the Action handles the various responses.

Wrapping up

This Action may be straightforward, but it demonstrates many of the tasks you’ll perform over and over when creating your own Actions. You can take these techniques for learning the user’s name, extracting parameters, delivering static and dynamic responses, and training your Dialogflow agents, and apply them to pretty much any Action project.

If you decide to develop Google Assistant Actions that do more than deliver a couple of bad jokes, share your work with others and submit your Action for approval!

Will you develop for the Actions directory? Let us know in the comments below!