What happens is that, I'm working with Dialogflow , given that I'm creating a Chatbot for a company, so through webhook, in the Fulfillment options I'm connecting to a Firebase database asi.
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
// initialise DB connection
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: 'https://<Nombre de la base de datos>.firebaseio.com',
});
process.env.DEBUG = 'dialogflow:debug';
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 handleAge(agent) {
const edad = agent.parameters.edad;
//agent.add('Muchas gracias...');
//return admin.database().ref('ageInfo').once("value").then((snapshot) => {
// var averageAge = snapshot.child("runningAverage").val();
// agent.add('Our recorded average age is ' + averageAge);
//});
return admin.database().ref('ageInfo').transaction((ageInfo) => {
if(ageInfo !== null) {
let oldAverage = ageInfo.runningAverage;
let oldTotalCount = ageInfo.totalCount;
let newAverage = (oldAverage * oldTotalCount + edad) / (oldTotalCount + 1);
ageInfo.runningAverage = newAverage;
ageInfo.totalCount+=1;
//agent.add('Nuestra edad promedio registrada es ' + newAverage);
}
return ageInfo;
}, function(error, isSuccess) {
console.log('Actualizar el éxito de transacción de edad promedio: ' + isSuccess);
});
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('preguntaedad', handleAge);
agent.handleRequest(intentMap);
});
My question is if there is any possibility that I can connect to a database, to enter data entered by the user.