You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telegram bot长轮询设timeout=3600仍50秒返回,如何延长轮询时长?

Telegram Bot API getUpdates 长轮询超时超过50秒设置方案

你遇到的50秒强制返回是Telegram Bot API的官方限制:getUpdates接口的timeout参数最大有效值为50,传入超过50的数值会被服务端自动截断为50秒,这一限制和客户端的libcurl配置无关,无法通过修改请求参数突破。

要实现等效的1小时长轮询效果,可以按以下方案调整:

  • 调用getUpdates时timeout参数固定填50,达到服务端允许的最大单次轮询时长
  • 每次请求结束(无论是拿到新更新还是50秒无更新返回)后,立刻发起下一次getUpdates请求,连续的短轮询逻辑上等效于长轮询,不会遗漏更新,也能实现长期等待的效果
  • 调整libcurl的超时配置,将CURLOPT_TIMEOUT参数设置为略大于50秒,避免客户端先于服务端断开连接,参考代码如下:
CURL *curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_URL, "https://api.telegram.org/bot<你的bot_token>/getUpdates");
  // libcurl超时设为60秒,留足够余量避免提前断开
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60L);
  // 接口timeout参数填服务端允许的最大值50
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "timeout=50");
  CURLcode res = curl_easy_perform(curl);
  curl_easy_cleanup(curl);
}

内容的提问来源于stack exchange,提问作者simonepernice

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 21:15:10