What should I do when my DialogFlow webhook takes too long (more than 5 seconds) to respond?
I have seen this question on the DialogFlow forum appear in a few different forms. Ultimately, this is the goal:
You want to send an unprompted, “out-of-band” message to the end user from your DialogFlow webhook
What is an “out-of-band” message?
One example is a webhook which takes too long to respond. You want to send a quick “OK, working on it…” type response. And then you want to do some kind of async processing, and then send a message back to the end user when the results are available.
Another example is where you are trying to send a “reminder” to the user. For example, you wish to remind a user on FB Messenger about an alarm they set on your service using your DialogFlow agent.
Push notifications don’t work
As it turns out, such “push” notifications are not possible in DialogFlow. What I mean by a push notification is:
DialogFlow figures out, based on the sessionID, what channel is being used (web, FB, Slack etc), and when a message is “generated” programmatically (say by sending a request to the /query endpoint), DialogFlow will take the responsibility to deliver the message to the appropriate channel.
The Request-Response mechanism
So this means everything in DialogFlow follows a “Request-Response” mechanism. For example, notice this comment (I think this is the best comment about this problem I have seen):
What about sending a message to the user when a web chat loads
Maybe you are wondering how I have created bots which interact before the user sends a message. Isn’t this like a push notification?
But even here, I do honor the “request-response” mechanism. That is, my website chatbot has some backend code which first makes a request to the REST API, gets the response, and then renders a suitable message in the chat window based on the response.
How to handle this scenario?
So now that we understand the limitation, we can think of ways to mitigate the problem. I will recap what I wrote in my comment on a related thread:
If your webhook cannot send the info back in 5 seconds, you should
- Either redesign the code so it can send the response in 5 seconds
- Or Use the repeated followupEvent approach suggested here: Fulfilment using webhook – takes time to get the data by @prabhasgupte
- Or use the callback URL approach suggested here: Fulfilment using webhook – takes time to get the data and here: Fulfilment using webhook – takes time to get the data by @Michael_Natkin
- or choose another bot building framework which doesn’t impose this onerous constraint 🙂