DEV Community

Jaydeep Borkar
Jaydeep Borkar

Posted on

Building a Chatbot using Dialogflow on Google Assistant for Beginners

In this tutorial, we will learn to build a chatbot (a virtual assistant) using Dialogflow which will work on Google Assistant.

But what is Dialogflow in the first place?
It’s a tool by Google to build conversational chatbots.

1) Go to the Dialogflow Console -- you will see a Dialogflow home page.
2) Sign in using your Google account.
3) After you sign in, click on Go to console.
You will be directed to the main console of Dialogflow. Now click on the drop-down icon next to the settings icon in the left-most column, and then click on create new agent.

Now, what is an agent over here?
Agent is an interface in Dialogflow which contains different sections called intents which further contain all the responses to the user’s queries. We will learn more about intents as we proceed ahead.

Creating a new agent
After you click on create new agent, give an agent name, default language, time zone,  and click on create. Keep create a new Google project as it is in the Google project section. It will create a new Google project for Actions on Google and Google Cloud. We will be integrating Dialogflow with Actions on Google console which would help us to deploy it on Google Assistant.
Now, you can see your created agent in the console. We will be creating different intents now for our agent. You can create as many intents as you want. Two default intents -- Fallback and Welcome are already created.

Let’s understand the Default Fallback and Welcome intent
Fallback intent comes into picture when the bot won’t understand the query triggered/asked by the user. It means there is no intent that matches the query asked by the user. It will give a response like sorry, I didn’t get it. Talking practically -- whenever you say something very unusual to Google Assistant, it will say sorry, didn’t get you. Here, after you said something strange that’s not a natural language, the Fallback intent got triggered and gave this response.

Default Welcome intent will greet the user once your bot is called. You can customize both the default intents with the responses that you want.

Now click on the + sign to create more intents. You can make as many intents as you want, depending on your use-case. Let’s say you are building a chatbot for an ice cream parlor, and the user wants to know all the flavors that you serve. Here, you will create an intent let’s say flavors. Then you will add the training phases.

Training Phrases
Training phases are the queries that the user will ask to your chatbot. You can add as many similar training phrases as you want. These are the queries that you think the user would most likely ask. For the flavor intent, you can add training phrases like -- What flavors do you have?, Show me the flavors, Flavours, I want to have a look at the flavors, and the list is endless. These training phrases are used to train a natural language understanding model internally. This is the best part about
Dialogflow, you don’t have to write any code to train the model. In circumstances, where you want real-time results, you can write some Node.js code which we will talk later about!

The Response section in the Intent
Now, in the Responses section, you can add the responses that you would like to show to the users. For instance, a simple response to a question in the training phrase -- Show me the flavors could be Mango, Vanilla, Chocolate. Make sure you append every response for every intent that you create with the sentence would you like to know anything else?, because for every intent except the exit intent (the intent that you would use when the user exits) it’s necessary that your response has a user prompt, else it would be against the guidelines published by Google and your app might not get published.

In a similar fashion, you can have as many intents as you like, as per your need.

Don’t forget to save every time you make an intent.

Making an Exit Intent
You also need to make an intent that would get triggered while the user leaves your app, which we would call as exit intent. For instance, if the user says, bye, you can have a separate intent for this with the training phrases related to bye, and in the response section, you can add anything that you like or something like feel free to visit again. Don't forget to toggle on set this intent as the end of the conversation at the bottom of this intent, as this intent will mark the end of the conversation with the user.

Using Node.js Code
Now, if you want to give some real-time output to your users, then you can write some Node.js code for the same. Go to the Fulfillment section at the left of the console, there you will see an option for Inline editor, toggle it on to enable it. You can write your code in index.js part. Let’s say, you’re making an app for a restaurant. The restaurant’s working hours are from 10:00 am - 10:00 pm, now if someone wants to check at 11:00 pm if the restaurant is open or not, instead of giving the static response like “Our timings are from 10:00 am - 10:00 pm”, the code will take the real-time parameters and will tell if the restaurant is open at that specific time or not. You can use Node.js code for different use cases, this is just an example. After you’re done, click on deploy.

You can have your chatbot integrated with different platforms like -- Slack, Facebook Messenger, Alexa, Cortana, Twitter, Viber, Skype, Telegram, and many others. Make sure to toggle on the platforms that you like. We will be sticking to the Google Assistant, which is the default one.

In the History section on the console, you can check how the users are interacting with your app and what all are they saying. You can use this as a tool to check what specific questions is your app unable to answer so that you can work on them.

In the Analytics section, you would see the analysis of how your app is performing.
Now it’s time to test the app. Let’s see how it works.  

Testing the App
On the Dialogflow console, at the right side, you would see See how it works on Google Assistant, click on that. Once you click it, you will see the test simulator. Let’s say that our app name is test app, then you will see the invocation phrase something along the line Talk to my test app on the simulator. You can test your app with all the training phrases/questions that you have used to train the model. You will get a better intuition about how your app will actually perform once it gets deployed on the assistant.  
Now, it’s time to deploy it.  

On the simulator page itself, you will see an overview section at the left. Click on that. In the Quick setup part, you can choose how your action should be invoked. In the Get ready for deployment part, you can choose what countries would you like to provide your app over Assistant. By default, it’s all 215 countries. You can also choose what surfaces would you like to run your app over, it’s both phones and speakers by default.

Now, go to the Deploy section, go to Directory information -- here you can add a short and long description for your app, sample invocations, background and profile image for your app, contact information, and privacy and consent. These all will be visible to your users. Feel free to click on Need help creating a Privacy Policy? to know how to make a privacy policy, please follow all the steps over there. You can various free tools to make a privacy policy for your app. Then you can choose what category your app belongs to and some other related information about your app below it. After you’re done with all these, click SAVE at the top.

Afterward, you can add Surface Capabilities and Company details in the Deploy section itself. After that, go to the release section within the deploy section itself -- there you would see an option to submit for production in the production part. Click on that and your app will be submitted for the review by Google. If it meets all the guidelines, it will be soon deployed on Google Assistant within 24-48 hours (you will receive an email from Google once it gets deployed successfully; and if it doesn’t, you will get an email in this case as well with all the errors so that you can fix them and submit for the production again). You can also opt for the alpha and beta versions for your app.

Once it’s deployed, your app will be available to over 500+ million devices without any installation, isn’t that cool?

Feel free to post any doubts or suggestions!

Top comments (0)