在Dialogflow实现中为所需参数分配默认值 [英] Assigning a default value to a required parameter in Dialogflow Fulfillment

查看:129
本文介绍了在Dialogflow实现中为所需参数分配默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR;(标题)如何通过Dialoflow Fulfillment为所需的参数分配默认值?

TLDR; (title) How to assign a default value to a required parameter via Dialoflow Fulfillment?

长期提问:

我有这两个必需的参数.user_id和user_pin.

I have these 2 required parameters. user_id and user_pin.

我利用Fulfillment webhook调用来填充插槽,除此行为外,一切似乎都运行良好:

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

在对数据库进行API调用以确认user_id和user_pin是否匹配以及不匹配之后,Dialogflow chatbot响应错误的用户Pin,请提供正确的Pin."

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_pin,但是Dialogflow触发了插槽填充,并再次询问了user_id.-请提供用户ID".

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

我不希望它再次询问user_id.为了解决这个问题,在提供了错误的user_pin之后,我创建了一个上下文:"incorrectPinContext",其参数为{user_pin:user_pin}.然后,我将默认值设置为:

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

user_id = #incorrectPinContext.user_id

这可以解决此问题,因为Dialogflow足够聪明,可以知道是否已经提供了user_id,并且不再要求再次提供user_id.但是我完成的所有默认值分配仅通过Dialogflow的控制台/UX完成.我已经浏览了Google的文档,但似乎找不到有关该操作的参考.

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.

这使我想到一个问题:如何通过Dialoflow Fulfillment为所需的参数分配默认值?

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

推荐答案

简短答案,请使用

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.

假设您正在对话中尝试提取名称和ID.这样做是为了模仿您所需的两个参数.

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.

然后,实现结构必须像

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:

  • 你好!->你能告诉我你的名字和身份证吗?
  • 好的,我叫Alan-> Alan,你好吗?
  • 77678->您的名字叫艾伦,您的身份证号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.

请注意,如果在两个上下文中使用相同的变量名(例如id),则该变量名会随着新值而改变,因此将不被记住.这就是为什么我使用"cid".

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

这篇关于在Dialogflow实现中为所需参数分配默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