TLDR; (title) How to assign a default value to a required parameter via Dialoflow Fulfillment?
Long question:
I have these 2 required parameters. user_id and use
Short answer, use a structure like
agent.setContext({
name: 'question2',
lifespan: 5,
parameters: {'name': name,'id': id},
});
const context = agent.getContext('question2');
const name = context.parameters.name
const id = context.parameters.id;
Long answer, let me show it to you with an example.
Assume you have a conversation where you are trying to extract the name and the id. This is done to mimic your two parameters required.
Then, the fulfillment structure has to be like
function welcome(agent){
agent.add(`Can you tell me your name and id?`);
agent.setContext({
name: 'question',
lifespan: 3,
});
}
function answers(agent){
agent.getContext('question');
const name = agent.parameters['given-name'];
const id = agent.parameters.id;
if (name && id) {
agent.add(`So your name is ${name} and your id ${id}`);
} else if (name) {
agent.add(`Hello ${name}, what was your id?`);
} else if (id) {
agent.add(`So your id is ${id} but what is your name?`);
} else {
agent.add(`Can you tell me your name and id?`);
}
agent.setContext({
name: 'question2',
lifespan: 5,
parameters: {'name': name,'cid': id},
});
}
function answers2(agent){
const cont = agent.getContext('question2');
const cname = cont.parameters.name;
const cid = cont.parameters.cid;
const nname = agent.parameters['given-name'];
const nid = agent.parameters.id;
if (cname){
if (cid){
agent.add(`So your name is ${cname} and your id ${cid}`);
} else if (nid){
agent.add(`So your name is ${cname} and your id ${nid}`);
} else {
agent.add(`Sorry still I do not know what is your id`);
}
} else if (cid){
if (cname){
agent.add(`So your name is ${cname} and your id ${cid}`);
} else if (nname){
agent.add(`So your name is ${nname} and your id ${cid}`);
} else {
agent.add(`Sorry still I do not know what is your name`);
}
} else {
agent.add(`I still need both your name and your id`);
}
}
Then, you can perform the following conversation:
You will need to modify the functions and the structure to fit your needs but I think this example is illustrative of how to pass values using context in fulfillment.
Notice that if you use the same variable name in two contexts, for example id, it would get changed with the new value and thus would not be remembered. That is why I used "cid".