首页 > 解决方案 > 如何在 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
       //})
    // )    

标签: javascriptnode.jsbuttondialogflow-esdialogflow-es-fulfillment

解决方案


我已经尝试使用 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 中使用建议芯片的更多信息。


推荐阅读