Last active
February 24, 2019 05:18
-
-
Save jirawatee/7d3b964972c58474bd07eb9026e270d8 to your computer and use it in GitHub Desktop.
Dialogflow integrate with Cloud Functions - Realtime Database
This file contains 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
"use strict"; | |
const functions = require("firebase-functions"); | |
const { WebhookClient } = require("dialogflow-fulfillment"); | |
const { Card, Suggestion } = require("dialogflow-fulfillment"); | |
const admin = require("firebase-admin"); | |
admin.initializeApp({ | |
credential: admin.credential.applicationDefault(), | |
databaseURL: 'https://<YOUR-PROJECT-ID>.firebaseio.com' | |
}); | |
process.env.DEBUG = "dialogflow:debug"; // enables lib debugging statements | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { | |
const agent = new WebhookClient({ request, response }); | |
function bodyMassIndex(agent) { | |
let weight = request.body.queryResult.parameters.weight; | |
let height = request.body.queryResult.parameters.height / 100; | |
let bmi = (weight / (height * height)).toFixed(2); | |
let result = "none"; | |
if (bmi < 18.5) { | |
result = "xs"; | |
} else if (bmi >= 18.5 && bmi <= 22.9) { | |
result = "s"; | |
} else if (bmi >= 23 && bmi <= 24.9) { | |
result = "m"; | |
} else if (bmi >= 25 && bmi <= 29.9) { | |
result = "l"; | |
} else if (bmi > 30) { | |
result = "xl"; | |
} | |
return admin.database().ref("bmi").child(result).once("value").then(snapshot => { | |
agent.add(snapshot.val()); | |
}); | |
} | |
let intentMap = new Map(); | |
intentMap.set("BMI - custom - yes", bodyMassIndex); | |
agent.handleRequest(intentMap); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment