我想在 Express 中使用 dialogflow 实现库
这是我的代码:
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:Progettinode_modulesdialogflow-fulfillmentsrcdialogflow-fulfillment.js:58:13)
[Node] at exports.doActions (D:Progettidistcontrollersdialogflow.js:14:19)
[Node] at Layer.handle [as handle_request] (D:Progettinode_modulesexpresslibrouterlayer.js:95:5)
你可以帮我吗? 如何将 dialogflow-fulfillment-nodejs 库与 Express 一起使用?
WebhookClient
构造函数采用一个包含字段request
和response
的选项对象。你把它们命名为req
和res
.
给定您必须doActions()
的参数,构造函数行应如下所示:
const agent = new WebhookClient({
request: req,
response: res
});