我有一个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

最佳答案

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

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

在这里,您仅导入Card and Recommendationation类,还应该添加Payload类:
const {Card, Suggestion, Payload} = require('dialogflow-fulfillment');

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

08-28 07:56