Vk-io: 尝试从对话中删除用户时出错。

创建于 2017-12-03  ·  20评论  ·  资料来源: negezor/vk-io

VK-IO - 4.0.0-alpha.4
节点 - v9.2.0
我想发送 2 个相同的请求以从 2 个对话中删除参与者。
1 次请求通过,第二次我发现错误。

You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
FetchError: network timeout at: https://api.vk.com/method/messages.removeChatUser?access_token=ТОКЕН&v=5.69
    at Timeout._onTimeout (/home/vk/node_modules/node-fetch/lib/index.js:1272:13)
    at ontimeout (timers.js:478:11)
    at tryOnTimeout (timers.js:302:5)
    at Timer.listOnTimeout (timers.js:262:5)
Caught unhandledRejection: { FetchError: network timeout at: https://api.vk.com/method/messages.removeChatUser?access_token=ТОКЕН&v=5.69
    at Timeout._onTimeout (/home/vk/node_modules/node-fetch/lib/index.js:1272:13)
    at ontimeout (timers.js:478:11)
    at tryOnTimeout (timers.js:302:5)
    at Timer.listOnTimeout (timers.js:262:5)
  message: 'network timeout at: https://api.vk.com/method/messages.removeChatUser?access_token=ТОКЕН&v=5.69',
  type: 'request-timeout',
  stackframes:
   [ { file_name: '/home/vk/node_modules/node-fetch/lib/index.js',
       line_number: 1272 },
     { file_name: 'timers.js', line_number: 478 },
     { file_name: 'timers.js', line_number: 302 },
     { file_name: 'timers.js', line_number: 262 } ] }

最有用的评论

如果在重新启动应用程序后,bot 响应了一段时间,然后停止,那么很可能是 Long Poll 对所有事情负责,如果你没有搞砸其他任何地方。 最有可能的是,你忘记了对不成功请求的错误处理,Long Poll VK 服务器经常喜欢给出 403、500-503。

同时,重要的是要记住node-fetch将所有服务器响应(即使有错误)传输到 then() 块,从 README 判断:

3xx-5xx 响应不是网络错误,应该在 then ()

因此,例如,该错误可能已潜入此处的某个地方。

可能大错特错,因为库代码我没研究过,但是“忽略”bots最常见的问题是Long Poll的运行不稳定,所以所有的错误都需要在发生时进行处理并重新连接到服务器。

PS然而,最好的选择是尝试将users.get → messages.getChatUsers → messages.removeChatUser → message.send → users.get?代码中的这些顺序请求“打包”为一个execute -request。 从保存请求的角度来看,这将更有效,因为每秒有 3 个请求的已知限制。

所有20条评论

我可以获得代码和已安装选项的列表吗?

vk.api.messages.getChatUsers({chat_id: iChatID, fields: "online"}).then((chat) => {
    if (chat.indexOf(user.id) > -1) {
        vk.api.messages.removeChatUser({chat_id: iChatID, user_id: user.id}).then(() => {
            return message.send(`@id${user.id} (${user.first_name} ${user.last_name}) ${user.sex == 1 ? "удалена" : "удалён"} из беседы!`);
        });
    } else {
        vk.api.messages.removeChatUser({chat_id: iChatID, user_id: user.id}).then((remove) => {
            if (remove == 1) {
                return message.send(`@id${user.id} (${user.first_name} ${user.last_name}) принудительно ${user.sex == 1 ? "удалена" : "удалён"} из беседы!`);
            } else {
                vk.api.users.get({user_ids: user.id, name_case: 'acc', fields: 'sex'}).then(([user2]) => {
                    return message.send(`Не могу удалить @id${user2.id} (${user2.first_name} ${user2.last_name}), так как ${user2.sex == 1 ? "её" : "его"} нет в беседе.`);
                });
            }
        })
    }
});

也许问题出在错误的检查if (chat.indexOf(user.id) > -1)
由于 chat 返回一组对象。

也许你应该这样做?

