根据您的要求,我尝试复制该场景。我从控制台和使用 Dialogflow Fulfillment Webhook 都尝试了相同的操作,在这两种情况下我都得到了预期的输出。
您可以参考下面提到的步骤:
- 创建一个意图测试
- 向意图添加训练短语,例如“first”、“second”,并用 @sys.any 标记它们
- 在 Action and Parameter 字段中定义提示,例如 “什么是第一个”用于第一个,“什么是第二个”用于第二个。
根据您提供的代码,您使用了 “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 代码。