Building and integrating a Facebook Messenger Chatbot using Dialogflow
January 26, 2021
Customer service plays an important role in building the brand reputation for your business. Often customers get in touch with this department because they want to address a problem regarding your services or simply ask a question about one of your products. Having an easy way for your customers to reach your staff members is essential, as it will directly impact your reputation.
There are multiple ways to make this interaction happen. Nowadays, more people prefer to communicate online via text messages instead of a phone call. They often contact you through your social media accounts or use the online chat available on your website. This approach may seem like a perfect solution, but even then, a staff member would have to manually reply to a customer asking even simple questions such as ‘what are your business hours?’.
Dialogflow Agents – engage with your users in a personalized way
A chatbot is a computer program that simulates and processes human conversation, either written or spoken. They can intelligently interact with your users, creating very effective conversational experiences when tailored to a specific business need.
Google Dialogflow is a platform that allows you to create chatbots (also known as agents) and integrate them into almost every popular service or assistant out there, like Facebook Messenger, Twitter, Amazon Alexa etc. It also allows you to use webhooks (user-defined HTTP callbacks) in order to make your chatbot smarter by gathering information from external APIs. Thinking about asking how is the weather like in a specific region? Chatbots can give you an answer for such a question by relying on Webhooks.
How does it work?
Dialogflow uses Natural Language Processing (NLP) technology. It translates natural language into machine-readable data by training a machine learning model with your examples.
This process can be described as the experience of collecting what the user is saying, mapping
it to an intent, taking actions on it and providing the user with a response.
Figure 1. Example of how Dialogflow handles a user utterance
Utterances are what an end-user would say to invoke the chatbot. These are then mapped to a specific intent, allowing us to collect parameters from the user. Once we know our users intent, Dialogflow can trigger specific logic in your service by configuring actions, while returning a response.
- Intents – capture the intention of a user who’s interacting with our chatbot
- User Expressions – each expression gets mapped to an Intent. These are the training phrases Dialogflow uses to train a ML model with many more similar phrases
- Entity – for each Expression mapped to an Intent, our chatbot has to figure out the thing the user wants information on. This is made possible with the help of Entities
Setting up the Chatbot
In this section we’ll build a Chatbot based on an open-source template, using the Verily COVID-19 Pathfinder virtual agent template. As the world responds to the COVID-19 crisis, providing your public with accurate timely information is crucial to keep people safe and informed. This template sets up your agent to answer frequently asked questions based on the latest Centers for Disease Control (CDC) guidance.
Each Dialogflow agent has two default intents: a default welcome intent – matched when an user begins a conversation with your agent and a default fallback intent – matched when your agent doesn’t understand the user’s query.
Head over to the Dialogflow Console and create a new agent, selecting one of your existing Google projects. Proceed to import the COVID-19 template by clicking on the gear icon on the left panel and selecting Import from ZIP under Import & Export tab. Once the import process is finished, you should see lots of intents (besides your existing default intents) and several entities already defined for your chatbot.
Let’s take a look at a random intent provided by the template – coronavirus.treatment which has the following training phrases (defined above as User Expressions) :
“Should I take antibiotics?”
“Treatments for Webhooks”
“Is there a cure for COVID-19?”
“What do I take for coronavirus”
Training phrases are expressions you can expect from users, triggering the intent. The more expressions you add to an intent, the better your chatbot will be able in figuring out what you actually want. Each expression can have an entity that defines the topic the user needs information on. In our case, the highlighted words are linked to the @covid-19” entity. Finally, our Agent needs to respond with something for this intent. We’ll simply provide a static text in the Responses section.
Do you want to see this chatbot in action? There is a quick way to test your chatbot directly from the Dialogflow Console, without having to integrate it with a custom service. Just ask a COVID-19 related question using the right panel.
NOTE: coronavirus.closure, coronavirus.confirmed_cases and coronavirus.death intents require you to set up the Google Maps and BigQuery APIs, steps not covered by the scope of this article.
Facebook Messenger integration
There are several steps you need to take before using the chatbot in Facebook Messenger. While I provide the main steps below, feel free to check the official documentation for any additional information.
1. Create a Facebook page
Don’t forget to save the Page Access Token, because you will need to use this value when setting up a Facebook App. In case you don’t have this token anymore, use the GraphAPI Explorer to retrieve it.
2. Create a Facebook app and link Facebook Messenger Platform to that application
In order to build and manage Facebook applications, you must sign up for a developer account using your regular Facebook account. Head over to developers.facebook.com to get started, then subscribe your app to the Facebook Page you created earlier. Don’t configure the webhook or test your integration yet.
3. Configure the integration from the Dialogflow Console
Enable Facebook Messenger service from the Integrations tab in the left sidebar menu. Provide any private token you desire (Verify Token). Enter the access token you copied when creating the Facebook page (Page Access Token). In case you don’t see the Integrations tab, keep in mind that when a non-default region is selected in the Dialogflow Console, the Integrations feature is not available (among other things).
4. Configure the Facebook webhook for your app
Use the Callback URL and Verify Token values you copied above to configure the webhook for your Facebook App. Be sure to enable messages and messaging_postbacks. Additional information on setting up a Facebook App can be found here.
At this point, the integration process is finished and we can test our Chatbot!
This chatbot will not be publicly accessible. Only admins or the user who created the Facebook App can chat with the chatbot, unless you make the app available to everyone by publishing it.
In today’s competitive market, you need to satisfy your customer’s expectations and do it in a way that makes you stand out from the crowd. Chatbots are affordable, help build trust for your business and can definitely do more than answering your customer’s questions.
Do you have any questions?