【问题标题】:How to create context and save parameters in dialogflow webhook如何在 dialogflow webhook 中创建上下文并保存参数
【发布时间】:2021-11-21 13:53:51
【问题描述】:

我想创建一个允许用户发送 2 个参数的意图,但是,我想不出某些错误。它在对话框流控制台中运行良好

这是我在控制台中的对话流代理:

这是我的节点 js 函数

function test(agent) {

      first = agent.parameters['first'];
      console.log("first");//nothing is returned so I think it does not enter the function 
      second = agent.parameters['second'];

      agent.context.set({
              "name": 'test',
              "lifespan": 2,
              "parameters": {
                "first":first,
                "second":second
              }
      });
      if (first != "") {
        agent.add("the 1st text is: " + first);
      } if (second != "") {
        agent.add("the 2nd text is" + second);
      }
}

聊天机器人的结果

非常感谢您的帮助

【问题讨论】:

    标签: javascript node.js dialogflow-es dialogflow-es-fulfillment


    【解决方案1】:

    根据您的要求,我尝试复制该场景。我从控制台和使用 Dialogflow Fulfillment Webhook 都尝试了相同的操作,在这两种情况下我都得到了预期的输出。

    您可以参考下面提到的步骤:

    • 创建一个意图测试
    • 向意图添加训练短语,例如“first”、“second”,并用 @sys.any 标记它们
    • Action and Parameter 字段中定义提示,例如 “什么是第一个”用于第一个,“什么是第二个”用于第二个。

    • 您需要为此 Intent 启用实现。

    根据您提供的代码,您使用了 “agent.context.set” 字段。由于您没有在 Intent 中使用任何 输入和输出上下文,因此无需在 webhook 代码中定义它们。

    您可以参考下面的 Node.js 代码。我已将 Node.js 10 客户端库 用于对话流。

    Index.js:

    'use strict';
    const functions = require('firebase-functions');
    const {WebhookClient} = require('dialogflow-fulfillment');
    const {Card, Suggestion} = require('dialogflow-fulfillment');
    process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
    exports.slot = functions.https.onRequest((request, response) => {
     const agent = new WebhookClient({ request, response });
     console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
     console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
    
     function going(agent){
     const value1=agent.parameters[`first1`];
     const value2=agent.parameters[`second2`];
      agent.add(`we received ` +value1+ ` in first field and ` +value2 + ` in second field as per your input`)
     }  
    
     let intentMap = new Map();
     intentMap.set('test',going);
      agent.handleRequest(intentMap);
    });
    

    Package.json:

    {
     "name" : "slot",
    "description": "This is the webhook",
    "version": "0.0.1",
    "private": true,
    "license": "Apache Version 2.0",
    "author": "Google Inc.",
    "engines": {
      "node": "10"
    },
     "dependencies": {
      "actions-on-google": "^2.2.0",
      "firebase-admin": "^5.13.1",
      "firebase-functions": "^2.0.2",
      "dialogflow": "^0.6.0",
      "dialogflow-fulfillment": "^0.6.1"
    }
    }
    
    

    输出:

    当您在 Dialogflow 中使用 Webhook 时,要使用 Webhook 从 Intent 中获取参数,我们使用 agent.parameters[`parameter name`] 函数。该参数需要存在于 Dialogflow 控制台中,否则 Webhook 无法获取参数值以继续对话流。

    在下面的屏幕截图中,您可以使用 agent.parameters[`parameter name`] 函数检查参数值是从 webhook 中获取的。

    我创建了一个示例网站,并在那里部署了 Dialogflow Messenger,它按预期工作。

    您可以参考以下网站截图:

    您能否尝试使用 webhook Node.js 代码以及我在解决方案中提供的 package.json 文件。

    如果这对您不起作用,请提供您的代理 Zip 文件以及完整的 Webhook 代码。

    【讨论】:

    • 感谢您的回复,dialogflow messenger 演示工作正常。但是,在我的网站上使用它时,它仍然显示相同的响应,所以我做了一些调试,我发现如果我在 dialogflow 控制台中添加参数,它不会进入测试函数(),反之亦然,如果我删除dialogflow控制台中的参数,它进入测试函数()
    • 我已经删除了设置的上下文,并且 dialogflow 控制台内的 webhook 已启用,我不知道为什么它要么只显示 node js 功能,要么只显示 dialogflow 控制台。另外,我发现第一个值总是在更新,而第二个值没有。
    • 嗨@Mounir 我已经更新了答案。
    猜你喜欢
    • 2018-11-15
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2019-03-01
    • 2020-04-14
    • 2022-01-13
    相关资源
    最近更新 更多