Skip to content

Instantly share code, notes, and snippets.

@lakhansamani
Created April 4, 2020 12:26
Show Gist options
  • Save lakhansamani/0f5b503761dc2f681d82297fcc43a1e5 to your computer and use it in GitHub Desktop.
Save lakhansamani/0f5b503761dc2f681d82297fcc43a1e5 to your computer and use it in GitHub Desktop.
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
const config = process.env.FIREBASE_CONFIG;
admin.initializeApp({
credential: admin.credential.cert(config),
databaseURL: 'ws://medicalchat-hfccsy.firebaseio.com'
});
const ref = admin.database().ref('disease');
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 fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function handleDisease(agent) {
const disease = JSON.stringify(agent.parameters.disease);
console.log(`disease is: ${disease}. Type of ${typeof disease}`);
const diseaseKey = disease.toLowerCase().replace(/ /g, '_').replace(/"/g, '');
console.log(`Disease key: ${diseaseKey}`);
agent.add(`Thank you for sharing that information`);
return ref.child(diseaseKey).once("value").then((data) => {
console.log('data', JSON.stringify(data), typeof data);
if (data && data.val()) {
const value = data.val();
agent.add(`Medicine for ${disease} is ${value.medicine}. \n And the symptoms for this disease might be \n ${value.symptoms}`);
} else {
agent.add(`Sorry no medicine found for this dieases`);
}
}).catch(err => {
console.log('error', err);
});
}
let intentMap = new Map();
intentMap.set('AskDisease', handleDisease);
agent.handleRequest(intentMap);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment