我正在编写一个 Telegram 机器人,我正在使用 official bot API .我有一个处理请求并发送 200 OK
的 webhook 服务器。每个请求的响应。
在服务器停止之前,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);
提前致谢。
最佳答案
当服务器启动时,您可以记录时间戳,然后使用它与传入消息进行比较 date
值(value)观。如果日期 >= 您开始时的时间戳...则可以处理该消息。
我不确定是否有办法告诉 Telegram 你只对新更新感兴趣,他们的重试机制是一项功能,因此不会错过消息……即使你的机器人离线。
关于telegram-bot - 防止使用网络 Hook 从 Telegram Bot API 获取旧更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31707256/