Complete Guide for Chatbot Development using Dialogflow
By: Devanshu Brahmbhatt
Tags: Chatbots, Bots, Chatbot Development, Dialogflow, Nodejs

F inally, your enthusiasm takes you to the right place for chatbot development, in this blog we’ll get hands-on experience of Chatbot development using Dialogflow, and Node JS. Believe me, after reading this blog you won’t face issues for developing Chatbot. Let’s get started

Wait! What is a chatbot and why it's needed!!!

Chatbot development booming nowadays, every organization wants to have a chatbot for their products the reason behind this is very simple but from the business aspect, it can be a major factor for their profits. In simple words, It gives you the power of automation. Let's understand the definition of a chatbot

A chatbot is a computer program that simulates human conversation through voice commands or text chats or both. In very simple terms its computer program tries to mimic human conversational behavior.

How it helps in business!

You can give an immediate customized response to your clients at any time. You can guide your clients for certain steps which makes ease for your clients You can identify the clear intention of the user that what he/she wants and respond accordingly.

The conclusion you gave personal assistant to your clients which can solve, identify the difficulties and can give the most suitable solution for that. It makes a big sense of a product's success.

Why Dialogflow and What is it?

Dialogflow is google software for chatbot developments. while developing a chatbot there are things that you need to care about, like

What is the user trying to say, What’s the exact intention behind the inputs, Which languages are supported or the user is interacting, How chatbot will respond to those actions, How to train your chatbot for better performance.

These are very important things while we are working on Chatbot development.

Benifites of Dialogflow.

Dialogflow is a Google service therefore when it comes to scaling the application or no. of request to handle we can trust Google. The major thing it's really easy to understand the mechanism and how it's working from the developer's perspective. You can train Phrases without taking support from any machine Learning model though internally it is using but that not our concern. It has supported many languages which is a great advantage to use. These are some of the key points to know.

Dialogflow is a natural language understanding platform that makes it easy to design and integrate a conversational user interface into your mobile app, web application.

Apart from Dialogflow, there are other frameworks as well which are working well, One of the famous frameworks is Rasa, it is also an Open-source framework but when your bot needs complex customization then you should go with scala also for some cases you need to train the Machine Learning Model for chatbot response.

Let’s Start Now

Before, Jump into the coding part it's important to understand how its work actually. These 4 keywords that we need to understand.

Agent: Agent handles conversations with your end-users, There is the name for a bot. Through devices when we calling the bot but actually is the agent that wakes up. In simple terms, the agent handles all your request and responses. Intent: The most important part to know, Intent as the name suggests its task is to identify user intention. In one project there can be many intents, according to user intention one intent will be called and then that invoked intent will respond to the user.

For an eg. if the user asks: “what is my company name?” then the Company related intent will be called and answer to the user. but if he/she asks “what is my colleague devanshu is doing?” then working_status-related intent will be called and answer.

I hope what is intent should be clear. if not then I have a diagram for you which we will discuss.

3. Fulfillment: In a chatbot, you can give a response in two ways.

Default response Dynamic response by fulfillment.

There are some cases like “what is my company name?” so in this case, you can set as default answer as “XYZ is a company name”, Whenever the bot accounted for this question it automatically answers. But what if someone asked “Who is my manager?” then for that, we need to check in our system in Database precisely and then we can give an answer. we must have an API that will return my manager’s name. In this kind of situations, we’ll take help from fulfillment,

Basically, It will called an API with all requested data for the dynamic response also called as Webook(invoked when any event occurred).

Finally, We are done with the basic concepts. Now let’s look at the diagram.

Dialogflow Process Diagram

Diagram Explanation:

The user will ask for something with the help of Google Assistant, Google Home, or any other device. The data(User input) will go to Dialogflow and Dialogflow will decide the intent for that request. Once we know the intention of the user, Now intent has two options either give default response or use fulfillment. If it's the default response then it's very easy just to respond but if it's fulfillment then it needs to go to our codebase. There are methods in Class that handle the request it will do some database or any operations for the request and send the response to intent back.

Finally, we are done with the explanation part let’s do some code now. Before that, there are certain things you need.

Setup the Dialogflow

Create your google cloud account: https://cloud.google.com/ Once you get your account confirmation, go to https://dialogflow.cloud.google.com/ You must land on this page

4. Now create your agent, I gave the “Audify” name to my agent.

5. It Takes sometime once you did it successfully you should get this window.

Hola! you are done with your Dialogflow setup now will create one intent name get-status. Our intent will give you the employee status whether he/she is available, not-available, busy, or away.

Create Intent

Now Click on Intents and create an Intent as “get-status”. Will go through with each important option,

Intent Creation Page

Training Phrases: As the name suggests we have to train our bot with some phrases so whenever similar types of input will come then this particular intent should be called. Try to put as many as training phrases for better performance.

2. Action and Parameters: For our example, if someone asked “what is the status of devanshu” In training phrases, it’s highlighted with a yellow color that suggested its dynamic value i.e it can be any name. In terms of Dialogflow it’s a parameter and it has a value as well. The entity is which type of this parameter is, in our case its @sys.given-name.

3. Responses: We can set default response if you needed, also you can set default response in fulfillment case also suppose your API is not called for any reason then you can at least send default response to your client.

4.Fullfillment: As already described for API calls and the dynamic response we need the Fulfillment, Enable the first option i.e Enable Webhook call for this event.

Please save your Intent, Congratulation you have successfully created your First intent, Now we are ready for How to respond to the request.

Coding

Let’s start with the directory structure for the Project. I am using Node.js and Express.js as my Backend framework and used MongoDB for storing the data. It’s a Standard NodeJs file structure but you have to add the Intents folder into the project.

Directory Structure for Chatbot Project

2. Create an index.js file in the Intent folder and paste this code. it’s the configuration file for Dialogflow, which intent-code should be called that will decide by switch-case.

Index.js file

3. Now Create an Availablity.js file under the same directory. Here we do our database operation, we fetch the status of an employee from the database and send a response to dialogflow.

Availbity.js

4. Now go to app.js your index file for the Node application and create a separate route for Dialogflow Fulfillment.

const Fullfillment = require('./intents/index'); app.use('/fullfillment', Fullfillment);

5. Install ngrok in your machine and run your application, “node app.js” for my case. Please run the below command in another terminal.

ngrok http 5001(port_name)

after running this command you’ll get hosted links. ngrok allows you to expose a web server running on your local machine to the internet

ngrok Images

6. Finally go to your Dialogflow select Fulfillment which is on the left side in Nav-bar, just after the Knowledge, Click on it, You’ll get this screen and paste your HTTPS URL to the webhook URL and now you are done with the setup.

Fulfillment Setup

7. Go to https://console.actions.google.com/ For deploying your application and test it on your Mobile phone.

The output of Your Chatbot

Conclusion

Finally, you are done with your basic chatbot application, there are many advanced features are there such as sentiment analysis and others but in this tutorial, I focused on the coding part and directory structure as it’s an important aspect. Thank you for reading and I thankful to Parth Shah for giving me guidance on Chatbot development.

I hope you will develop many bots now. I am equally enthusiastic to talk with you. Please share your doubts with me I loved to help :)