Dialogflow application is a Google framework that provides NLP / NLU (Natural Language Processing / Natural Language Understanding). Dialogflow enables you to create an Intelligent Assistant and manage what the Assistant understands and how it responds.
For each Assistant, you can define multiple end-user’s intentions called Intents. When an end-user writes something, Dialogflow matches the expression to the most suitable Intent, and your Assistant responds according to your defined response for that intent.
In order to test this you can just download the following example and import it to a pre-created assistant in Dialogflow:
This example includes:
Important: Ensure that Express Server, Axios, and Dialogflow client from npm was installed automatically with “npm install”
Note: You can check the complete code here: https://github.com/Mindbehind/mb-whatsapp-dialogflow-demo
In order to receive WhatsApp messages, expose an HTTP endpoint implementing the POST method. Set this endpoint as a webhook URL in your MindBehind WhatsApp Account (see https://www.mindbehind.com/knowledge/whatsapp-api-webhook)
On the aforementioned code example https://github.com/Mindbehind/mb-whatsapp-dialogflow-demo/blob/master/src/index.js you will see that we have created an Express server to listen to the port defined in the config on the endpoint:
The WhatsApp message comes with many parameters, but all we need here are these four:
1. From: phone number of the sender
2. To: phone number of the recipient
3. Content.text: the body of the message
4. allRequiredParamsPresent: information on whether the intent was fulfilled or not
Create a session to start a conversation with the Dialogflow chatbot. The session can contain several questions, the answers to which Dialogflow uses to produce the desired outcome. A session identifier can be any string. We chose the sender’s phone number in messageHandler in https://github.com/Mindbehind/mb-whatsapp-dialogflow-demo/blob/master/src/index.js inside messageHandler function, you will find see a screenshot demonstrating the start of that function below.
To detect the intent from a message, use a specifically formatted query in the conversation with the Dialogflow bot as you can see in the same function. The result of this query is a JSON with many different fields. One of the fields is an “action”. The field contains the name of the action (created in Dialogflow in the previous part of this tutorial).
You can vary your next steps based on what action has been detected. In our example, your action “fashion.trends” has been detected and this is how you can react:
TrendsHandler is the function that creates a specific WhatsApp message that reacts to Fashion Trends intent. If the intent has not been fulfilled yet, it means that the goal hasn’t been reached and the conversation continues, probably because the user needs to add some more info.
When the intent is fulfilled, the conversation is over; we have reached the goal. In this case, the user successfully asked the chatbot to send them information about specific products.
This was a short example of how to do that. There was also a face database mockup in our example. You have to get your real product data from your own database, of course.
See how we communicate with Whatsapp API in our sendWhatsappTextMessage function. We use Axios, a promise-based HTTP client, that will handle all around the API calls themselves, from parsing JSONs to handle HTTP status.
Note: For communication with WhatsApp API, you will require a MindBehind account API key (MB-API-KEY).
You can check how we wrote communication functions inside the next file:
Congratulations, you have created your first Fashion Chatbot! Try initiating the conversation with a question, for example: “What are the trends?”