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

How to use DialogFlow, node.js v2 library without Firebase

我正在尝试弄清楚如何在 express/bodyParser 和 node.js 库 v2 函数 的情况下使用 DialogFlow[=] =30=](在我自己的服务器上)。我可以使用 request/response 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 处理函数一样使用。因此,您可以使用 Express requestresponse 对象调用它来处理请求。

Agent class 的 运行 函数中,您可以执行类似

的操作
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
}

然后,当您将此服务器部署到 public HTTPS 端点时,您可以将 Dialogflow 中的实现 url 设置为如下内容:

https://subdomain.domain.tld/actions 其中 /actions 是您在代码中收听的 post 端点。

最后很简单。我只需要将应用程序包含在 bodyparser 中:

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