How to throw a custom message using Dialogflow aft

2020-04-14 09:34发布

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 forth time respond with a custom message (not the one of the options declared on the dialogflow interface)

One idea that I have is to make a counter within the input unknown action like this:

var counter = 1;
// The default fallback intent has been matched, try to recover (https://dialogflow.com/docs/intents#fallback_intents)
'input.unknown': () => {
    // Use the Actions on Google lib to respond to Google requests; for other requests use JSON
    if (requestSource === googleAssistantRequest) {
        sendGoogleResponse('I\'m having trouble, can you try that again?'); // Send simple response to user
    } else {
        if (counter == 3) {
            counter = 1;
            sendResponse('Custom message');
        } else {
            counter++;
            sendResponse('I\'m having trouble, can you try that again?'); // Send simple response to user
        }

    }
},

This would work, but idk if this will work for multiple user at the same time, I was thinking to create a storage for storing requests attached by a unique id and have a different counter for each request!

Do you have any better idea of achieving such thing in Dialogflow?

1条回答
姐就是有狂的资本
2楼-- · 2020-04-14 09:53

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
});
查看更多
登录 后发表回答