如何在Dialogflow Messenger上使用建议芯片等丰富的响应消息?

人气:702 发布:2022-10-16 标签: javascript node.js button dialogflow-es dialogflow-es-fulfillment

问题描述

大家好,我正在使用WebHook作为后端,使用Dialogflow作为前端,我正在使用node js中的Dialogflow实施库,我知道如何利用丰富的响应消息,如使用Dialogflow前端的按钮,但如何从WebHook代码使用它,可能使用一些JSON?我想用建议筹码 目前,我知道如何使用这张卡见下面的代码,现在我想用建议芯片回应一下怎么做?

//agent.add(new Card({
    //title: `RDF Graph Visualization`,
     //buttonText: 'open website',
     //buttonUrl: 'https://xxherokuapp.com/visualize/' + graphId
       //})
    // )    

推荐答案

我以WebHook的身份尝试了以下代码,并在Dialogflow Messenger上进行了测试。响应包含带有链接的建议筹码。

Index.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);
});

Package.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一起使用的详细信息,请参阅document。

762