无法使用 dialogflow fulfillment 将数据添加到 firestore

Not able to add data to firestore using dialogflow fulfillment

我想使用 dialogFlow fulfillment 将数据发送到我的 firestore,但它不起作用。 这是我的 index.js:

// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'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(functions.config().firebase);
const db = admin.firestore();
var product,phoneNo,eMail;;

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

var flag=0;


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

function welcome(agent) {
      if(flag==0){
        agent.add(`Can you please tell me what is your product?`);
        flag=1;
      }
      else if(flag==1){
        var prod=request.body.queryResult.queryText;
        product=prod;
        flag=2;
        agent.add(`Please provide me your Phone No or your E-Mail ID so that my team can contact you.`);
      }
      else if(flag==2||flag==3){
        let missingSlots1 = [];
        var queryRes=request.body.queryResult.queryText;

        var [phone,email] = [agent.parameters[`phone`], agent.parameters[`mail`]];

        if(queryRes.includes(`@`)&&queryRes.includes(`.`)){
          email=queryRes;
          eMail=queryRes;
          agent.parameters[`mail`]=queryRes;
        }
        else if(queryRes.length>=10&&queryRes!=product){
          console.log(`phone ke andar wala if `+queryRes);
            phone=queryRes;
            phoneNo=queryRes;
            agent.parameters[`phone`]=phoneNo;
        }

        if(!phoneNo){missingSlots1.push(`Phone No`);}
        if(!eMail){missingSlots1.push(`E-mail`);}

        if(missingSlots1.length==2){
            agent.add(`Please provide me your Phone No or your E-Mail ID so that my team can contact you.`);
        }
        else if(flag==2){
            if(!eMail){
                agent.add(`Would you please provide your E-Mail ID?`);
            }
            if(!phoneNo){
                agent.add(`Would you please provide your Phone No?`);
            }
            flag=3;
        }
        else{
            flag=4;
            addLeads();
            agent.add(`Okay.Now you are good to go!`);  
        }
      }
  }


  function addLeads(){
    var data={
        'product':product,
        'email':eMail,
        'phoneNo':phoneNo
    };

    const dialogflowAgentRef = db.collection('botData').doc(eMail);
    let setDoc = dialogflowAgentRef.set(data,{merge:true});
  }



  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  agent.handleRequest(intentMap);
});

为简单起见,我删除了其他功能。 这是我的 package.json 依赖项:

"dependencies": {
    "actions-on-google": "^2.2.0",
    "firebase-functions": "^2.0.2",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.5.0",
    "@google-cloud/firestore": "^0.16.1",
    "firebase-admin": "^6.0.0"
  }

这是我的 firestore 许可:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

我在日志中看到的主要错误是:

Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail

如果我评论 let setDoc = dialogflowAgentRef.set(data,{merge:true}); 行,我的程序运行正常,但使用这一行,程序甚至不会进入此函数并显示意图响应而不是我的实现响应。 我该如何解决这个问题?

您显示的 "error" 是一个警告,通常不会阻止工作正常进行。它是说它假设环境基于它所在的项目 运行。如果您正在访问同一项目中的数据存储 - 您应该不会遇到任何问题。

当您尝试调用 set() 时,您没有说明发生了什么,但听起来这可能根本没有发生。

鉴于状态机取决于全局 flag 变量的值,这可能并不令人惊讶。尝试以这种方式跟踪对话有两个问题:

  1. 看起来它们只是在欢迎 Intent 中被调用。您没有显示此 Intent 定义,但这可能仅在首次调用机器人时发生,之后不会发生。

  2. 因为这是一个全局变量,而不是附加到对话的值,如果多个用户试图同时使用该机器人或者服务器是运行 on 已重置。如果您使用的是 Firebase Cloud Functions 或 Dialogflow 内置编辑器,这可能会在您不知情的情况下发生。

特别是,(2) 可能导致它永远无法到达调用 set() 的状态。

调用 set() 本身似乎没有任何问题,但您没有进行任何错误处理。看起来您在知道 set() 是否有效之前就发送了 "You're good to go" 消息。要解决这个问题,您可能需要更改 addLeads() 以使其 return 成为 Promise,然后调用它也可以使用 Promise。这可能会将 addLeads() 更改为

  function addLeads(){
    var data={
        'product':product,
        'email':eMail,
        'phoneNo':phoneNo
    };

    const dialogflowAgentRef = db.collection('botData').doc(eMail);
    return dialogflowAgentRef.set(data,{merge:true});
  }

然后,当你调用它时,你需要同时使用 Promise 和 return 它(因此 Dialogflow 将等待发送回复,直到 set() 完成。)可能是这样的:

        return addLeads()
          .then( () => {
            agent.add(`Okay. Now you are good to go!`);  
          })
          .catch( err => {
            console.error( "There was a problem", err );
            agent.add( "There was a problem saving the data." );
          });