如何使用dialogflow-fulfillment-nodejs与Express

时间:2018-06-12 13:31:18

标签: node.js express dialogflow

我想将对话框流程实现库用于Express

这是我的代码:

import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as dialogflowController from './controllers/dialogflow';

const app: express.Application = express();
app.use(bodyParser.json());
app.post('/echo', dialogflowController.doActions);
const http = require('http');
const httpServer = http.createServer(app);
httpServer.listen(80, function () { // ascolta sulla porta 80 per comando: ngrok http 80
  console.log('HTTP Started!');
});



// dialogflowController controller unit

import { Request, Response, NextFunction } from 'express';
const { WebhookClient, Card, Suggestion } = require('dialogflow-fulfillment');


export let doActions = (req: Request, res: Response, next: NextFunction) => {
  console.log('here it works');
  const agent = new WebhookClient({ req, res });
  console.log('crashes to the previous line');
};

这就是错误

Error: Request can NOT be empty.
[Node]     at new WebhookClient (D:\Progetti\node_modules\dialogflow-fulfillment\src\dialogflow-fulfillment.js:58:13)
[Node]     at exports.doActions (D:\Progetti\dist\controllers\dialogflow.js:14:19)
[Node]     at Layer.handle [as handle_request] (D:\Progetti\node_modules\express\lib\router\layer.js:95:5)
你能帮帮我吗? 如何在Express?

中使用dialogflow-fulfillment-nodejs库

1 个答案:

答案 0 :(得分:7)

WebhookClient构造函数采用包含字段requestresponse的选项对象。您将其命名为reqres

根据doActions()

的参数,构造函数行看起来应该是这样的
const agent = new WebhookClient({
  request: req,
  response: res
});