Last active
May 25, 2019 22:08
-
-
Save zeuslawyer/9edc961255a78b4e893256664b894a8a to your computer and use it in GitHub Desktop.
Dialogflow App Fulfilment via EXPRESS server on Cloud Functions
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
const { | |
dialogflow, | |
Permission, | |
Suggestions, | |
BasicCard | |
} = require("actions-on-google"); | |
// Instantiate the Dialogflow client. | |
const app = dialogflow({ debug: true }); | |
// Handlers go here.. | |
app.intent("Default Welcome Intent", conv => { | |
// handler for this intent | |
}); | |
app.intent("Say_Something_Silly", conv => { | |
// handler for this intent | |
}); | |
module.exports = app; |
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 express = require("express"); | |
const bodyParser = require("body-parser"); | |
const functions = require("firebase-functions"); | |
// clients | |
const dialogFlowApp = require("./DialogflowApp"); | |
const expressApp = express().use(bodyParser.json()); | |
// EXPRESS APP fulfillment route (POST). The entire dialogFlowApp object (incl its handlers) is the callback handler for this route. | |
expressApp.post("/", dialogFlowApp); | |
// EXPRESS APP test route (GET) | |
expressApp.get("/", (req, res) => { | |
res.send("CONFIRMED RECEIPT OF GET."); | |
}); | |
/* | |
* LOCAL NGROK SERVER LOGIC. ENSURE that you "export IS_LOCAL_DEV=true" in terminal prior to start | |
*/ | |
if (process.env.IS_LOCAL_DEV) { | |
const PORT = 8000; | |
expressApp.listen(PORT, () => | |
console.log(`*** SERVER RUNNING LOCALLY ON PORT ${PORT} ***`) | |
); | |
} else { | |
console.log("*** NOT LOCALLY SERVED - OR - LOCAL ENV VAR NOT SET ****"); | |
} | |
//EXPORT two endpoints: one express app, one dialogflow app | |
exports.fulfillmentExpressServer = functions.https.onRequest(expressApp); | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(dialogFlowApp); |
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
"dependencies": { | |
"actions-on-google": "^2.0.0", | |
"body-parser": "^1.18.3", | |
"express": "^4.16.4", | |
"firebase-functions": "^2.2.0" | |
}, | |
"devDependencies": { | |
"ngrok": "^3.1.1" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment