How to implement a binary decision tree on google actions using dialogflow fulfillment node.js? - node.js

I want to ask the users a few questions in my welcome intent and depending on their answers give them a particular output.
My problem is that, once the user answers the first question, the agent exits my welcome intent and tries to match with the next intent.
I thought I could solve this by matching it back to the welcome intent.
So ideally it should have gone like this :
Welcome intent -> 1st question -> user answer -> Welcome intent ->2nd question -> and so on
But actually this happens :
Welcome intent -> 1st question -> user answer ->Welcome intent -> 1st question
It will keep asking the first question.
To solve this , I started maintaining a flag for each question.
If question 1 was answered I would set its flag true and then use it to skip the first question when the welcome intent is matched for the second time.
This is a very convoluted way to do it and probably far from the best way to do it.
Can anyone point me in the right direction to implement it?
Thank you.
Edit : I have given up my old method. Let me explain what I want to do and then I can get guidance on the way I should implement it.
I have 16 different outputs and I would like to show one of them to the user depending on their answer to 4 questions. Each question will only have two answers as options and depending on the option chosen by the user for each question , I will pick one of the 16 outputs and display it to the user.
How do I accomplish this using diaglogflow node.js?

First of all remember that an Intent captures what the user says or does and not what you do with that information or how you reply to it. That part is best handled by your fulfillment. While we can use Contexts to limit which Intents will be considered for a user response they are probably most useful in this case to store your state and keep track of which questions have been asked and answered.
Under this scheme, your Intents remain responsible for capturing input and your fulfillment examines this input, changes state based on this input, and sends a reply based on the new state (what it needs to ask next). If the user's responses will be mostly the same (free form, or from the same set of phrases), you can even use the same Intent to capture this input and the fulfillment would use the state and input to determine what logic to execute. If you need different types of input, and thus different Intents, their handlers can still call common functions to do the processing and change the state/reply.
This is further discussed in Thinking for Code: Design conversations not logic

You can use the concept of the follow-up intents in Dialogflow Console to create a chain of questions/answers.
Here is an example of my chain of questions/answers:
Though I still strongly advise you to study also how input/output context works. Especially if you want to collect all user reply parameters in the one final fulfillment of the last step to avoid storing the user inputs in fulfillments attached to every intent of the chain.

Related

What is the right approach for handling direct and contextual questions in dialogflow?

I am creating a HR chatbot using Dialogflow. I am unable to figure out the right approach to have the bot handle both direct questions and questions asked in a contextual manner. For example:
Contextual case:
User: I want to know how many leaves i can get in a year
Bot: You get x number of leaves
User: Ok cool how do i apply for one then?
Bot: Follow this process to apply for a leave
Direct case (2 separate conversations with direct questions):
Conversation 1:
User: I want to know how many leaves i can get in a year
Bot: You get x number of leaves
Conversation 2:
User: I want to know how to apply for leave
Bot: Follow this process to apply for a leave
Approaches I have tried:
1) Adding input and output contexts to handle contextual cases and add direct questions to knowledge base.
The issue with this approach is that since we cannot give multiple phrases in knowledge base, most direct questions do not match
2) Have 2 intents, one with input and output contexts and one to handle direct questions. (For example: One intent would be leaves.apply.context which would have both input and output context set and would have training phrases like how do i apply for this and another intent leaves.apply.direct which would have training phrases like how do i apply for a leave and no context). I'm not convinced that this is the right way as I am essentially creating two intents for the same question with the same response.
So is there a recommended approach to solve this problem?
I don't think there is a recommended approach to this, it is a matter of taste. Your solutions are a nice way of approaching this, but I don't think you will get around the fact that you will have to make an extra intent with the same response if you want to allow a follow-up question to also be approachable directly due to the context.
If you really don't like to create two intents for the same response, I think you would be able to workout this scenario by creating two intents and dropping the contextual flow. Just create a HowManyLeavesAvailableIntent and a HowToApplyForLeaveIntent without any context and train the HowToApplyForLeaveIntent to trigger on phrases that would follow-up HowManyLeavesAvailableIntent and direct questions for HowToApplyForLeaveIntent. Due to the missing context, this might not be ideal because it can create weird mappings to intents, but it would allow you to have just one intent for how to apply for leave.

Google Action Intent with multiple input parameters

I'm looking to POC a small Google Action that gives a decision based on a few yes/no answers the user must answer first. Effectively I need to:
Ask a question
Store the result
Ask the next question
Store results
* Repeat until all yes/no answers given then end the conversation with a decision using saved values in the conversation.
Ongoing though I would like to add help to any of the questions. So the user could say "I don't understand", "Can you give an example", "Help" and it would give an example to help the user answer yes or no to the question they are up to.
After playing around through the labs it looks like I would do this by creating an Intent for my end decision and then nest a bunch of follow up intents within to gather all my yes/no answers. I feel this would get messy though as it would be a huge chain of them.
Is there a better way to design it?
Yes, using Followup Intents would be messy. It is almost never the right approach to the problem. Remember that Intents capture what the user has said and not what you are doing with what they have said.
If the questions are truly just yes/no, I would setup six Intents:
However you trigger the start of the questioning. This might be your Welcome Intent, or it may be something else.
Saying "yes" and equivalent
Saying "no" and equivalent
Asking for help
Asking to repeat the question
A fallback Intent that handles other unexpected input
When questioning begins, your fulfillment would setup a Context that contains the current question being asked and the responses for the questions so far. Answering yes or no would update the responses, determine what question to ask next, save this in the context, and ask it. Help, repeat, and the fallback Intent would respond with appropriate information based on the current question.

DIALOGFLOW! How to create a followup intent that is provoked when 3 previous intents have been answered in a certain way?

I’m working on a memory game with multiple levels on Dialogflow. Each correctly guessed question is a separate intent and all 3 questions need to be answered correctly to proceed to the next level. How do I create the game so that the second level (which will be the follow up intent) is only provoked when all 3 questions from the 1st level are answered correctly? Thank you in advance!
Instead of having the followup intent, you can maintain a counter in a parameter (see my answer here for storing parameters).
Enable webhook in all the intents.
Whenever a question is answered, check the validity and if correct increment the counter.
When the counter reaches 3, you can respond back with the response of your choice.
If you want to go by your way then, after answering 3 question correctly you will need to generate an event dynamically via webhook. And the fourth intent should be triggered using that event.

How do I set context and followup event in one intent?

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}`);
});
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:
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
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.

How can I make the content response rely on multiple intents

I know the question might sounds confusing, but basically my agent asks the user 3 yes-or-no questions and the user answer after each one, note that each question is an intent by itself, anyhow I managed to accomplish that successfully.
Now after the questions were asked and answered by the user, I want the agent to to give a final response, but it must be dependent on the answers which were given by the user..!
For example, if the user's answers were (yes-yes-no) then it gives a specific response and so on for each combination I set.!
Can this be done..!?
You can do this with API.AI using follow-up intents. You will have to track the user responses for each matched intent in fulfillment and then provide a final response.

Resources