1

I have created a dialogflow agent to register some informations from the users, aind i need to write these answers as the same document for each user, but the code i made is writing each answer to an individual document instead of all of them as different, the logs don't show any errors so i suppose it's a logic problem that i am unable to identify...

index.js:

'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

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 cadastroHandler(agent) {
    let nome = agent.parameters.nome ;
    db.collection("cadastros").add({ nome: nome });

    let estado = agent.parameters.estado ;
    db.collection("cadastros").add({ estado: estado });

    let cidade = agent.parameters.cidade ;
    db.collection("cadastros").add({ cidade: cidade });

    let coord = agent.parameters.coord ;
    db.collection("cadastros").add({ coord: coord });

    let wppcoord = agent.parameters.wppcoord ;
    db.collection("cadastros").add({ wppcoord: wppcoord });

    let emailcoord = agent.parameters.emailcoord ;
    db.collection("cadastros").add({ emailcoord: emailcoord });

    let area = agent.parameters.area ;
    db.collection("cadastros").add({ area: area });

    agent.add(`cadastro concluido`);
  }

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


  let intentMap = new Map();
  intentMap.set('cadastro', cadastroHandler);
  intentMap.set('Default Fallback Intent', fallback);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

each of the paramaters are asked in sequence to the user in the same "cadastro" intent and logged in as individual parameters

Alex L
  • 4,168
  • 1
  • 9
  • 24

1 Answers1

0

Change your function to this

function cadastroHandler(agent) {
    let { nome, estado, cidade, coord, wppcoord, emailcoord, area } = agent.parameters;
    db.collection("cadastros").add({ 
       nome: nome,
       estado: estado,
       cidade: cidade,
       coord : coord ,
       wppcoord  : wppcoord  ,
       emailcoord: emailcoord ,
       area :area 
    });

    agent.add(`cadastro concluido`);
  }

Reza
  • 18,865
  • 13
  • 88
  • 163