0

I am trying to read data in Firebase - Cloud Firestore database in Dialogflow. But not able to connect to the database and getting the following error in log:

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

Code is in welcome intent fulfillment - Inline Editor(Powered by Cloud Functions for Firebase)

Collection Name in Cloud Firestore - users document - 10 and 11 (totally 2) field - book_id and text

Can someone please help me resolve this one or face similar issue.?

I have tried different solutions provided for this error faced by others, but none of it has solved mine.

  1. Tried changing node version to 8 in package.json
  2. Tried Initializeapp as below:

    const admin = require('firebase-admin');

    const app = dialogflow({debug: true});

    admin.initializeApp();

  3. Its not going inside app.intent(welcome) itself

index.js

// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
const axios = require('axios');
const {dialogflow} = require('actions-on-google');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

const admin = require('firebase-admin');
const app = dialogflow({debug: true});

admin.initializeApp();

const db = admin.firestore();
db.settings({timestampsInSnapshots: true});
const collectionRef = db.collection('users');

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

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  /*function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }*/

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }

  app.intent('welcome', (conv) => {

    console.log("welcome agent invoked");
    const book = (agent.parameters.book).toString();
    const termRef = collectionRef.doc('10');

  return termRef.get()
    .then((snapshot) => {
      const {text, book_id} = snapshot.data();
      conv.ask(`Here you go, ${text}, ${book_id}. ` +
        `What else do you want to know?`);
     console.log('text:', +text);
     console.log('book_id:', +book_id);
    }).catch((e) => {
      console.log('error:', e);
      conv.close('Sorry, try again');
    });
});
});

package.json

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "8"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "^2.2.0",
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.2",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.5.0"
  }
}

Trying to get book_id value and text value from collection users in cloud firestore

Igor F.
  • 2,649
  • 2
  • 31
  • 39
Dinesh BR
  • 31
  • 4

0 Answers0