Интеграция расширенного сообщения в полнофункциональный диалог - PullRequest
3 голосов
/ 28 апреля 2020

У меня есть код выполнения для чат-бота 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 intent. У меня есть одно предупреждение:

Payload is not defined

1 Ответ

0 голосов
/ 01 мая 2020

В верхней части кода у вас есть следующее утверждение:

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

Здесь вы импортируете только класс Card и Offer, вам также следует добавить класс Payload:

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

После того, как вы это сделаете, предупреждение должно исчезнуть.

...