Search code examples
javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functionsdialogflow-es

One index.js file in fulfillment dialogflow with multiple functions/trigger to firestore #AskFirebase


I have the code below in the dialogflow ("firebase-functions": "^ 3.7.0" and node: 10) DialogFlow index.js with issue Photos project in DialogFlow

  1. I have four intents: Ligar, Desligar, Abrir e Fechar (all with fulfillment enabled)

  2. I not have issue in deploy or in execution(logs cloud functions), but the only function that works is getLigar(). How to solve this?

    'use strict';
    
    const functions = require('firebase-functions');
    const {WebhookClient} = 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});
    
      function getLigar(agent) {
        var gpioalarmstateb = agent.parameters;
        return db.collection('xxxxx').doc('yyyyy').get()
          .then(doc => {
            const xalarmstate = doc.data().gpioalarmstate;
            if (!xalarmstate) {
              db.collection('xxxxx').doc('yyyyy').update({
                gpioalarmstate: true
              })
            }
            agent.add(`Gpioalarmstate is ${gpioalarmstateb} xalarmstate is  ${xalarmstate}.`);
          });
      }
    
      function getDesligar(agent) {
        var gpioalarmstateb = agent.parameters;
        return db.collection('xxxxx').doc('yyyyy').get()
          .then(doc => {
            const xalarmstateD = doc.data().gpioalarmstate;
            if (xalarmstateD) {
              db.collection('xxxxx').doc('yyyyy').update({
                gpioalarmstate: false
              })
            }
            agent.add(`Gpioalarmstate is ${gpioalarmstateb} xalarmstate is  ${xalarmstateD}.`);
          });
      }
    
    
      function getAbrirr(agent) {
        var gpiogaragestateb = agent.parameters;
        return db.collection('xxxxx').doc('yyyyy').get()
          .then(doc => {
            const xgaragestate = doc.data().gpiogaragestate;
            if (!xgaragestate) {
              db.collection('xxxxx').doc('yyyyy').update({
                gpiogaragestate: true
              })
            }
            agent.add(`Gpiogaragestate is ${gpiogaragestateb} xgaragestate is  ${xgaragestate}.`);
          });
      }
    
      function getFechar(agent) {
        var gpiogaragestateb = agent.parameters;
        return db.collection('xxxxx').doc('yyyyy').get()
          .then(doc => {
            const xgaragestateF = doc.data().gpiogaragestate;
            if (xgaragestateF) {
              db.collection('xxxxx').doc('yyyyy').update({
                gpiogaragestate: false
              })
            }
            agent.add(`Gpiogaragestate is ${gpiogaragestateb} xgaragestate is  ${xgaragestateF}.`);
          });
      }
    
      let intentMap = new Map();
      intentMap.set('Ligar', getLigar);
      intentMap.set('Desligar', getDesligar);
      intentMap.set('Abrir', getAbrirr);
      intentMap.set('Fechar', getFechar);
      agent.handleRequest(intentMap);
    });

Solution

  • The answer was: I needed async before the 4 functions mentioned in the question and put await in the right place. Below I put an example that served for the first of the 4 functions:

      async function getLigar(agent) {
    
        return await db.collection('xxxxx').doc('yyyyy').get()