实现对话流中的一个 index.js 文件,多个 functions/trigger 文件用于 firestore #AskFirebase

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

我在对话流中有以下代码(“firebase-functions”:“^ 3.7.0” 和节点:10) DialogFlow index.js with issue Photos project in DialogFlow

  1. 我有四个意图:Ligar、Desligar、Abrir e Fechar(都启用了实现)

  2. 我在 部署 或执行(记录云函数)时没有问题,但唯一有效的函数是 getLigar() .如何解决?

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

答案是:我在问题中提到的4个函数之前需要async,并把await放在正确的位置。下面我举了一个为 4 个函数中的第一个函数服务的示例:

  async function getLigar(agent) {

    return await db.collection('xxxxx').doc('yyyyy').get()