Search code examples
dialogflow-esactions-on-googledialogflow-es-fulfillment

How to manage the conversation flow if face timeout limit (5 seconds) in Dialogflow / Api.ai?


I am making a bot on Dialogflow with a Fulfillment. Considering the given strict 5-second window in DialogFlow, I am getting [empty response] as a response.

I want to overcome this issue, but my web service requires more than 9 seconds for the execution.

I am considering to redesigning the conversation flow in such a way that we will start streaming audio till the Response is processed.

Example:
User Question: xx xxx xxx xxxx xxxxx?
Response: a). We'll play fixed audio to keep the user engaged for few seconds till it finds a response text in the back end;
b). Receive answers from the web service and save them in the session to display further.

How can I achieve this and how can I handle the Timeout issue?


Solution

  • You're on the right track, but there are a number of other things to consider.

    First, however, keep in mind that anything that is trying to "avoid" the 5 second timeout already indicates some issues with the design. Waiting 10 seconds for a reply is a pretty long time with something as interactive as voice! Even 5 seconds, which is the timeout, is a long time. (And there is no way to change this timeout.)

    So the first thing you may want to do is consider if there is a better/faster way to do what you want.

    If not, the rough approach would be something like this:

    1. Get the request from the user.
    2. Track a unique identifier, either tied to the user or tied to the session. You'll be using this as a key into some kind of database or data store.
    3. Start the API call as part of an asynchronous request or in another thread.
    4. Reply immediately that you're working on it in a way that the user will send another request. (See below for this issue.) You'll want to make sure that the ID is maintained as part of this session - so you'll need to save it as part of the Session data.

    At this point - you're basically doing two things in parallel.

    When the API call completes, it needs to save the result in the datastore against the identifier. (It can't save it in the session itself - that response was already sent back to the Assistant.)

    You're also waiting for a reply from the user. When it comes in:

    1. Check to see if you have a response saved for this session yet.
    2. If not, then go back to step 4. (You may want to track how many times you get here and give up at some point.)
    3. If you do have the result, reply to the user with the information.

    There is an issue with how you reply in step 4, since you want to do something that will guarantee you another request from the person expecting an answer. There are a few possible approaches:

    • The most straightforward way would be to send back a Media response to play a few seconds of "hold music". This has the advantage that, when the music stops, it will send an event to Dialogflow which you can capture as an Intent and then continue with step 5.

      But there are some problems:

      • Not all versions of the Assistant support the Media response. You will need to check to confirm the feature is supported before you use it and, if not, use another approach (see below).
      • The media player that is presented on some Assistants allow the user to stop playback, or will not correctly send an event when the audio stops in some situations. So you may never get another request in this session.
    • Another approach involves some more advanced conversation design tricks, so may not always be suitable for your conversation. Your response can say that you're looking up the results but then ask the user a question - possibly one that is related to other information that you will need. With their reply, you can collect this information (if you need it) and then see if you have a result yet.

      In some conversations - this works really well. For example, if you're looking up flights to somewhere, while you're looking that up you might ask them if they will need a hotel or rental car, which you might ask about anyway.

      Other conversations, however, don't easily have such questions. In these cases, you may need to ask something that isn't relevant while you stall for time.