Let’s Build An Action With Dialogflow

Sreelal TS
7 min readMar 22, 2019

Welcome back! I’m so glad to have you here again!

Actions can be very useful in some cases. For example, as I mentioned in one of my previous articles, think you’re running a local business, to say, a local bike repair shop. It’ll be a super heavy task of handling your clients by managing a lot of phone calls to make appointments or giving info about your business. Let’s post a brand new agent for this. Now, things have been changed, all the above-mentioned tasks are done by your Action, and you’re more happy with your business. Today, we’re going to build such an Action. This idea was illustrated in Actions On Google Session at Google IO 18 by Matt Carroll and Daniel Imrie-Situnayake (The session video is added at the end of this article).

Let’s start from basics. The Assistant works with more than 500+M devices and makes the most of the best features of each device. So, extending the Assistant to get things done across Google require your Actions to have the ability to understand the users. This is where Dialogflow comes in! Using Google’s Machine Learning and Natural Language Understanding tools Dialogflow makes your Action super powerful! Let’s jump in and start from scratch! All you need to have is a Google Account. I hope, you’re all set! Now, make sure that the necessary permissions are enabled!

Check your Google permission settings

In order to test the Action that you’ll build for this codelab, you need to enable the necessary permissions.

  1. Go to the ‘Activity Controls’ page (https://myaccount.google.com/activitycontrols).
  2. Sign in with your Google account, if you have not already done so.
  3. Ensure that the following permissions are enabled:
  • Web & App Activity (you should also enable the option to ‘Include Chrome history’)
  • Device Information
  • Voice & Audio Activity

Create an Actions Project

Actions project is a cover for your Action which contains the metadata like description, icon, invocations, etc. For creating a new Actions project follow these steps:

  1. Log on to Actions On Google Console
  2. Click on the Add/Import project button.
  3. Give your project a name and click on the create project button. (Wait until the project creation is done and you’re redirected to a page to select a category for your Action).
  4. Rather than pick a category, scroll down to the More options section and click on the Conversational card.
  5. Now, Click Build > Actions in the left navigation panel.
  6. On the Create Action dialog, select Custom Intent and click Build. This will open the Dialogflow Console in another tab.

Create a Dialogflow agent

Now that you’ve built your Actions project, create a Dialogflow agent and associate it with your project:

  1. 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.
  2. Click Create.

(source: Assistant Codelab 1)

Welcome the user

Now, first, let’s define how our agent should welcome the user. For this, click on the Intents in the left panel and select Default Welcome Intent. There you can see a bundle of sentences under Training Phrases section. These are the user utterances, and what an intent really do is match all those training phrases to one single intent, here the Default Welcome Intent. Scroll down to the Response section. There you can define how your Action should respond to the user. So, here we are welcoming the user to our Action. So, add something like:

  • “Hey there, welcome to my bike shop! How can I help you today?

Adding more variations will make your Action more life-like. So, let’s add one more welcome statement.

  • “Hi, a warm welcome to my bike shop? What are you looking for today?”

Now, click Save. And, let’s test it out! In the simulator, type: “hi” and hit enter. Now, in the simulator, you can see your Default Welcome Intent is matched and the agent is responded well.

TL;DR

  1. Add some user utterance in the Training Phrases section.
  2. Add some text response in the Response section.

Hours and Appointment.

Hours

Well done, you’ve now all set to welcome the user. Now, let’s add some functionality. As we’re creating this Action for our Bike repair shop, there might be users who ask for your working hours and get an appointment. Let’s set up an intent to manage the queries about hours.

  1. First, create a new intent. For this, click on Intents in the left nav. And click on Create Intent.
  2. Give the intent a name. Let’s call it hours’.
  3. Now, add some training phrases. Think about what our user might say when they want to know about the work hours. We’re going to add:
  • What are your hours?
  • When do you open?
  • How late can I come in?

4. Add some response too! Like:

  • We’re open from 9 am to 5 pm every weekday.

You know this is not an accurate answer for all the queries about work hours. Suppose, the user asks for “Are you open now?”, our agent answer the same as we defined! So, here we need to do some calculations like, getting the current time and comparing with work hours to give the accurate answer. We can use Fulfillment for this. Sorry, but we’re not using it right now! We’ll be covering it may be in another article :)

Appointment

Now, let’s create an intent for managing appointments.

  1. As done before create a new intent, and name it appointment’.
  2. Add some Training Phrases. Like:
  • I’d like to get a bike tune-up
  • I’d like to schedule an appointment for next Tuesday
  • Can I schedule service for noon?
  • Can I set up an appointment for my bike tomorrow at 2 pm?
  • I’d like to come in at 9 am on May first.

Add some response and test it!
Remember that, we should accept a date and time. And if the user didn’t provide a date and time in the query we have to ask for it.

  1. Under Action and parameters, make both parameters, ie. date and time, required. Create the prompt text to ask the user for when they want to come in.
Making the parameters required.

You can get the resolved value of the date and time parameters in the response using $date and $time.

We might want more information, let’s say the appointment type. Let’s ask the user for the appointment type. So we’ll add a prompt for appointment type in all our responses. We’ll add “Do you need a tune-up or repair?”.

There we go! Now, we need to define what types of appointments we accept. To do this, we will create a custom entity.

  • In the left nav, click on Entities > Create Entity
  • Name your entity. eg: AppointmentType

This time we have two types of appointment, ie. Service and Fix. Define it as in the screenshot below:

Defining our custom entity.

Now we need to create another intent to accept the answer for the type of appointment. For this, we will add a Follow-up intent. It’ll be only triggered only after the appointment intent is fired. Hover on the appointment intent in the Intents tab. Click on the Add follow-up intent option and select custom. In the training phrases, add:

  • I need a repair
  • Can you service my bike
  • tune-up

As a response, we have to confirm the appointment. So, need to get the date and time the user set in the parent intent (appointment intent). We can do access the parent intent’s parameters by
#<InputContext>.<parameterName>. eg: #appointment-followup.date

It’s all about input and output context in the Dialogflow. You can learn more here.

So, our response will look something like this:

  • Okay, I’ll schedule a $AppointmentType for #appointment-followup.date, at #appointment-followup.time! We’ll see you then!

Now let’s check how this works on the Actions On Google Simulator. Click on the Integrations > Google Assistant. Click on the Test button on the pop-up box. This will lead you to Actions On Google Simulator. Test your Action there!

Hurray! It works perfectly!

Bonus Content

The interaction with your Action can be even improved with Rich Responses like Suggestion Chips, Basic Cards etc. Try it yourself!

  • Add a new intent to give information about your shop, use a Basic Card to give Rich Response.
  • Try using some Suggestion Chips so that the interaction with your Action will be improved!

See the IO Session here:

Thanks!

--

--

Sreelal TS

codes • dreams • thoughts . 💙 Organizer, Flutter Kozhikode 🥇 Platinum Product Expert at Google PE Program #Flutter #GDGKozhikode