vk.api.messages.removeChatUser({
    chat_id: iChatID,
    user_id: user.id
})
    .catch((error) => {
        if (error.code === 15) {
            return 0;
        }

        throw error;
    })
    .then((removed) => {
        if (removed === 1) {
            return message.send(`@id${user.id} (${user.first_name} ${user.last_name}) ${user.sex === 1 ? 'удалена' : 'удалён'} из беседы!`);
        }

        return message.send(`Не могу удалить @id${user.id} (${user.first_name} ${user.last_name}), так как ${user.sex === 1 ? 'её' : 'его'} нет в беседе.`);
    });

由于消息中的情况不同,我第二次使用vk.api.users.get({user_ids: user.id, name_case: 'acc', fields: 'sex'}).then(([user2])

又是莫名其妙的错误,已经出乎意料。

Caught unhandledRejection: { FetchError: network timeout at: https://api.vk.com/method/friends.getRequests?access_token=ТОКЕН&v=5.69
    at Timeout._onTimeout (/home/bots/vk/node_modules/node-fetch/lib/index.js:1272:13)
    at ontimeout (timers.js:478:11)
    at tryOnTimeout (timers.js:302:5)
    at Timer.listOnTimeout (timers.js:262:5)
  message: 'network timeout at: https://api.vk.com/method/friends.getRequests?access_token=ТОКЕН&v=5.69',
  type: 'request-timeout',
  stackframes: 
   [ { file_name: '/home/bots/vk/node_modules/node-fetch/lib/index.js',
       line_number: 1272 },
     { file_name: 'timers.js', line_number: 478 },
     { file_name: 'timers.js', line_number: 302 },
     { file_name: 'timers.js', line_number: 262 } ] }
Caught unhandledRejection: { FetchError: network timeout at: https://api.vk.com/method/friends.getRequests?access_token=ТОКЕН&v=5.69
    at Timeout._onTimeout (/home/bots/vk/node_modules/node-fetch/lib/index.js:1272:13)
    at ontimeout (timers.js:478:11)
    at tryOnTimeout (timers.js:302:5)
    at Timer.listOnTimeout (timers.js:262:5)
  message: 'network timeout at: https://api.vk.com/method/friends.getRequests?access_token=ТОКЕН&v=5.69',
  type: 'request-timeout' }

在这种情况下,机器人处于在线状态,但根本不响应消息。
如果该friends.getRequests 在计时器上,则每30 秒检查一次。

重新启动后,问题就消失了。

版本 VK-IO - 4.0.0-alpha.5

机器人再次掉线,在线,但不响应消息。
据我了解,长轮询掉了

DEBUG=vk-io:updates ,您需要先收集信息。

如果在重新启动应用程序后,bot 响应了一段时间,然后停止,那么很可能是 Long Poll 对所有事情负责,如果你没有搞砸其他任何地方。 最有可能的是,你忘记了对不成功请求的错误处理,Long Poll VK 服务器经常喜欢给出 403、500-503。

同时,重要的是要记住node-fetch将所有服务器响应(即使有错误)传输到 then() 块,从 README 判断:

3xx-5xx 响应不是网络错误,应该在 then ()

因此,例如,该错误可能已潜入此处的某个地方。

可能大错特错,因为库代码我没研究过,但是“忽略”bots最常见的问题是Long Poll的运行不稳定,所以所有的错误都需要在发生时进行处理并重新连接到服务器。

PS然而,最好的选择是尝试将users.get → messages.getChatUsers → messages.removeChatUser → message.send → users.get?代码中的这些顺序请求“打包”为一个execute -request。 从保存请求的角度来看,这将更有效,因为每秒有 3 个请求的已知限制。

将库从版本 3 更新到版本 4 后出现问题。
在此之前,没有任何问题。

2017-12-08T08:27:13.947Z vk-io:updates http -->
2017-12-08T08:27:13.947Z vk-io:updates http <--
2017-12-08T08:27:13.947Z vk-io:updates longpoll update [ 9, -286131                      123, 1, 1512721234 ]
2017-12-08T08:27:13.947Z vk-io:updates http -->
2017-12-08T08:27:13.948Z vk-io:updates http <--
2017-12-08T08:27:13.948Z vk-io:updates longpoll update [ 9, -286131                      123, 1, 1512721234 ]
2017-12-08T08:27:13.948Z vk-io:updates http -->
2017-12-08T08:27:13.948Z vk-io:updates http <--
2017-12-08T08:27:13.948Z vk-io:updates longpoll update [ 9, -286131                      123, 1, 1512721234 ]
2017-12-08T08:27:13.949Z vk-io:updates http -->
2017-12-08T08:27:13.949Z vk-io:updates http <--
2017-12-08T08:27:13.949Z vk-io:updates longpoll update [ 9, -286131                      123, 1, 1512721234 ]
2017-12-08T08:27:13.949Z vk-io:updates http -->
2017-12-08T08:27:13.947Z vk-io:updates http -->
2017-12-08T08:27:13.947Z vk-io:updates http <--
2017-12-08T08:27:13.947Z vk-io:updates longpoll update [ 9, -286131                      123, 1, 1512721234 ]
2017-12-08T08:27:13.947Z vk-io:updates http -->
2017-12-08T08:27:13.948Z vk-io:updates http <--
2017-12-08T08:27:13.948Z vk-io:updates longpoll update [ 9, -286131                      123, 1, 1512721234 ]
2017-12-08T08:27:13.948Z vk-io:updates http -->
2017-12-08T08:27:13.948Z vk-io:updates http <--
2017-12-08T08:27:13.948Z vk-io:updates longpoll update [ 9, -286131                      123, 1, 1512721234 ]
2017-12-08T08:27:13.949Z vk-io:updates http -->
2017-12-08T08:27:13.949Z vk-io:updates http <--
2017-12-08T08:27:13.949Z vk-io:updates longpoll update [ 9, -286131                      123, 1, 1512721234 ]
2017-12-08T08:27:13.949Z vk-io:updates http -->

然后机器人简单地停止并再次开始忽略所有消息。

我已经上传了更新,试试吧。

而现在情况不同了,我写了一条消息,消息出现在bot日志中,但他没有响应命令。

2017-12-08T14:44:02.580Z vk-io:updates http -->
2017-12-08T14:44:02.581Z vk-io:updates http <--
2017-12-08T14:44:02.581Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.581Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.581Z vk-io:updates http -->
2017-12-08T14:44:02.581Z vk-io:updates http <--
2017-12-08T14:44:02.581Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.581Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.581Z vk-io:updates http -->
2017-12-08T14:44:02.582Z vk-io:updates http <--
2017-12-08T14:44:02.582Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.582Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.582Z vk-io:updates http -->
2017-12-08T14:44:02.582Z vk-io:updates http <--
2017-12-08T14:44:02.582Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.583Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.583Z vk-io:updates http -->
2017-12-08T14:44:02.583Z vk-io:updates http <--
2017-12-08T14:44:02.583Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.583Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.583Z vk-io:updates http -->
2017-12-08T14:44:02.584Z vk-io:updates http <--
2017-12-08T14:44:02.584Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.584Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.584Z vk-io:updates http -->
2017-12-08T14:44:02.584Z vk-io:updates http <--
2017-12-08T14:44:02.584Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.584Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.584Z vk-io:updates http -->
2017-12-08T14:44:02.585Z vk-io:updates http <--
2017-12-08T14:44:02.585Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.585Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.585Z vk-io:updates http -->
2017-12-08T14:44:02.585Z vk-io:updates http <--
2017-12-08T14:44:02.585Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.585Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.585Z vk-io:updates http -->
2017-12-08T14:44:02.586Z vk-io:updates http <--
2017-12-08T14:44:02.586Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.586Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.586Z vk-io:updates http -->
2017-12-08T14:44:02.586Z vk-io:updates http <--
2017-12-08T14:44:02.586Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.586Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.587Z vk-io:updates http -->
2017-12-08T14:44:02.587Z vk-io:updates http <--
2017-12-08T14:44:02.587Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.587Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.587Z vk-io:updates http -->
2017-12-08T14:44:02.588Z vk-io:updates http <--
2017-12-08T14:44:02.588Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.588Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.588Z vk-io:updates http -->
2017-12-08T14:44:02.588Z vk-io:updates http <--
2017-12-08T14:44:02.588Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.588Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.588Z vk-io:updates http -->
2017-12-08T14:44:02.589Z vk-io:updates http <--
2017-12-08T14:44:02.589Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.589Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.589Z vk-io:updates http -->
2017-12-08T14:44:02.589Z vk-io:updates http <--
2017-12-08T14:44:02.589Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.589Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.589Z vk-io:updates http -->
2017-12-08T14:44:02.590Z vk-io:updates http <--
2017-12-08T14:44:02.590Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.590Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.590Z vk-io:updates http -->
2017-12-08T14:44:02.590Z vk-io:updates http <--
2017-12-08T14:44:02.590Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.590Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.590Z vk-io:updates http -->
2017-12-08T14:44:02.591Z vk-io:updates http <--
2017-12-08T14:44:02.591Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.591Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.591Z vk-io:updates http -->
2017-12-08T14:44:02.591Z vk-io:updates http <--
2017-12-08T14:44:02.591Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.591Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.591Z vk-io:updates http -->
2017-12-08T14:44:02.592Z vk-io:updates http <--
2017-12-08T14:44:02.592Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.592Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.592Z vk-io:updates http -->
2017-12-08T14:44:02.592Z vk-io:updates http <--
2017-12-08T14:44:02.592Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.592Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.592Z vk-io:updates http -->
2017-12-08T14:44:02.593Z vk-io:updates http <--
2017-12-08T14:44:02.593Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.593Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.593Z vk-io:updates http -->
2017-12-08T14:44:02.593Z vk-io:updates http <--
2017-12-08T14:44:02.593Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.593Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]
2017-12-08T14:44:02.593Z vk-io:updates http -->
2017-12-08T14:44:02.594Z vk-io:updates http <--
2017-12-08T14:44:02.594Z vk-io:updates longpoll update [ 7, 2000000004, 84955 ]
2017-12-08T14:44:02.594Z vk-io:updates longpoll update [ 4,
  84959,
  532497,
  2000000004,
  1512744139,
  '/ping',
  { from: '46199828' } ]

