代码之家  ›  专栏  ›  技术社区  ›  swmcdonnell

如何使用dialogflow,node.js v2 library而不使用firebase

  •  0
  • swmcdonnell  · 技术社区  · 8 年前

    我正试图找出如何将DialogFlow与Express/BodyParser和node.js库一起使用 第二版 功能 没有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);
    

    但我很困惑:

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

    2. 我怎样才能把这个例子和我已经做过的事情结合起来 使用json数据并开始使用node.js函数 应用程序=对话框流() 在图书馆?

    2 回复  |  直到 8 年前
        1
  •  0
  •   Shuyang Chen    8 年前

    这个 app 使用 dialogflow 函数可以像快速请求处理程序函数一样使用。因此,你可以用快车叫它 request response 对象来处理请求。

    在运行函数中 Agent 同学们,你可以做些

    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 是你在代码中听到的后端点。

        2
  •  0
  •   swmcdonnell    7 年前

    最后,事情很简单。我只需要在BodyParser中包含应用程序:

    eapp.use(bodyParser.json(), app);
    
    推荐文章