使用网络钩子防止从 Telegram Bot API 获取旧更新

5 telegram-bot

我正在编写一个 Telegram bot,我正在使用官方的 bot API。我有一个 webhook 服务器来处理请求并200 OK为每个请求发送响应。

在服务器停止之前,webhook 已分离,因此 Telegram 不再发送更新。但是,每当我打开机器人并再次设置 webhook URL 时,Telegram 就会开始用旧更新淹没 webhook 服务器。

有什么方法可以防止这种情况发生,而无需/getUpdates反复请求直到我到达最后一次更新?

这是我的代码外观的高度简化版本:

var http = require('http'),
    unirest = require('unirest'),
    token = '***';

// Attach the webhook
unirest.post('https://api.telegram.org/bot' + token + '/setWebhook')
    .field('url', 'https://example.com/api/update')
    .end();

process.on('exit', function() {
    // Detach the webhook
    unirest.post('https://api.telegram.org/bot' + token + '/setWebhook')
        .field('url', '')
        .end();
});

// Handle requests
var server = http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' })
    res.end('Thanks!');
});

server.listen(80);
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Chr*_*and 3

当服务器启动时,您可以记录时间戳,然后使用它来与传入消息date值进行比较。如果日期 >= 开始时的时间戳...则可以处理该消息。

我不确定是否有办法告诉 Telegram 您只对新更新感兴趣,他们的重试机制是一项功能,这样即使您的机器人处于离线状态,也不会错过消息。