How to use Parameters to modify Context in Google Dialogflow - dialogflow-es

We are running an experiment, where I need to manipulate the dialog flow responses based on the participant's ID. My thought is there is a way to set the Output Context based on a parameter value.
For example, we have a prompt that asks for the participant's id. This matches an intent with a "participantID" parameter. Now what I would like to do is set the output context to be the value of the "participantID" parameter. Then I could set the input context to be a specific "participantID".

Any parameters set on an intent will be kept in the output context and recoverable in posterior follow-up intents within the lifespan of such context.
Through the Dialogflow UI you can reference them in the initial response as $<parameter-name>.
For example, assuming your parameter name is id, the response can be: Welcome player $id,....
For values in the context in posterior follow-up intents use #<context-name>.<parameter-name>.
For example, in a second follow-up which has parameter answer and input context id-followup use Your answer has been $answer, if you are not player #id-followup.id please let me know your actual id.
If you need to use the Fulfillment Webhook I recommend a structure like the one illustrated here, i.e. a structure like:
//The user inputs a participant ID (name of the parameter is id)
function answers(agent){
const id = agent.parameters.id;
agent.add(`So your id is ${id}`);
agent.add(`Any extra questions...`);
agent.setContext({
name: 'question',
lifespan: 5, // Amount of follow-up intents this context will be kept
parameters: {'cid': id},
});
}
// Next functions that use the participant id as input context
function answers2(agent){
// Get the context and from it extract the id value
const cont = agent.getContext('question');
const particular_id = cont.parameters.cid;
// The rest of your code
Some documentation you may find useful includes Input and output contexts, Parameter reference,...

Depending on what version of dialogflow-fulfillment you are using, I'm talking about 0.6.1 here. The syntax for setting context is:
agent.context.set({
name: "ctx_name",
lifespan: 5,
parameters: {'cid': id},
});

Related

How to pass one more parameter other than option from the intent showing the carousel to the intent handling the carousel?

I am using an intent to first present the carousel to the user.
When the user clicks on one of the options in the carousel, in the handler intent I get the key of the carousel item that the user selected.
Example of carousel intent,
app.intent('search', async (conv,params) => {
conv.ask(`Choose one item`,new Carousel({
title :`Search results`,
items : carouselItems,
}));
});
Example of the handler intent,
app.intent('handle_carousel', async (conv,params,option) => {
const key = parseInt(option);
});
However, along with the key of the option selected I also want to pass another integer from the carousel intent to the handler intent.
This other integer is different for each option. You can think of the other integer as an ID, it's unique for each option.
How can I achieve that?
You have a few approaches for passing additional data that should be associated with each key.
The first is, as you note in your answer, storing that mapping in a table that is stored as part of session data (either using conv.data or a Dialogflow context).
Another is to encode that data as part of the key that you include with each option, and then decode the key when you get it back.
So, for example, you could make the key a result of an encode function like
function encodeOptionKey( key, otherValue ){
return `${key}:${otherValue}`
}
and then decode it with a function such as
function decodeOptionKey( option ){
const [key,otherValue] = option.split(':');
return {
key,
otherValue
}
}
and call this from your handler with something like
app.intent('handle_carousel', async (conv,params,option) => {
const {key, otherValue} = decodeOptionKey( option );
// ...
});
I created a map of the keys of various carousel options and the corresponding parameter I wanted to pass, saved that map in conv.data.store, which is the conversation storage provided by actions-on-google. Then I used that map to get the parameter from the carousel key that was being passed to the handler intent.
For example in the carousel intent :
let map = {
keyofcarousel : option,
other_parameter : otherparam,
};
conv.data.store = map;
Then call conv.data.store in the handler intent.

Alexa voice with Jovo Framework: Is this possible to get IntentName by utterance? How can I decide which intent to jump to based on user utterance?

I have built an Alexa skill with the following flow:
LAUNCH -> AccountLinkingIntent -> CampaignIntent
In AccountLinkingIntent, presently I am routing to CampaignIntent if Account is already linked.
Up to this everything is working fine. Now I have to add another Intent ActiveContactIntent so that the flow becomes:
LAUNCH -> AccountLinkingIntent -> CampaignIntent / ActiveContactIntent
i.e, From AccountLinking I need to decide which Intent to route to.
The invocation goes like this (CampaignIntent):
Alexa, ask <invocation_name> to get my latest campaign result
OR (ActiveContactIntent)
Alexa, ask <invocation_name> who is my most active contact
Based on the utterance, I need to tell Alexa where to go. So far I have the following in AccountLinkingIntent
...
return this.toIntent("CampaignIntent");
...
But now I need to decide the same as this:
...
if( ... ) {
return this.toIntent("CampaignIntent");
} else {
return this.toIntent("ActiveContactIntent");
}
...
Is there any way to get the IntentName by the utterance so that I can check by the same such as:
...
if( intent_name_by_utterance === "CampaignIntent" ) {
return this.toIntent("CampaignIntent");
} else {
return this.toIntent("ActiveContactIntent");
}
...
Or probably, if it is possible to get intent_name_by_utterance I may also pass the value as the argument of toIntent method if it is allowed to pass a variable!
return this.toIntent(intent_name_by_utterance);
UPDATE:
I have tried the following to see whether the intent name is being returned:
LAUNCH() {
return this.toIntent("LinkAccountIntent");
},
async LinkAccountIntent() {
const intent_name = this.$request.getIntentName();
this.tell(Current intent is: ${intent_name});
},
Invoked the skill in following two fashions:
Alexa, ask <invocation-name> to give me my latest campaign results
Alexa, ask <invocation-name> who is my most active contact
give me my latest campaign results AND who is my most active contact are the utterances for respective intents.
I am using Alexa Test console for testing. In both cases, I was expecting the name of the intent (this.$request.getIntentName()), ending up with
Hmm, I don't know that one.
My intention is to call an intent by its utterance directly by waking up the skill using its invocation name.
Any suggestion?
I think you should treat both intends separately and not through the launch, because for example in the case
"Alexa, ask who is my most active contact"
Alexa skips the launch and jumps directly to resolve the intend ActiveContactIntent.
So just as you have the LAUNCH(){} function you must also have CampaignIntent(){} and ActiveContactIntent(){}.
That way you will avoid Alexa answering
Hmm, I don't know that one.
To verify that the user has already linked his account, you would have to enter a code like the one below:
if (!this.$request.getAccessToken()) {
this.$alexaSkill.showAccountLinkingCard();
this.tell('Please link you Account');
} else {
//code for your respective action
}
I recommend you to check the documentation about "routing with jovo" to have a little more clarity about this topic. You can review it at the following link:
https://www.jovo.tech/docs/routing

Dialogflow node.js fulfilment with multiple entities

I hope someone can help me with this.
I have built an application that uses node.js to fulfil my intent in dialogflow.
For example, I have an intent with one required action:
It goes to my fulfilment:
// Handle the Dialogflow intent named 'Default Welcome Intent'.
app.intent(DEFAULT_INTENT, (conv, params) => {
let categoryId = params.category;
let options = {
'method': 'GET',
'url': apiUrl + 'questions/categories/' + categoryId + '/scenario',
'json': true
};
return request(options).then(response => {
// TODO: What happens if there is more than one question?
let question = response[0];
conv.ask(question.text);
}, error => {
conv.ask('There was an issue with the request: ' + options.url);
});
});
As you can see, this asks a question based on the category sent to the fulfilment.
The problem I have is that the response I want from the user is different for each question.
Once they have responded, it will also have a fulfilment that will ask another question.
Is it possible to do it this way and if so, can someone give me an example of how? If not, can someone help me work out what the alternative is?
The approach you're using makes sense. The key thing to remember is that Intents capture what the user says, not how you handle what they say. You can influence which Intent gets triggered by setting an Input Context, and making sure you have previously set an Output Context for it.
One possible approach would be that for each question you're asking, you set a corresponding Output Context for that question. You can then have one or more Intents that take this as the Input Context. These are otherwise regular Intents, so you'd handle them normally. You might want to clear the context (by setting its lifespan to 0) after it matches, so you don't accidentally match it later.
For example, if your question contains, not only the text of the question, but also the context name of the question, the code might look something like this:
conv.ask( question.text );
conv.contexts.set( question.contextName, 5 );
Let's say that the question object looks something like this
{
text: "What is your favorite color?",
contextName: "color-favorite"
}
You might have a Dialogflow Intent that handles this that looks something like this
Note that the Output Context has explicitly set it to 0, which will remove it. You can also do this in your fulfillment code with something like
conv.contexts.delete( 'color-favorite' );

How to use a parameter from a follow-up intent within the Inline Editor in DialogFlow

I'm using the DialogFlow Inline Editor and I need to be able to provide different responses based on the specific values of a parameter within a follow up intent.
Via the UI, I am able to build a follow up intent and refer to an entity in the previous intent, but I can't set up logic based on specific values of that entity, which is why i need to be able to do this via the Online Editor.
Can someone show me an example of how to use an entity within a follow up intent via the Online Editor?
So far, I am able to output a context within a function (function 1 below), but i am unable to use an entity that was provided by the user within function 1 in a function 2 that uses a follow-up intent.
//function 1:
function fruit(agent) {
const fruit = agent.parameters.FruitValue
agent.add(`what fruit do you like`)
agent.setContext({ name: 'cherries-followup', lifespan: 2, parameters: {FruitValue: fruit}})}
//function 2:
function details(agent) {
const context = agent.getContext('cherries-followup');
const fruit = context.parameters.FruitValue;
agent.add(`Good to know you like ${fruit}!`);
}
intentMap.set('CherriesIntent', fruit);
intentMap.set('CherriesDetailIntent', details);
Picture of what I did via the UI which i would like to replicate in the Online Editor
Update (Solved):
I have now rewritten the code to be in line with the DF post (so that it is easier to debug: https://dialogflow.com/docs/getting-started/integrate-services)
function languageHandler(agent) {
const language_spoken = agent.parameters.language;
{agent.add(`Wow! Where did you learn ${language_spoken} and for how long?`);
agent.setContext({
name: 'languages-followup',
lifespan: 2,
parameters:{languages: language_spoken}
});
}
}
function languageCustomHandler(agent) {
const context = agent.getContext('languages-followup');
//const allContexts = agent.contexts; // [{ name: 'languages-followup', ...}]
const language_spoken = context.parameters.language;
const country = agent.parameters.country;
const duration = agent.parameters.duration;
agent.add(`Wow! So cool you learned ${language_spoken} in ${country} for about ${duration.amount} ${duration.unit}!`);
}
intentMap.set('Language', languageHandler);
intentMap.set('Language - custom', languageCustomHandler);
It works now, after making sure that the duration parameter value is either set to duration.original in the UI or after adding .amount and .unit to duration (otherwise you will get: "Wow! So cool you learned German in China for about [object Object]!" instead of "Wow! So cool you learned German in China for about 1 mo!
PARAMETER VALUE
country China
duration {"amount":1,"unit":"mo"}

How can i pass input argument when writing loopback-testing

I am writing a test driven development for my strongloop API code with the help of loopback-testing .
Here they do not have any detailed document on this, so i am stuck with case of argument passing with the API call
Example i have a below case,
Method : PUT
URL : /api/admin/vineyard/<vineyard_id>
i need to pass the below arguments with this URL
1. 'vineyard_id' is a id of vine, it should be an integer .
2. in header = 'token'
3. in body = '{'name':'tastyWine','price':200}'
How can i pass these three arguments with this API ?
I can easily handle ,if there is only two types of arguments
Example :
Method : POST
`/api/user/members/<test_username>/auth'`
arguments : test_username and password
I can handle this like this ,
lt.describe.whenCalledRemotely('POST',
'/api/user/members/'+test_username+'/auth', {
'password': test_passwords
},
But how can i handle the above case , Many thanks for your answers for this example.
I'm not entirely sure what your specific problem is, but I will attempt to walk through everything you should need.
I am assuming you are using the predefined prototype.updateAttributes() method for your model as described here.
Next assumption is that you want to use the built-in authentication and authorization to allow the user to call this method. Given that assumption, you need something like this in your test code:
var vineyard_id = 123; //the id of the test item you want to change
var testUser = {email: 'test#test.com',password: 'test'};
lt.describe.whenCalledByUser(testUser, 'PUT', '/api/admin/vineyard/'+vineyard_id,
{
'name':'tastyWine',
'price':200
},
function () {
it('should update the record and return ok', function() {
assert.equal(this.res.statusCode, 200);
});
}
);
If you are using the out-of-the-box user model, you should be fine, but if you extended the model as is commonly done, you may need something like this early on in your test file:
lt.beforeEach.withUserModel('user');
Also, be aware of a few (currently incomplete) updates to will allow for better handling of built-in model extensions: Suggestions #56, Add support for non-default models #57, and givenLoggedInUser() function throws error #59.

Resources