我有一些问题。
我想用这个程序制作一个机器人。
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
我:你好
Bot : 欢迎来到我的代理!
机器人:你叫什么名字?
我:史密斯
机器人:史密斯
机器人:1
机器人:2
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
我将 intents 设为“hello”,其参数为“name”。
但是当它运行时,它会按照下面的顺序工作。
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
我:你好
机器人:你叫什么名字?
我:史密斯
Bot : 欢迎来到我的代理!
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
这与我的意图完全不同。
你能推荐我制裁剪品的正确方法吗?
非常感谢。
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, 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 hello(agent) {
agent.add(`Welcome to my agent!`);
let city = request.body.queryResult.parameters['name'];
agent.add(`${city}`);
information().then((a) => {
agent.add(JSON.stringify(a));
});
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
let intentMap = new Map();
intentMap.set('hello', hello);
intentMap.set('Default Fallback Intent', fallback);
agent.handleRequest(intentMap);
});
function information(){
return new Promise(function (resolve, reject) {
var a = ['1','2'];
resolve(a);
});
}
最佳答案
由于您已将 name
设置为必需参数,Dialogflow 将等待调用 fulfillment webhook,直到所有必需参数都已填充(除非您有开关以使用 fulfillment 进行插槽填充,你不知道)。
那么发生的事情是:
- Intent 被触发是因为“hello”匹配了 Intent
- 缺少参数
name
,因此 Dialogflow 会提示输入它 - 用户回复并填写
name
- Dialogflow 将信息发送到您的 webhook
- 您的代码添加了“欢迎”消息
这就是它变得棘手的地方。并非所有客户端都支持回复中的多条消息,或者支持那么多,因此额外的 add()
调用可能会被忽略,具体取决于您正在测试的客户端。一般来说 - 您应该只添加不同类型的响应(文本、卡片等)以实现跨平台兼容。
关于javascript - 如何使用 webhook 在 dialogflow 中创建正确的流程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53795822/