Как интегрировать карты Slack Design с полнофункциональным кодом dialogflow? - PullRequest
0 голосов
/ 27 апреля 2020

У меня есть полноценный код диалогового потока, который хорошо работает, но избавляет от моих любимых карт Slack. В основном он задает три вопроса, а затем вычисляет, какие ответы были наиболее частыми:

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);
  }

  function answer2Handler(agent){
    agent.add('Intent answer2 called');
    const answer = agent.parameters.number;
    answers.push(answer);
  }

  function answer3Handler(agent){
    agent.add('Intent answer3 called');
    const answer = agent.parameters.number;
    answers.push(answer);
    agent.add('Here is the mode');
    const mfi = mode(answers);
    agent.add(mfi);
  }

  function mode(arr1){
    var mf = 1; //default maximum frequency
    var m = 0;  //counter
    var item;  //to store item with maximum frequency
    for (var i=0; i<arr1.length; i++)    //select element (current element)
    {
            for (var j=i; j<arr1.length; j++)   //loop through next elements in array to compare calculate frequency of current element
            {
                    if (arr1[i] == arr1[j])    //see if element occurs again in the array
                     m++;   //increment counter if it does
                    if (mf<m)   //compare current items frequency with maximum frequency
                    {
                      mf=m;      //if m>mf store m in mf for upcoming elements
                      item = arr1[i];   // store the current element.
                    }
            }
            m=0;   // make counter 0 for next element.
    }
    return item;
  }

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('answer1', answer1Handler);
  intentMap.set('answer2', answer2Handler);
  intentMap.set('answer3', answer3Handler);

  agent.handleRequest(intentMap);
});

Однако даже они имеют место из полезной нагрузки Slack по умолчанию, которую я создал:

enter image description here

Как их сохранить?

Если у вас есть большие ресурсы и учебные пособия для создания чат-ботов с диалоговым потоком и javascript или Python, я был бы рад услышать о них!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...