How do I set context and followup event in one int

2019-06-04 06:19发布

I am trying to jump to a random question with followup event, and at the same time store the question number in the context. But dialogflow only jumps to the event without storing the question number. Is there a way to do followup event and store a context in one intent?

app.intent('Quiz - random', (conv) => {
    let rand = Math.floor(Math.random() * quiz_len) + 1;
    conv.data.current_qns = rand;

    conv.followup(`quiz-question${rand}`);
});

1条回答
相关推荐>>
2楼-- · 2019-06-04 06:55

Not really. The point of using conv.followup() is to make it as if the new Intent is the one that was actually triggered by the user. Remember - Intents represent what the user is saying not what you're replying with. You can include a parameter with the redirect, which I guess you can use to send the question, but this still would be the equivalent of a parameter sent by the user.

It isn't entirely clear why you feel you need to direct to a different Intent. Is it just to ask the question as part of the reply? Go ahead and ask it in the Intent handler you're in and store the number in the context directly.

Update

You've indicated in the comments that you want to structure things so you have a "random dispatcher" that then redirects to an Intent to ask a question, and that Intent has a Followup Intent that accepts the right answer (and possibly ones that deal with wrong answers).

This requires you to build a lot of extra Intents for all the various questions, and then the conditions on each question. This requires you to re-build this structure every time you want to add a new question.

Dialogflow works very well to navigate the structure of a conversation - so you don't need to use it to navigate specific questions, answers, and responses. Remember - Intents model what a user says in broad terms, not how our agent responds.

Here is a structure that might work better:

  1. There is an Intent that handles the user asking for a random question. The Intent fulfillment for this (ie - the handler function in your webhook) does the following:
    • Selects a question
    • Sets a context to indicate which question is being asked
    • Sends the context and the question to the user
  2. There is another Intent that handles user responses. All user responses. If this is a multiple choice question, you can set very specific phrases, but otherwise you may need to make this a Fallback Intent that has the context specified above as an Input Context.
    • Your handler for this compares the answer.
    • If correct, you clear the context, say so, and ask if they want another question. (Which would then loop to the Intent specified in step 1.)
    • If incorrect, you make sure the context is still valid and tell them they're wrong. Possibly increase a counter so you don't let them guess indefinitely.

This means you're only building two Intents that handle the question itself. You should add a few more. Here are a few to think about:

  • What happens if the user doesn't say anything at all? The "No Input" scenario should be handled differently than a bad answer.
  • If you're asking a multiple choice question, perhaps you'll use a list and need to handle a list response.

Finally, if you're using a list, you may want to do something fancy and use Dialogflow's API to set a Session Entity Type for just that question. This allows you to set what you're expecting (with some aliases) for what is correct and incorrect, and then use a Fallback Intent for the context to say that what the user said didn't make sense.

查看更多
登录 后发表回答