javascript - 如何在 Dialogflow Messenger 上使用丰富的响应消息,如建议芯片?
问题描述
大家好,我使用 webhook 作为后端,使用 Dialogflow 作为前端 我在节点 js 中使用 Dialogflow-fulfillment 库使用一些 JSON?我现在想使用建议芯片,我知道如何使用卡现在看下面的代码我想使用建议芯片响应怎么做?
//agent.add(new Card({
//title: `RDF Graph Visualization`,
//buttonText: 'open website',
//buttonUrl: 'https://xxherokuapp.com/visualize/' + graphId
//})
// )
解决方案
我已经尝试使用 Fulfillment 作为 Webhook 的以下代码,并在 Dialogflow Messenger 上对其进行了测试。响应包含带有链接的建议芯片。
索引.js:
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const { Payload } = require("dialogflow-fulfillment");
process.env.DEBUG = 'dialogflow:debug';
exports.myfunction = 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 to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function pet(agent){
agent.add(`which one u want`);
const payload = {
"richContent":[
[
{
"type":"chips",
"options":[
{
"text":"Dog",
"link" : "https://en.wikipedia.org/wiki/Dog"
},
{
"text":"Cat",
"link":"https://en.wikipedia.org/wiki/Cat"
},
{
"text":"Rabbit",
"link" : "https://en.wikipedia.org/wiki/Rabbit"
}
]
}
]
]
};
agent.add(new Payload(agent.UNSPECIFIED, payload, {rawPayload: true, sendAsMessage: true}));
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('mypet',pet);
agent.handleRequest(intentMap);
});
包.json:
"name": "myfunction",
"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 Messenger 中使用建议芯片的更多信息。
推荐阅读
- ios - iOS 中的 VXI-11 协议
- c++ - 在 C++ 中使用三元运算符的问题
- android - 如何在颤动中突出显示 ListView 的顶部项目
- asp.net-web-api - 如何从 Swashbuckle for asp.net 中包含的 Swagger-UI 中删除应用程序/json
- c# - 如何在 VS2019(版本 16.8.0)中将“移动应用程序(Xamarin.Forms)”项目定位到 .net 5.0?
- javascript - 在 JavaScript 中解析数字中的字母的函数
- python - 如何排列数字在熊猫中创建新列
- c++ - StringPiece 是否与字符串右值引用冲突?
- reactjs - 将路由器与其他查询参数反应相同的路径
- python-3.x - 将 3d 列表转换为同一索引上的 pandas 单个数据帧