和代码本身?

vk.auth.implicitFlowUser().run().then((response) => {
    vk.setToken(response.token);
    vk.updates.startPolling();
});

vk.updates.on('message', (message, next) => {
    if (!message.text || message.from.id != 2000000004 && message.from.id != ***) return;
    let command = message.text.split(" ")[0].slice(1).toLowerCase()
    if (command === 'ping') {
        vk.api.users.get({user_ids: message.payload.user_id}).then(([user]) => {
            message.send(`Pong!\n@id${message.payload.user_id} (${user.first_name}) 😉`);
        });
    }
});

对不起,答案很长:)

我必须马上说你不应该直接访问上下文属性。 因为对于不同类型的接收数据,它们可能会有所不同,而且您也不需要每次都重新登录,如果您不想获得VK的IP禁令或只是验证码,则不应这样做。
你也可以关注一个简​​单的机器人

根据代码,我会说它是一个工人,问题显然只是直接调用from.id 。 如果需要查看聊天ID,可以使用context.getChatId() !== 4 && context.getUserId() !== ***

无需每次都重新登录

你能告诉我我的授权错误在哪里吗?
以及如何正确地做到这一点。

授权后保存令牌并从配置中使用它是值得的。

轮询问题与c#43有关

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

Zharckov picture Zharckov  ·  13评论

alexey2baranov picture alexey2baranov  ·  8评论

ProgrammingLife picture ProgrammingLife  ·  9评论

Pacmard picture Pacmard  ·  3评论

Jengas picture Jengas  ·  15评论