【问题标题】:Integrate rich message into Dialogflow fullfilment将丰富的消息集成到 Dialogflow 实现中
【发布时间】:2020-08-12 14:35:26
【问题描述】:

我有一个 Dialogflow 聊天机器人的实现代码,我希望能够发送(以及稍后接收和分析)丰富的消息。由于我对 javascript 编程有点陌生,所以我想知道如何使用实现代码发送丰富的消息。我试着为function answer1Handler做一个。

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
var answers = [];

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(`Welcome to my agent!`);
  }

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

  function answer1Handler(agent){
    agent.add('Intent answer1 called');
    const answer = agent.parameters.number;
    answers.push(answer);

    const payload = {
      "slack": {
        "attachments": [
          {
            "blocks": [
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "*Compared to most people in the UK :uk:, how optimistic are you about life?* Poll by <fakeLink.toUser.com|Mihailo>"
                }
              },
              {
                "type": "divider"
              },
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": ":cold_sweat: *I’m feeling more negative about everything; less free and more pessimistics*"
                },
                "accessory": {
                  "value": "1",
                  "type": "button",
                  "text": {
                    "emoji": true,
                    "type": "plain_text",
                    "text": "Write 1"
                  }
                },
                "block_id": "1"
              },
              {
                "accessory": {
                  "value": "2",
                  "type": "button",
                  "text": {
                    "emoji": true,
                    "type": "plain_text",
                    "text": "Write 2"
                  }
                },
                "block_id": "2",
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": ":expressionless: *More positive about the quality of life, but worrying more about the NHS and the security situation*"
                }
              }
            ]
          }
        ]
      }
    }

    agent.add(
        new Payload(agent.UNSPECIFIED, payload, {rawPayload: true, sendAsMessage: true})
    );
  }

  intentMap.set('RhymingWord', rhymingWordHandler);
  intentMap.set('answer1', answer1Handler);

  agent.handleRequest(intentMap);
});

但是当我调用 answer1 意图时它不起作用。我有一个警告:

Payload is not defined

【问题讨论】:

    标签: javascript google-cloud-platform dialogflow-es


    【解决方案1】:

    在您的代码顶部有以下语句:

    const {Card, Suggestion} = require('dialogflow-fulfillment');
    

    这里你只导入 Card 和 Suggestion 类,你还应该添加 Payload 类:

    const {Card, Suggestion, Payload} = require('dialogflow-fulfillment');
    

    完成此操作后,警告应该会消失。

    【讨论】:

    • 谢谢,警告确实消失了。不幸的是,它没有在 Slack 中集成丰富的消息。部署后它仍然是默认的
    • 我应该让您知道,您使用的库自 2018 年以来不再维护。众所周知,Payload 类无法按预期工作。我在 FB Messenger 集成中遇到了同样的问题。我最终改用了 fb api。我不知道 slack 是否有发送消息的 API,但如果有,我强烈建议您使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    相关资源
    最近更新 更多