Last active
November 3, 2019 23:21
-
-
Save Anshul0305/6df09495c64be710104da3c76e7f95b6 to your computer and use it in GitHub Desktop.
Dialogflow - Save to Firebase Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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