Nowadays, users have so many apps on their phones that they’re either hesitant to download and install new apps in the first place, or they hardly ever use them after a short interaction shortly after installation. However, from designing user interfaces to building and maintaing a robust backend as well as a frontend for multiple screens, devices and operating systems; building apps takes a lot of time and effort. With decreasing levels of user engagement, and high development costs, building a chatbot instead of a mobile app may be the best way to offer your digital service. Chatbots integrate with messaging/voice assistant platforms that are already popular amongst billions of users, so user engagement isn’t an issue and building a chatbot is actually much easier than you might think.
Building a chatbot from scratch takes a lot of work because the interaction between the user and the bot through human language requires some Machine Learning and Natural Language Processing. However, if you build your chatbot using Google’s Dialogflow, then you don’t need to worry about any natural language processing. Dialogflow uses all of Google’s expertise in Machine Learning to handle all of the Natural Language Processing and integrates with your backend like a traditional mobile app or website would. In this post, I’m going to show you how to build a chatbot of your own for Facebook’s Messenger using Dialogflow.
1. Project Setup
Let’s begin by visiting Dialogflow’s website and entering the console by hitting the button at the top right corner. After signing in with your Google Account, and accepting some terms and conditions you should be redirected to the Dialogflow console, which looks like this.
In order to create a new chatbot, create a new agent by entering in an agent name, the default language and your time zone. Before we begin building our chatbot, let’s setup the integration with Facebook messenger by clicking on the integration tab on the side panel to the left. As you can see a Dialogflow chatbot can be easily deployed on many different messaging platforms and voice assistants. Although the integration process differs for each platform based on the differences in each platform’s APIs, the process is pretty similar. For Messenger you begin by getting access to the to Messenger’s API through Facebook’s developer console. Thereafter, you make the webhook on Facebook’s developer console the Calback URL provided on Dialogflow’s console, and get the Verify and Page Access tokens from Facebook’s developer console and fillout the fields on Dialogflow’s console as seen below.
2. Small Talk
Although we can’t build a chatbot that will know how to respond to every possible user input, enabling the pre-built small talk feature can go a long way in making our chatbot seem smarter. You can do this by opening the Small Talk tab from the side panel to the left and enabling it; some of the responses can be customized here as well.
3. Creating the Intents
An intent is essentially one complete interaction between the chatbot and the user. Most of the chatbot development is going to happen through these intents so let’s move to the intents tab on the sidepanel to the left. Let’s begin creating an intent by hitting the “Create Intent” button and keying in an intent name. The contexts section allows you to connect different intents together. For example, if you want an intent to be called after this intent you can configure this by adding an input context and if you want another intent to be called after this one you can add an output context.
The events section allows you to call the intent when a certain event occurs without the need for a match in the text input to the chatbot. For example, if you want to call the intent at the start of a conversation before any user input occurs you can add use this section to trigger the intent whenever the welcome event occurs. The events you can use are prebuilt, and defer based on the platform the chatbot runs on.
Next, we have the “Training Phrases” section, which isn’t optional unlike the previous two sections. As mentioned above, with a chatbot the shapes and forms a user’s input can take are infinite. Users will request for the same service in many different ways when interacting with a chatbot. This is problematic because we cannot hardcode all of these possibilities to call the intent that fulfills the required service. The solution is to use Machine Learning to make our chatbot smart enough to figure out what the user is saying and call the relevant intent. The “Training Phrases” section allows us to give Dialogflow some training data that correspond to this intent in order to train it’s NLP models. In other words, we type in a bunch of possible user inputs that should call this intent so that Dialogflow’s models get a basic idea of what inputs should trigger this intent. Adding more training phrases will make the chatbot better at figuring out when to call this intent. Here’s an example for some training phrases you might have for an intent that is designed to order a pizza.
The “Action and parameters” is very important if your chatbot needs to get some data from the user in order to perform a service. For example, for our pizza ordering intent, we need some data from the user in order to make the order like the type of pizza, and the pizza size. We can get this data by creating parameters in this section. There are a few settings we can configure for each parameter. The required setting should be checked if the parameter is required; you can think of the entity setting as a variable type or object type that this parameter will take the shape of; the parameter value is an identifier used to reference the extracted value from the user inputs; the is list setting should be checked if the parameter collects a list of data; and finally the prompts setting allows you to configure the questions that the chatbot will ask the user to get that piece of data. Here’s how I have configured the parameters for our pizza ordering chatbot.
One thing we haven’t accounted for thus far is if the user provides the required data without being explicitly asked for it. For example, a user might begin his interaction with our pizza chatbot by saying “I want a large peperoni pizza.” We can account for these situations by adding these possibilities to our training phrases sections and then annotating the data within each training phrase with the corrseponding parameters; you can do this by highlighting the data in each training phrase and typing in the name of the relevant parameter. Here is the adjusted training phrases for the pizza ordering chatbot.
The “Responses” section allows you to configure how the chatbot should respond to the user at the end of this intent. You can add basic text responses, or you can add unique responses for each platform that follow the conventions and take advantage of the features of that platform. You can configure these unique responses either by adding a custom payload and coding in the response based on the platform’s APIs or you can hit the plus button next to the default menu and configure them using the GUI. For Facebook Messenger, some of the prebuilt unique response types on Dialogflow are images, cards, and quick replies.
The fulfillment section is probably the most important because it allows you to get your own backend talking to the chatbot. You can create a pretty good chatbot without ever needing a backend, but for most use cases you’re going to need to connect the chatbot to a backend. Not all intents will need to work with your API so to enable the integration we have to enable webhook call for this intent from the fulfillment section. We also need to do some more configuration before this will work by moving to the fulfillment tab from the left side panel. Here we have to enable webhooks, and provide the URL, and authentication information for our API.
The graphic below shows the data flow when we connect up our backend to the chatbot. Dialogflow gets the user input using Messenger’s API, and then handled all of the natural language processing and completing the intent before sending the parameters to our backend, where we process the data and send a response back to Dialogflow to be relayed back to the user via Messenger’s API. This means that instead of configuring the response’s section during the intent configuration, we can send unique responses from our backend.
The JSON request to your backend from Dialogflow looks like this.
{
"originalDetectIntentRequest": {
"payload": {}
},
"queryResult": {
"allRequiredParamsPresent": true,
"diagnosticInfo": {},
"fulfillmentMessages": [
{
"text": {
"text": [
"string"
]
}
}
],
"intent": {
"displayName": "string",
"name": "string"
},
"intentDetectionConfidence": 0,
"languageCode": "string",
"parameters": {
"type": "pepperoni",
"size": "large"
},
"queryText": "string"
},
"responseId": "string",
"session": "string"
}
I’m not going to go through this entire request because the docs do a pretty good job of that, but I will go through the most important parts for any app. The intent object gives information about the intent, and the parameters object includes the data extracted by the chatbot for the required parameters. The response format from your backend to dialogflow should look like this.
{
"fulfillmentText": "Thank you for your order!",
"fulfillmentMessages": [
]
}
The fulfillment text value is simply the text response that will be sent to the user on messenger. The fulfillment messages object allows you to pass a custom payload like a quick reply, cards or images that are unique to the platform the chatbot runs on. The structure of the objects within this list will be based on the platform’s own features and APIs.
4. Welcome and Fallback Intents
When we created the new agent welcome and fallback intents were generated automatically. The fallback intent is called whenever the chatbot encounters some kind of error and the welcome intent is called as soon as a chat session is initiated by a user with the chatbot. Although these intents have some prebuilt responses and training phrases, you can adjust the configurations in order to suit your requirement.
I decided to write this post because it took me a while to figure out how to use Dialogflow, when I was building a chatbot for a project a few months ago. Chatbots are a great way to offer digital services to the modern day user, and I hope this post helps you make your own chatbot with Dialogflow.