如何通过 Fulfillment 为 Dialogflow Messenger 集成添加建议芯片?

How to add Suggestion Chips through Fulfillment for the Dialogflow Messenger integration?

所以我在网站中嵌入了 Dialogflow Messenger,并希望添加一些建议信息块。通过 Custom Payload 响应类型很容易,它们显示得很好。 但是如何通过 fulfillment 添加它们呢?

我目前有一个自定义的 webhook 设置,我的想法是这样的:

if (x) {
  agent.add('blablabla');
  agent.add(new Suggestion('One');
} else {
  agent.add('blablabla');
  agent.add(new Suggestion('Two');
}

new Suggestion 不起作用,那么还有另一种方法吗? 我在想这样的事情:

agent.add(new Payload(
  "richContent": [
    [
      {
        "options": [
          {
            "text": "One"
          },
          {
            "text": "Two"
          }
        ],
        "type": "chips"
      }
    ]
  ]));

实质上是尝试将自定义负载直接插入响应 JSON,如果这有意义的话。但是,是的,不知道该怎么做。有人知道怎么做吗?

我不清楚new Suggestion() 不起作用 的确切含义。您的意思是 Dialogflow Messenger 中不显示建议信息块?它们是否显示在 Dialogflow 本身中?

分享几点:

  • 据我所知,结构 agent.add(new Suggestion(“One”)); 应该有效。我尝试了一个简单的示例,它在 Dialogflow UI 中运行良好,代码为:
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
 
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));
  
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  function welcome(agent){ 
    agent.add("What is your favorite animal?");
    agent.add(new Suggestion("Dog"));
    agent.add(new Suggestion("Cat"));
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }

  agent.handleRequest(intentMap);
});
  • 如果即使在 Dialogflow 中也没有呈现建议芯片 UI 我建议尝试使用之前的代码来丢弃 Dialogflow 设置的任何潜在问题。您可能需要升级一些依赖项,例如"dialogflow-fulfillment": "^0.6.1".

  • 一些集成,例如 Google Assistant 使用来自 actions-on-googleSuggestions 库。例如,参见 official Google Assistant code example. You may try to follow a similar behavior if it fits your use case although I do not think it is the case. As a reference you can check this github 问题。