javascript - 如何在不同的 Firebase 文档上记录来自与 DialogFlow 机器人的不同对话的响应?

标签 javascript firebase dialogflow-es

我一直在关注 this video ,但似乎每次我们都在同一个地方拍摄。我希望我可以分别记录每个 Facebook 客户的回复。你知道我该怎么做吗?

'use strict';

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

admin.initializeApp({
    credential: admin.credential.applicationDefault(),
    databaseURL:'ws://la-base-de-datos.firebaseio.com/'
});

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

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

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });

  function welcome(agent) {
    agent.add(`Salut! C'est Jacques. Et ouai, je suis revenu d'outre-tombe?`);
    agent.add(`J'étais maire de la ville autrefois tu sais?`);
    agent.add(`Si ca te dit je te pose quelques questions pour savoir quel candidat pourrait te correspondre?`);
    agent.add(`Et quels sont les gens qui partagent tes opinions autour de toi!`);
    agent.add(`On y va?`);
    agent.add(new Suggestion("C'est parti!"));
  }

  function answerIntroductionHandler(agent){
    agent.add('Super! Quel est votre arrondissement? (I, ..., XIII, ...)');
  }

  function answerArrondissementHandler(agent){
    const arrondissement = agent.parameters.text;
    admin.database().ref("data").set({
        arrondissement: arrondissement
    });
    agent.add(new Card({
        title: '1/11 Faut-il accélérer l’automatisation ',
        imageUrl: 'http://www.leparisien.fr/resizer/mz-PnB5RECZ1q-z9GDRvlB_3jsg=/932x582/arc-anglerfish-eu-central-1-prod-leparisien.s3.amazonaws.com/public/RJPSM346RO4M5VIDDOS35APBII.jpg',
        text: 'du métro ?'
      })
    );
    agent.add(new Suggestion("Oui"));
    agent.add(new Suggestion("Non"));}

  function answer1Handler(agent){

    const answer = agent.parameters.Boolean;

    if(answer === 'true'){
      score.griveaux = (score.griveaux+1) || 1 ;
    }else{
    }

    admin.database().ref("data").set({
        accelerer_metro: answer
    });

    answers.push(answer);

  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('answerIntroduction', answerIntroductionHandler);
  intentMap.set('answerArrondissement', answerArrondissementHandler);
  intentMap.set('answer1', answer1Handler);


  agent.handleRequest(intentMap);
});

我的数据库是这样的:

introducir la descripción de la imagen aquí

最佳答案

Firebase 数据库使用简单的 JSON 模型来存储数据,因此您可以在 data 中创建单独的对象以使用 push 来存储值

改变这个

admin.database().ref("data").set({
    accelerer_metro: answer
});

admin.database().ref("data").push({
    accelerer_metro: answer
});

通过这种方式,您将能够存储所有日志,但它们将无法识别。更好的方法可能是为每个用户提供单独的存储链接。

对于可识别信息,您可以使用用户 ID(来自 conv.user.access.token 或者如果您已将 ID 存储在 storage 中)或使用对话id conv.id

admin.database().ref(`data/${conv.id}`).update({
    accelerer_metro: answer
});

关于javascript - 如何在不同的 Firebase 文档上记录来自与 DialogFlow 机器人的不同对话的响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62199471/

相关文章:

javascript - 奇怪的 JavaScript 行为?

javascript - 在 angularJs 中创建指令

firebase - 获取firebase实时数据库中的随机条目

javascript - 由于 promise 链,Firebase Firestore 更新查询未执行。我究竟做错了什么?

dialogflow-es - 如何将后续意图上传到另一个父意图

json - DialogFlow Google Assistant Webhook 响应 : "Empty speech response"

javascript - 数据库位于 Meteor 的哪里?

javascript - 在对象上 react 映射,然后在数组上 react

python - 在 Python 中调用 Pyrebase 时出现未知语法错误

python - 当响应包含 baicCard 时,Dialogflow Webhook 在 AoG Simulator 中给出错误响应