0

I tried to connect my existing firestore database to Dialogflow fulfillment. The function deployed to Google CLoud Function successfully but I cannot add data to my database. I have tried to change the node version 8 but still failed. Please help me.

Index.js

'use strict';
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
   

  admin.initializeApp({
   apiKey: "Axxxx",
    authDomain: "/xxxxx.firebaseio.com",
    databaseURL: "https:/xxxxxx.firebaseio.com",
    projectId: "mxxxxx8",
    storageBucket: "xxxxx.com",
  });

  const functions = require('firebase-functions');
  const settings = {timestampsInSnapshots: true};
  var db = admin.firestore();
  admin.firestore().settings({ timestampsInSnapshots: true });
 
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?`);
  }
  

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

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": "~7.1.1",
    "firebase-functions": "^2.2.1",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.5.0"
  }
2
  • Never mind, I found the answer. I made new google cloud project from my current firebase project. Commented Dec 1, 2020 at 16:00
  • If you're done with this question and it's no longer relevant, simply delete it. Commented Dec 1, 2020 at 16:24

3 Answers 3

1

The solution is upgrade your Firebase project plan to Blaze Plan. Then make a new Dialogflow agent under that Firebase project.

Sign up to request clarification or add additional context in comments.

Comments

0

Your Firebase initialization seems wrong. Here's how you should do it:

.
.
.
admin.initializeApp({
  credential: admin.credential.applicationDefault(),
  databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});
.
.
.

This should work.

1 Comment

Thank you for your respond .I already try but still get the same error.
0

Based on the documentation it should be like this,

serviceAccount = require('./serviceAccount.json');

const adminConfig = JSON.parse(process.env.FIREBASE_CONFIG);
adminConfig.credential = admin.credential.cert(serviceAccount);
admin.initializeApp(adminConfig);

2 Comments

Thank you for your respond. I already found the solution
Please consider adding the solution to your issue as an answer stackoverflow.com/help/self-answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.