I’ve had the request to solve this issue a couple of times, so let’s have a post on this.
The idea is that for some back-end processes, you might need to have your Dialogflow agent waiting for a “long” time until you have the process finished and you can come back with an answer to the user. One example would be making a reservation in your backend hotel reservation system … if the process takes more than 5 seconds, the maximum time a Dialogflow fulfillment request can take, you need to handle a “long” waiting for your agent.
In voice enabled chatbots long waits are not the best user experience; I would recommend to keep some user interaction, play some music, do a quiz … you decide, but it is definitely something to keep an eye on. For text only chatbot this can be less critical.
So, let’s say you have a function called “transactionIsFinished” that checks if your process has finished or not. With the following code you can create a fulfillment to wait for a few seconds (maximum is 5 including the execution time for the function):
You would need a few intents to make this work:
- First the intent that triggers the wait; in a real scenario it would be the request that needs that “slow” backend process to finish. In my case I just ask to wait and it is called waitStart, and it will trigger the fulfillment above
- An intent to call when we need to wait more time. It is called waitMore, and it prompts the user to either continue waiting or leave
- 2 intents with the Yes or No answers to the previous question
- An intent to finish the flow once the process has completed
As you can see in the code both waitMore and waitReturnAfterFinished will be called using events (as explained in this post) from the waiting fulfillment. The waitMore intent looks like this:
The user will answer with a yes or not, which will be captured by the corresponding intents, which have the input Context to limit their scope (more info on contexts here):
The No intent will finish the flow, and the Yes intent will trigger the same fulfillment code above, so we need a dual mapping for that:
With that we are all set to wait as long as the user wants to have the transaction completed.
I work for Google Cloud, but this post are personal ideas and opinions