I want to send data to my firestore using dialogFlow fulfillment but it is not working. Here is my 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);
});
I have removed other functions for simplicity. Here is my package.json dependencies:
"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"
}
And here is my firestore permission:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
Main error that I can see in my logs is:
Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail
If I comment the let setDoc = dialogflowAgentRef.set(data,{merge:true});
line ,my program works fine but with this line, the program does not even enters this function and shows the intent response rather than my fulfillment response. How should I fix this?