我试图弄清如何将DialogFlow与express / bodyParser和node.js库 v2 函数不带Firebase 一起使用(在我自己的服务器上)。我可以使用它处理请求/响应JSON数据,但是我无法弄清楚使用node.js库函数 dialogflow()需要做什么。这是我使用JSON数据的摘要:
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);
});
我发现网上发布的唯一解决方案是使用以下代码:
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);
但是我很困惑:
DialogFlow实现需要一个https终结点,所以我没有 创建像我一样的https服务器?
如何将这个示例集成到已经完成的停止操作中 使用JSON数据并从以下位置开始使用node.js函数 库中的 app = dialogflow()?
答案 0 :(得分:0)
使用app
函数创建的dialogflow
实例可以像Express Request处理程序函数一样使用。因此,您可以使用Express request
和response
对象调用它来处理请求。
在您的Agent
类的run函数中,您可以执行类似的操作
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
}
然后,当将此服务器部署到公共HTTPS终结点时,可以将Dialogflow中的实现URL设置为类似以下内容:
https://subdomain.domain.tld/actions
,其中/actions
是您在代码中收听的发布端点。
答案 1 :(得分:0)
最后,这非常简单。我只需要将应用程序包含在bodyparser中:
eapp.use(bodyParser.json(), app);