【问题标题】:Google dialogflow agent.add(new Suggestion()) not working on dialogflow messengerGoogle dialogflow agent.add(new Suggestion()) 在 dialogflow messenger 上不起作用
【发布时间】:2021-03-09 14:43:36
【问题描述】:

我正在尝试使用实现(内联编辑器)添加建议芯片的非常基本的代码,但它不起作用。 以下是我尝试使用的代码:

'use strict';

const functions = require('firebase-functions');
const { WebhookClient } = require('dialogflow-fulfillment');
const { Card, Suggestion } = require('dialogflow-fulfillment');
const { Suggestions } = require('actions-on-google');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

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 welcome(agent) {
    agent.add(new Suggestion(`Quick Reply`));
    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 yourFunctionHandler(agent) {
    agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
    //the following suggestion chips are not working 
    agent.add(new Suggestion(`Quick Reply`));
    agent.add(new Suggestion(`Suggestion`));
  }
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);

  intentMap.set('car', yourFunctionHandler);

  agent.handleRequest(intentMap);
});

以下是对话流信使的图像:

【问题讨论】:

    标签: dialogflow-es dialogflow-es-fulfillment


    【解决方案1】:

    您看不到建议提示的原因是 Dialogflow Web Demo 仅用于简单的文本响应。它不支持任何丰富的响应。如果您想测试丰富的响应,您应该尝试将其与其他消息传递应用程序集成,或者在使用 Web 的情况下,您应该创建自己的 Web 小部件。
    以下是来自docs 的更多信息。

    【讨论】:

    • 非常感谢先生的帮助。我正在使用对话流信使。你能通过分享一个示例代码来帮助我,我应该如何使用实现内联编辑器添加丰富的响应?
    • 信使是指 Facebook 信使吗?
    • 没有先生 Dialogflow 信使。
    【解决方案2】:

    您的代码中的 Suggestions Chips 指令似乎是用于 Google 助理集成的。如果您在 Google 助理中测试您的代理很可能会工作(对于 Phone device)。我测试了以下代码,在这种情况下得到了预期的输出:

      function yourFunctionHandler(agent) {
         agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
         agent.add(new Card({
             title: `Title: this is a card title`,
             imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
             text: `This is the body text of a card.  You can even use line\n  breaks and emoji! ?`,
             buttonText: 'This is a button',
             buttonUrl: 'https://assistant.google.com/'
           })
         );
         agent.add(new Suggestion(`Quick Reply`));
         agent.add(new Suggestion(`Suggestion`));
       }
    

    由于您的目标是通过 Dialogflow Messenger 集成使其工作,我可以想到两个选项:

    1. 您可以在 Dialogflow 控制台中指定自定义负载。在您的意图中有一个默认响应部分,当您单击 ADD RESPONSE 时,您将看到选项自定义有效负载。您可以在其中粘贴 Chips code example,意图将通过 Dialogflow Messenger 中的芯片进行响应。

    2. 要使其在 Fulfillment 部分工作,您需要为 rich responses 构建 json 结构,其中有效负载必须是 richContent。我不确定如何在内联编辑器中使用 NodeJS 来返回 json 响应,但我使用 python 构建了自己的 Cloud Function 并将其配置为我的Webhook

    这种方法有效,我遵循的程序发布在here,这是我的功能:

    def entry_function(request):
     
       response_json = jsonify(
           fulfillment_text="This message is from Dialogflow's testing!",
           fulfillment_messages=[
               {
                   "payload": {
                       "richContent": [[{
                           "actionLink": "https://assistant.google.com/",
                           "subtitle": "This is the body text of a card.  You can even use line\n  breaks and emoji! ?",
                           "title": "Title: this is a card title",
                           "type": "info"
                       },
                       {
                          "type": "chips",
                          "options": [
                            {
                              "text": "Chip 1",
                              "image": {
                                "src": {
                                  "rawUrl": "https://example.com/images/logo.png"
                                }
                              },
                              "link": "https://example.com"
                            },
                            {
                              "text": "Chip 2",
                              "image": {
                                "src": {
                                  "rawUrl": "https://example.com/images/logo.png"
                                }
                              },
                              "link": "https://example.com"
                            }
                          ]
                        }]]
                   }
               }
           ]
       )
       return response_json
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 2020-07-29
      • 1970-01-01
      相关资源
      最近更新 更多