Last active
February 21, 2024 23:42
-
-
Save jirawatee/1d58fc98a8543e4b46e37d8d5693691d to your computer and use it in GitHub Desktop.
Dialogflow integrate with Cloud Functions - Cloud Firestore
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.firestore().collection('bmi').doc(result).get().then(doc => { | |
agent.add(doc.data().description); | |
}); | |
} | |
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