Google dialogflow how use dynamic response strings? - dialogflow-es

I'm trying to make a simple dialogflow conversation. I have made a simple conversation to ask a person who they are trying to call and then says if a person is available / unavailable, which is just a text reply. When a person is unavailable I have potentially 3 actions the user can do, leave a voicemail, redirect to a colleague or wait until the person is available.
The message I would return is
"I'm sorry nameOfPerson is not available, would you like to leave a voicemail message, redirect to a colleague or wait until nameOfPerson is available?"
Which works, but how can I make it so that lets say there is no voicemail the intent only response with, "I'm sorry nameOfPerson is not available, would you like to redirect to a colleague or wait until nameOfPerson is available?"
Currently the only way I see to fix this is making 7 intents with all different contexts like:
Intent1: voicemailContext
Intent2: redirectContext
Intent3: voicemailContext, redirectContext
Intent4: waitingContext
Intent5: WaitingContext, redirectContext
Intent6: waitingContext, voicemailContext
intent7: waitingContext,redirectContext, voicemailContext
All that work for just a small adjustment seems way to complicated. Also currently I'm sending those options in a call which I would much rather not since people could also just say those options and get a match

Dialogflow is very poor at handling logic. You can sorta do it, as you've seen, but this does lead to overly complicated models. One thing to remember that might help with this is that Intents are good at modeling what the user says, but that you're still responsible for what you do with that.
Better in many cases is to put the logic in a fulfillment webhook. This lets your code determine what the best response is and send that response. Depending how you want to structure your replies, you have a few other options:
You can send a context back in the reply. This would limit which Intents can get triggered when the user replies.
Another approach is to only have one Intent for the reply, but to use Entities to represent what sorts of things they can do. Then have your fulfillment, again, determine if they have given you a valid or invalid reply and respond accordingly.

Related

Response based on user input, dynamic reponses

Good day,
I am currently trying to create an intent that is able to put out responses depending on the user input. (The chatbot should be implemented on a website later)
Let's say we have an entity called cars with three entries: "Volkswagen" "Audi" "Ford".
Now when the user types in something with e.g. Audi in it, the response will correspond to this. Something like this: If Audi then give this response, if Ford then this response.
I couldn't find anything helpful yet.
Thank you in advance!
Remember that Intents represent what the user says and not how you handle this or how you reply. Although Dialogflow does offer the ability to respond to Intents, these aren't based on specific values that may appear in parameters.
There are, however, a variety of ways you can handle what you're doing, based on the rest of what you're trying to do.
Multiple Intents
One solution is to create an Intent for each type of thing that the user may talk about. You could then put the response you want for each in the response section of that Intent.
This is probably a bad approach, but may be useful in some ways. It requires you to duplicate phrases between the different Intents, which leads to a lot of duplication. On the upside, it does let you vary the replies, and truly represents the Intent of what the user is trying to say.
Using Parameters with Fulfillment
A better approach is to have a single Intent with many phrases representing what the users can ask. These phrases would have parameters of your Entity Type.
You can then enable Fulfillment for this Intent and write a Fulfillment webhook for the Intent that would look a the value of the parameter and send back an appropriate reply.
Using Parameters with DetectIntent
Since your ultimate goal is to embed this on a website, it may be more appropriate to have your website show something different based on what the user has said. (For example to show a picture of the car in another pane or links to different pages, to use your example of cars.)
In this case, your chat client (or a proxy) would be calling the DetectIntent API. You can structure your Intent similar to above, with parameters of the Entity Type, and the reply that is sent to your client would contain the Intent along with the value of the parameter. Your client then can check the value of the parameter and change the display accordingly.

How to implement a binary decision tree on google actions using dialogflow fulfillment 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.

Is it possible to deviate from a conversation flow and come back to the original flow in DF

I am trying to develop a chat-bot in google Dialog Flow where user deviates from the original conversation flow (CF) but ends up coming back to original somewhere in the middle.
bot responses are in bold
For example:
original CF: hi -> how may i help you -> i would like to go travelling -> OK, i would suggest Europe. would you be interested? -> yes -> alright here's the price
deviated CF: hi -> how may i help you -> i would like to go travelling -> OK, i would suggest Europe. would you be interested? -> maybe -> Europe has lot of beautiful destinations u can travel to. would you be interested -> yes -> alright here's the price
The only way i found to implement this is to make a new intent and develop its followups which is making this very redundant. I had to develop two separate intents fully. Is there any way i can make an intent just for the deviated CF and join it to the original intent?
One simple solution is to make many follow-up intents, but that is never ending process.
Here is another approach I want to suggest:
Make a list of important intents which you want to handle in case of
deviation
When the intent is hit, save that to some DB (or cache), let's say
unfinished_intent
In each request or every 2-3 requests check the value of
unfinished_intent, if it contains some intent name, prompt for it
After your intent is fulfilled, delete the unfinished_intent
This is just an idea, how to implement is upto you.
I suggested this because it is generic and it will catch all the cases.
Hope it helps.
Keep in mind that the user can change the direction of the conversation at any time. So using a long chain of followup intents is a bad idea. Even using a short chain is a bad idea. Followup intents should be limited to fairly narrow circumstances, and in most cases they're not wise nor necessary.
Instead, keep track of the information that you have about the user and the information that you still need as part of a context. If you are engaged in a side conversation, or have made a recommendation, keep track of that as well, since the user may ask questions about it. Build many top-level Intents that represent what the user is saying, not where you are in the conversation or how you plan to reply.
See also Thinking for Voice: Design Conversations, not Code based on this answer on StackOverflow.

How to create a search form with dialogflow

I am trying to make a search algorithm with dialogflow that could take any combination of: first name, address, phone number, zip code or city as input to a search algorithm. The user does not need all of them, but we will refine our search with each additional answer until we only have one result. Basically we are trying to identify which customer we are talking to.
How should this type of intent (or set of intents) be structured? We have tried one intent with multiple parameters, but we do not need all of them to be required. We have also written a JavaScript function for fulfillment but how can we communicate back to dialogflow as to whether we need more information?
Thank you very much for your help.
Slot filling is designed for this purpose.
Hope that helps.
Please post more code/details to help answers be more specific.
First, keep in mind that Intents reflect what the user is saying, and not typically what you're replying with or what other information you need. Slot filling sometimes bends this rule, but only if you have required slots.
Since you don't - you need a different approach.
This can be done with a single intent, although you may find that multiple intents make it easier in some ways. The approach is broadly the same:
When you ask the question, make sure you set an Outgoing Context with a relatively short lifespan (2-3 is good) to indicate you are collecting user info.
Create an Intent (or Intents) that have sample phrases that capture the information you need.
Some of these will have obvious entity types (phone number and zip code) while others will be more difficult (First name has a system entity type, but it doesn't include all possible first names).
You will need to create sample phrases that collect the parameters by themselves, along with phrases that make sense. You're the best judge of this, and you should probably write some sample conversations before you write the phrases.
In your fulfillment, you'll figure out if you have enough information.
If you do, you can reply and clear the Context that was set. (Clearing it is important so Dialogflow doesn't match the information collecting Intent again.)
If you do not, you can add the information you have as parameters to the Context so you can save it for later processing, make sure you reset the Context lifespan (so it doesn't expire), and prompt the user for additional information. Again, having a conversation mocked out ahead of time will help here.

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.

Resources