我使用“ botact ”库创建了一个聊天机器人,但是 当我尝试在vk-community API工作页上验证我的 bot 时,我在“ Windows PowerShell ”中收到错误消息(在这里,我启动了bot的服务器):< / p>
TypeError: Cannot read property 'fwd_messages' of undefined
at Botact.getLastMessage (C:\Users\whoami\Desktop\Bot-test\node_modules\botact\lib\utils\getLastMessage.js:2:11)
at Botact.module.exports (C:\Users\whoami\Desktop\Bot-test\node_modules\botact\lib\methods\listen.js:29:28).
文件“ getLastMessage.js”包含以下代码:
const getLastMessage = (msg) => {
if (msg.fwd_messages && msg.fwd_messages.length) {
return getLastMessage(msg.fwd_messages[0])
}
return msg
}
module.exports = getLastMessage
答案 0 :(得分:1)
因此,我对botact
并不太了解,但是根据到达/
路由时的代码,您需要传递包含object
属性的主体。
现在,由于这是vk bots
的漫游器框架,因此它可能会自动发送请求正文。您可以通过记录请求正文来确定。
server.post('/', async (req,res)=>{
console.dir(req.body);
await bot.listen(req, res);
});
const { type, secret, object, group_id } = framework === 'koa'
? args[0].request.body
: args[0].body
...
...
...
const { events, middlewares } = actions
const forwarded = this.getLastMessage(object)
现在,当您执行bot.listen
时,将传递req
作为第一个参数。和{ type, secret, object, group_id }
这些字段从req.body
中分离出来。
然后object
被传递给getLastMessage
函数。
因此对于最少的请求正文,您将需要
{ "object": {} }
这是我从邮递员将其添加到请求正文后得到的200 OK
输出
POC代码:
const express = require("express");
const bodyParser = require("body-parser");
const { Botact } = require("botact");
const server = express();
const bot = new Botact({
token: "token_for_my_group_i_just_hided_it",
confirmation: "code_for_my_group_i_just_hided_it"
});
server.use(bodyParser.json());
server.post("/",bot.listen);
server.listen(8080);