【发布时间】:2019-12-24 18:46:16
【问题描述】:
已解决(见下文)
我正在尝试创建一个 API,以便我可以读取文本(从 word 文档)并让 botpress 中的机器人响应该文本的一部分。
我对几件事感到困惑:
我的 API 应该遵循什么结构(函数应该进入什么文件以及如何连接它们,或者,我可以将函数放入主 app.js 文件中)
如何调用该 word 文档的我想要的部分,以便机器人可以响应?
如您所见,我可以调用数组中的不同元素(执行{{session.response.0}} 并且机器人将响应Tony On enter 或{{session.response.1}} "Lisa"。
我的 api 结构中只有一个 app.js 文件,但没有其他文件。
这是我的 api 文件 (app.js)
var express = require("express");
var fs = require('fs');
var app = express();
var port = process.env.PORT || 3002;
app.get("/url", (req, res, next) =>{
res.json(["Tony", "Lisa", "Michael","Ginger","Food"]);
});
fs.readFile('/home/user/Desktop/test/doc.html', 'utf8', function(err, contents) {
res.json(contents);
});
app.listen(port, () => {
console.log("Server running on port: " + port);
});
这是我的操作文件(也就是调用/链接 api 到 botpress 的东西):
const axios = require('axios')
/**
* @title testApi
* @category Test
* @author test
*/
const testApi = async () => {
// We call the test API
const { data } = await axios.get('http://localhost:3002/url/')
// We assign the response to the session variable so we can use it later
session.response = data
}
// Actions are async, so make sure to return a promise
return testApi()
已解决 编辑:找到一个文本阅读器 (https://github.com/dbashford/textract)
【问题讨论】:
-
你能提供你目前有什么吗?
-
我做了...代码在帖子里
-
请将您的解决方案添加为下方的“答案” - 这可以提高未来访问者对该问题的可读性。
-
您需要了解 JS 正在处理回调,您的代码没有任何意义 imo
-
@FlashThunder 你是什么意思?它到底有什么意义?我还是个 JS 初学者
标签: javascript node.js api-design botpress