Home / DialogFlow / Dialogflow webhook for website chatbots
DialogFlow

Dialogflow webhook for website chatbots

This is a post about where to host your webhook. However, this is not a post about cloud service providers. I am not going to talk about which one is better – Google Cloud or Heroku or AWS or Azure.

Rather, I am going to ask you to consider the option of placing your webhook outside of your Dialogflow process.

How the webhook works

Generally speaking, the webhook works like this:

  1. End user sends a message to your Dialogflow agent via
    1. a messaging channel such as Twitter or Slack
    2. through Dialogflow’s test console
    3. an API call
  2. Your agent maps the message to an intent
  3. The agent sends some JSON to the webhook
  4. The webhook performs business logic after extracting relevant information from the JSON
  5. The webhook generates a JSON object which contains the results of the business logic
  6. The webhook’s JSON is sent back to the Dialogflow agent (which is running on Dialogflow’s server)
  7. Inside the JSON, there is a field which the Dialogflow agent looks for (displayText) and either
    1. displays the text in the field if the end user is interacting from one of the messaging channels such as Twitter or Slack
    2. displays the text in addition to showing the JSON (via the Show JSON button) if using the Dialogflow’s test console
    3. returns (more correctly forwards) the JSON to the caller if it was an API call

The 5 second limitation

Dialogflow imposes a 5 second limitation on your webhook to respond. If it doesn’t, it is treated as a timeout. There are many who feel that if you cannot respond in 5 seconds, you might be doing something wrong. That is a valid point, but unfortunately in the real world you are going to encounter scenarios where the 5 seconds are not going to be sufficient.

Website chatbots

When you place your chatbot on your website (as opposed to using a channel such as Slack or Twitter), you have an interesting choice to consider.

Here is an alternate approach to doing everything the webhook can do.

  1. End user interacts with your website chatbot (i.e. sends it a message)
  2. From your website, you call the Dialogflow agent via REST API call
  3. Your agent maps the message to an intent
  4. The agent sends the JSON back to your website process
  5. The website process will perform the webhook business logic by calling another service
  6. After the service performing the business logic returns, the website process will return the response to your end user

Notice that in this case, you have worked your way around the 5 second limitation imposed by Dialogflow.

Questions

This raises many questions.

Will this approach mess up the state of the system (i.e. contexts)?

If you use the proper method of setting a unique session ID when interacting with your REST API, it shouldn’t cause any problems. The context is saved on Dialogflow’s servers, and is sessionID specific. When the next request goes to Dialogflow, as long as the correct sessionID is used, the system state will be maintained as expected.

What happens if the webhook updates the context via contextOut?

You can replicate this behavior by setting that same context when sending the next query from the user. Alternately, you can also explicitly set the context by calling the REST API.

What happens if the webhook uses a followupEvent?

You can simply trigger the event via REST API call from your website process. You will get the same overall effect.

Aren’t you using Dialogflow as a thin NLP layer in that case?

Yes, but that is true even if you use the traditional webhook.

Wait, can this still be called a webhook?

Hmm.. I don’t know. But does it really matter? 🙂

Conclusion

There are probably some issues I haven’t considered, and would also love to know your thoughts in the comments.

Lastly, I am planning to use this approach in one of the test chatbots I am building for my own site. I will keep you posted on the results. Instead of considering this as a solid recommendation, think of this post as a proposal for an alternative option (especially if the 5 second timeout rule is too hard for you to work with).

FREE COURSES

Related Posts

    • Yes and no.

      I have already written a detailed guide which explains the first step.

      As for calling a service to implement the webhook behavior, I don’t have any articles on that but it shouldn’t be very hard to do. If it is urgent, you can get in touch with me via the Services page for a paid consultation.

  • Hi Aravind,

    I would like to know about website chatbot building. I would appreciate if you cloud give some guideline for how we should build website chatbot step by step.

    Thank you.