繁体   English   中英

如何在 dialogflow webhook 中创建上下文并保存参数

[英]How to create context and save parameters in dialogflow webhook

我想创建一个允许用户发送 2 个参数的意图,但是,似乎有些错误我无法理解。 它在 dialogflow 控制台中运行良好

这是我在控制台中的 dialogflow 代理:

在此处输入图像描述

这是我的 node.js function

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);
      }
}

聊天机器人的结果

在此处输入图像描述

我将衷心感谢您的帮助

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

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

  • 创建一个意图测试
  • 将训练短语添加到意图中,例如“first”、“second”,并用@sys.any标记它们
  • 在“操作”和“参数”字段中,定义提示,例如“首先是什么”和“第二个是什么”。

在此处输入图像描述

  • 您需要为此意图启用 fulfillment。

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

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

索引.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"
}
}

output: 在此处输入图像描述

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

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

在此处输入图像描述

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

您可以参考以下网站截图: 在此处输入图像描述

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM