我正在努力弄清楚如何在DialogFlow /bodyParser和node.js库v2 functions 中使用不带Firebase (在我自己的服务器上)。我让它处理请求/响应JSON数据,但我不知道如何使用node.js库函数对话框()。下面是我使用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);但我很困惑:
发布于 2018-08-26 04:21:16
最后,这很简单。我只需要在just解析器中包含这个应用程序:
eapp.use(bodyParser.json(), app);发布于 2018-07-09 19:37:11
使用app函数创建的dialogflow实例可以像使用快捷请求处理程序函数一样使用。因此,您可以使用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是您在代码中收听的后置端点。
https://stackoverflow.com/questions/51195505
复制相似问题