0

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 user_pin.

I utilized Fulfillment webhook call for slot-filling and everything seems to work well except this behavior:

After an API call to the database to verify if user_id and user_pin matches and they do not match, Dialogflow chatbot responds with "Incorrect user pin please provide the correct pin."

User then provides another user_pin but Dialogflow triggers the slot-filling and asks for the user_id again. - "Please provide a user ID".

I don't want it to ask for the user_id again. So to fix that, after an incorrect user_pin is provided, I created a context: "incorrectPinContext" with parameters {user_pin:user_pin}. I then set the default values to:

user_id = #incorrectPinContext.user_id

This fixes the problem as Dialogflow is smart enough to know if user_id is already provided or not and no longer asks again for user_id. But all of the default value assignment I've done was only through the console / UX of Dialogflow not in fulfillment. I've browsed through Google's documentations but cant seem a reference on how to do that.

So that brings me to the question: How to assign a default value to a required parameter via Dialoflow Fulfillment?

Sairaj Sawant
  • 1,842
  • 1
  • 12
  • 16
brian3415
  • 65
  • 1
  • 7

1 Answers1

0

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:

  • Hello!-> Can you tell me your name and id?
  • Sure, my name is Alan -> Hello Alan, what was your id?
  • 77678 -> So your name is Alan and your id 77678

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".

aemon4
  • 1,037
  • 6
  • 11