如何在没有Firebase的情况下使用DialogFlow,node.js v2库



我正在尝试弄清楚如何将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);

但我对以下方面感到困惑:

  1. DialogFlow 实现需要一个 https 端点,所以我没有 像我一样创建HTTPS服务器?

  2. 如何将此示例集成到我已经完成的停止中 使用 JSON 数据并开始使用 node.js 函数app=dialogflow((在库中?

使用dialogflow函数创建的app实例可以像 Express Request 处理程序函数一样使用。因此,您可以使用 Expressrequestresponse对象来调用它来处理请求。

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 端点时,可以将对话流中的实现 URL 设置为如下所示的内容:

https://subdomain.domain.tld/actions/actions是您在代码中侦听的后端点。

最后,这很简单。我只需要将应用程序包含在正文解析器中:

eapp.use(bodyParser.json(), app);

最新更新