How do I log responses from different conversations with a DialogFlow bot on different Firebase documents?

Viewed 68

I've been following this video, but it seems like every time we shoot in the same place. I wish I could record the responses separately for each Facebook client. Do you know how I can do that?

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

And my database looks like this:

introducir la descripción de la imagen aquí

1 Answers

Firebase Database uses a simple JSON model for storing data, hence you can create separate objects inside data to store the values by using push

Chaning this

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

to

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

By this you would be able to store all the logs, but they would be un-identifiable. A better approach could be having separate storage link for each user.

For identifiable information, you can use either user id (from conv.user.access.token or if you have stored an id in storage) or use the conversation id conv.id

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