node.js - 无法使用对话流实现将数据添加到 firestore

标签 node.js firebase google-cloud-firestore dialogflow-es

我想使用对话流实现将数据发送到我的 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}); 行,我的程序可以正常工作,但是使用此行,程序甚至不会进入此函数并显示意图响应而不是我的履行响应。我应该如何解决此问题?

最佳答案

您显示的“错误”是一个警告,通常不会阻止事情正常进行。也就是说,它假设基于其运行的项目的环境。如果您正在访问同一项目中的数据存储 - 您不应该遇到任何问题。

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

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

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

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

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

set() 本身的调用似乎没有任何问题,但您没有进行任何错误处理。看起来您在知道 set() 是否真正起作用之前就发送了“You're good to go”消息。为了解决这个问题,您可能需要更改 addLeads() 以便它返回一个 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 并返回它(因此 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." );
          });

关于node.js - 无法使用对话流实现将数据添加到 firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59926863/

相关文章:

node.js - Nodejs 和 Coffeescript 安装(Ubuntu 12.04)

javascript - 登录按钮重定向到两个页面。对于首次登录者,它会重定向到协议(protocol)页面,对于休息,它会直接重定向到主页?

android - 未生成 Crashlytics 映射文件

swift - "The default Firebase app has not yet been configured"当它有时 - Swift

json - Flutter:如何下载Json文件到本地然后访问

javascript - Firestore - 如何在我的状态下添加文档 ID 及其数据?

angularjs - 使用 Express/Passport、AngularJS 和 ensureLoggedIn 的身份验证和路由在 "/"url 上不起作用

javascript - ': :' 是什么意思?

java - 比 datasnapshot 更好的方法是从 Firebase 检索每个特定帖子的特定数据?返回的位置错误

angular - 使用 Firebase 规则定义默认值