I'm trying to figure out how to use DialogFlow with express/bodyParser and the node.js library v2 functions without Firebase (on my own server). I have it working with the request/response JSON data, but I can't figure out what I need to do to use the node.js library function dialogflow() . Here's a snippet of what I have that's working with the JSON data:
const {config} = require('./config');
const https = require('https');
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const options = {
key: fs.readFileSync(config.SSLDIR + 'privkey.pem'),
cert: fs.readFileSync(config.SSLDIR + 'cert.pem'),
ca: fs.readFileSync(config.SSLDIR + 'chain.pem')
};
const eapp = express();
eapp.disable('x-powered-by');
eapp.use(bodyParser.urlencoded({extended: true}));
eapp.use(bodyParser.json());
const server = https.createServer(options, eapp).listen(config.LISTEN_PORT, () => {
console.log(`API listening on port ${config.LISTEN_PORT}. Ctrl-C to end.`);
});
server.on('error', (e) => {
console.log(`Can't start server! Error is ${e}`);
process.exit();
});
// I have an Agent class that reads the request object and handles it
eapp.post("/actions", (request, response) => {
const agent = new Agent(request, response);
agent.run();
return;
});
eapp.all('*', (request, response) => {
console.log("Invalid Access");
response.sendStatus(404);
});
The only solution posted online that I could find said to use the following code:
const express = require('express');
const bodyParser = require('body-parser');
const { dialogflow } = require('actions-on-google');
const app = dialogflow();
express().use(bodyParser.json(), app).listen(3000);
But I'm confused about:
DialogFlow fulfillment requires an https endpoint, so don't I have to create an https server like I did?
How can I integrate this example into what I've already done to stop using the JSON data and start using the node.js functions from app=dialogflow() in the library?
The app
instance created using the dialogflow
function can be used like an Express Request handler function. Thus, you can call it with the Express request
and response
object to handle the request.
In the run function for your Agent
class, you can do something like
run() {
const request = ...; // Express request object
const response = ...; // Express response object
const app = ...; // app instance created using the dialogflow function
app(request, response); // call app with the Express objects
}
Then when you deployed this server to a public HTTPS endpoint, you can set the fulfillment url in Dialogflow to something like:
https://subdomain.domain.tld/actions
where /actions
was the post endpoint you listened to in the code.
In the end, it was very simple. I just needed to include the app in bodyparser:
eapp.use(bodyParser.json(), app);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.