我该如何解决转发邮件的问题?

时间:2020-01-11 13:08:14

标签: javascript node.js bots vk

我使用“ 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

1 个答案:

答案 0 :(得分:1)

因此,我对botact并不太了解,但是根据到达/路由时的代码,您需要传递包含object属性的主体。

现在,由于这是vk bots的漫游器框架,因此它可能会自动发送请求正文。您可以通过记录请求正文来确定。

server.post('/', async (req,res)=>{
    console.dir(req.body);
    await bot.listen(req, res);
});

/lib/methods/listen.js

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输出

postman request example

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);