0

I was trying to connect firebase realtime database with dialogflow. and I want, when user ask question to chatbot it will read data from firebase realtime database and show that data to user. I have tried this way:-

'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const firebaseAdmin = require("firebase-admin");

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
 
 firebaseAdmin.initializeApp({
     apiKey: "AI*********************",                             
  authDomain: "**********.firebaseapp.com",         
  databaseURL:"https://**************.firebaseio.com",
  storageBucket:"*******.appspot.com",       
  messagingSenderId: "***************",                
});

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 akvo agent!`);
  }
 
  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }
  
  function getFromFirebase(agent){
    return firebaseAdmin.database().ref('board_id').once("value").then((snapshot) => {
    var boardid = snapshot.val();
      agent.add(boardid);
    });
  }


  // 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('board id', getFromFirebase);
  agent.handleRequest(intentMap);
});

This is my 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": "10"
  },
  "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"
  }
}

after deploying this code I am getting an error.

In Logs explorer :- Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail

FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Failed to parse access token response: SyntaxError: Unexpected token M in JSON at position 0"."}

In Diagnostic info Fulfillment status :- "Webhook call failed. Error: DEADLINE_EXCEEDED, State: URL_TIMEOUT, Reason: TIMEOUT_WEB."

Please help to solve this problem.

Akvo
  • 3
  • 2

0 Answers0