javascript - 带有所需参数的 Dialogflow 插槽填充问题
问题描述
我正在使用 Dialogflow 为零食机创建一个机器人,但我遇到了问题。我有两种类型的实体:
simple-element
:没有类型的对象(巧克力、薯条)。complex-element
:具有多种类型的元素,例如水(静止或苏打水)和咖啡(lungo、macchiato 或 Americano)。
我为这两个实体做了两个不同的意图,order.simple.element
并且 order.complex.element
.
在 order.complex.element
我设置为所需参数元素的数量和元素类型本身,我想也设置复杂元素类型,但只有当复杂元素匹配时,这意味着问用户你想要水? 还是火花?只有当实体是水时。
我也尝试了 webhook 调用,但它不起作用。这是我使用nodejs的代码:
const functions = require('firebase-functions');
const {WebhookClient, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug';
exports.dialogflowFirebaseFulfillment = 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 welcome (agent) {
agent.add(`Welcome! What do you want?`);
}
function fallback (agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function orderComplexElement(agent) {
var complex_el = agent.parameters['complex-element'].toLowerCase();
if (complex_el === 'water') {
agent.add('How do you want it? Still or sparkling?');
agent.add(new Suggestion('still'));
agent.add(new Suggestion('sparkling'));
} else if (complex_element === 'coffee') {
agent.add('How do you want it? You can choose from americano, lungo and macchiato.');
}
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('order.complex.element.try', orderComplexElement);
agent.handleRequest(intentMap);
});
如何根据类型根据需要制作复杂元素complex-element
类型?
解决方案
您需要捕获用户对 water-type 和 coffee-type 的响应,然后将这些参数保存在您的上下文中。例如:
var water-type = request.body.queryResult.queryText; // this captures the user input
agent.context.set({ // saves the parameter to a context.
name:'sessionvars', //you can use this context to capture all variables
lifespan: 50, //context will be available for 50 conversational turns
parameters: {
'water-type': water-type
}
});
推荐阅读
- html - 使用 dart 而不是 JavaScript 来操作 DOM,你需要一个 pubspec 文件吗?
- python - 连接列的值并再次放回同一行
- javascript - 无法在反应js中正确地将表单绑定到嵌套数组
- python - 如何在字符串列表中搜索序列
- postgresql - Flutter 异常:图像数据无效
- laravel - SQLSTATE [23000]:违反完整性约束:1052 where 子句中的列 'tenant_id' 不明确
- node.js - 从 MongodB 中的数组中检索最后 40 个或某些特定数量的元素
- github - 如何使用 Cargo 在 Github Actions 中下载另一个私有存储库?
- mysql - 在 From_UnixTime 中将 1 天添加到正常日期
- sql - 我想获取每个标签的条件并在 sql 中有这样的表