【问题标题】:How to use rich response messages like suggestion chips on Dialogflow messenger?如何在 Dialogflow Messenger 上使用丰富的响应消息,如建议芯片?
【发布时间】:2021-10-08 15:32:26
【问题描述】:

大家好,我使用 webhook 作为后端,使用 Dialogflow 作为前端我在节点 js 中使用 Dialogflow-fulfillment 库我知道如何使用丰富的响应消息,例如使用 Dialogflow 前端的按钮,但是我如何从webhook 代码可能使用一些 JSON?我想使用建议筹码 目前,我知道如何使用该卡,请参阅下面的代码现在我想使用建议芯片响应如何做到这一点?

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

【问题讨论】:

    标签: javascript node.js button dialogflow-es dialogflow-es-fulfillment


    【解决方案1】:

    我已尝试使用 Fulfillment 作为 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);
    });
    

    包.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"
     }
    }
    

    输出: document 中提供了有关通过 Dialogflow Messenger 使用建议芯片的更多信息。

    【讨论】:

    • 这对我也有用!!!谢谢!不要忘记const { Payload } = require("dialogflow-fulfillment");应该存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2023-03-30
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多