首页 > 解决方案 > Google dialogflow agent.add(new Suggestion()) 无法在 dialogflow Messenger 上运行

问题描述

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

'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-esdialogflow-es-fulfillment

解决方案


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

  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 代码示例,并且 Intent 将使用 Dialogflow Messenger 中的芯片进行响应。

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

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

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

推荐阅读