Skip to content

Instantly share code, notes, and snippets.

@Anshul0305
Last active November 3, 2019 23:21
Show Gist options
  • Save Anshul0305/6df09495c64be710104da3c76e7f95b6 to your computer and use it in GitHub Desktop.
Save Anshul0305/6df09495c64be710104da3c76e7f95b6 to your computer and use it in GitHub Desktop.
Dialogflow - Save to Firebase Database
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: 'ws://DATABASE_UNIQUE_ID.firebaseio.com/'
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({request, response});
function saveHandler(agent){
const value = agent.parameters.PARAMETER_NAME;
return admin.database().ref(DATABASE_REFERENCE).set({
KEY: value
});
}
function fetchHandler(agent){
return admin.database().ref(DATABASE_REFERENCE).once("value").then((snapshot) => {
const value = snapshot.child("KEY").val();
if(value !== null){
agent.add(`The value from DB is ${value}`);
}
});
}
let intentMap = new Map();
intentMap.set('SaveToDB', saveHandler);
intentMap.set('FetchFromDB', fetchHandler);
agent.handleRequest(intentMap);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment