I am having issue ingrating my dialogflow intents with the firebase realtime database. I have a database in firebase that only has a single key/value pair that has the address. I have also initalized the dialogflow intent with the name getAddress. Can’t seem to integrate the bot to firebase using fulfulment. Image of firebase database attached for reference.
Tell me what am I doing wrong.
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const firebaseAdmin = require("firebase-admin");
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.applicationDefault(),
databaseURL: 'https://wax-bde2f.firebaseio.com/'
});
//const {Card, Suggestion} = require('dialogflow-fulfillment');
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 getFromFirebase(agent) {
return firebaseAdmin.database().ref('address').once("value")
.then((snapshot) => {
if (snapshot.exists()) {
agent.add(`Hey! The address is: ${snapshot.val()}`);
} else {
agent.add("Address not found in the database.");
}
})
.catch((err) => {
console.error("Error fetching data from Firebase:", err);
agent.add("Sorry, there was an issue fetching the address.");
});
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('getAddress', getFromFirebase);
agent.handleRequest(intentMap);
});