In the next post in our “Must Know Concepts of Dialogflow” series, we take a look at Dialogflow webhooks.
Here is the official definition:
Once again, there is a simpler way to understand what a webhook is.
Asking your chatbot to add two numbers
Let us define an intent for adding two numbers.
Now, in the response, we can get the two numbers that the user input, and define a response like this:
As you can see, we need to “fill in the blank” at the end of the response.
This is called fulfillment, because we are fulfilling the user’s request. To do this fulfillment, we use something called a webhook.
What is a webhook?
The webhook is some code (usually running on a server outside of Dialogflow) which performs your chatbot’s business logic.
In this case, since you want to add two numbers, the webhook will have some code which adds the two numbers. While this might seem like a lot of overhead to have a webhook just to do some simple math computation, that is how Dialogflow works.
The Dialogflow philosophy (as of this writing) is to offload all the business logic to your webhook.
Calling the webhook
First, make sure that you have switched on the “Enable webhook call for this intent” in the Fulfillment section at the bottom of the intent. (Note: you must do this for every intent which uses a webhook).
Webhook Data Flow
This is how data flows when you call a webhook.
1 User types a message to your agent
2 Your agent will map it to an intent
3 The result of this mapping will produce some JSON. To see this JSON, you can type the message into the Dialogflow test console without toggling the “Enable webhook call for this intent” and click on the Show JSON button at the bottom.
Clicking on the Show JSON button will let you see the JSON which will be sent to the webhook if the “Enable webhook” was toggled on.
4 The webhook receives this JSON as a POST request. The webhook code should parse this JSON data and extract relevant information.
In the JSON above, it will look at the “parameters” field and extract the two numbers to be added.
5 The webhook should return the result of its computation back to the intent as the response.
6 The intent will extract the relevant fields from the response JSON and display the result to the user
Dialogflow webhooks are used to perform any and all business logic in your chatbot.