How to throw a custom message using Dialogflow after three times of fallback

前端 未结 1 429
渐次进展
渐次进展 2021-01-22 10:56

I am developing a chatbot using Dialogflow, I would like to throw a message to user when the chatbot doesn\'t understand the user input for three times in a row and for the fort

相关标签:
1条回答
  • 2021-01-22 11:35

    This will not work the way you've designed it. Not quite for the reason you think, but close.

    You don't show the rest of your code (that's ok), but the counter variable is probably in a function that gets called each time it processes a message. When that function is finished, the counter variable goes out of scope - it is lost. Having multiple calls at the same time won't really be an issue since each call gets a different scope (I'm glossing over some technical details, but this should be good enough).

    One solution is that you could store the variable in a global context - but then you do have the issue of multiple users ending up with the same counter. That is very very bad.

    Your solution about keeping a counter in a database, keyed against the user, does make sense. But for this need, it is overkill. It is useful for saving data between conversations, but there are better ways to save information during the same conversation.

    The easiest solution would be to use a Dialogflow Context. Contexts let you save state in between calls to your webhook fulfillment during the same conversation and for a specific number of messages received from the user (the lifespan).

    In this case, it would be best if you created a context named something like unknown_counter with a lifespan of 1. In the parameters, you might set val to 1.

    The lifespan of 1 would mean that you'll only see this context the next time your webhook is called. If they handle it through some other Intent (ie - you understood them), then the context would just vanish after your fulfillment runs.

    But if your input.unknown handler is called again, then you would see the context was there and what the value is. If it doesn't meet the threshold, send the context again (with a lifespan of 1 again), but with the value being incremented by 1. If it did meet the threshold - you'd reply with some other answer and close the connection.

    By "send the context", I mean that the context would be included as part of the reply. So instead of sending just a string to sendGoogleResponse() or sendResponse() you would send an object that included a speech property and an outputContexts property. Something like this:

    var outputContexts = [
      {
        name: 'unknown_counter',
        lifespan: 1,
        parameters: {
          'val': counterValue,
        }
      }
    ];
    sendResponse({
      speech: "I'm confused. What did you say?",
      outputContexts: outputContexts
    });
    
    0 讨论(0)
提交回复
热议问题