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

Telegram Bot启动后触发telegram.error.TimedOut错误,求排查原因

问题:Telegram Bot启动后出现TimedOut错误,排查网络还是代码问题?

运行Telegram Bot程序时,启动几秒后出现telegram.error.TimedOut错误,请问这是网络连接问题还是代码错误?

代码示例

from telegram.ext import *
def start_command(update, context):
    update.massage.reply_text("Hi. I'm a test bot")

def help_command(update, context):
    update.massage.reply_text("if you need help! search on Google")

def main():
    app = Application.builder().token('your token').build()
    
    app.add_handler(CommandHandler("start", start_command))
    app.add_handler(CommandHandler("help", help_command))
   
    app.run_polling()
    app.idle()

if __name__ == '__main__':
    main()

错误信息

File "C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\telegram\request\_httpxrequest.py", line 200, in do_request
    raise TimedOut from err
telegram.error.TimedOut: Timed out

问题分析与解决方法

1. 代码层面的错误

你的代码里存在明显拼写错误:update.massage应为update.message(注意是message而非massage)。这个错误不会直接导致启动时的TimedOut,但当用户触发/start或/help命令时,会抛出属性错误,导致Bot无法正常响应,需要优先修正。

2. TimedOut错误的核心原因

telegram.error.TimedOut本质是网络请求超时,主要由以下网络问题导致:

  • 所在网络无法正常访问Telegram服务器(部分地区需特殊网络配置才能连接Telegram服务)
  • 网络延迟过高,超过了默认请求超时时间
  • 本地防火墙、杀毒软件或代理设置阻断了Bot与Telegram服务器的通信

3. 解决步骤

  • 修正代码拼写错误:将所有update.massage替换为update.message
  • 排查网络连通性:确认当前网络能正常访问Telegram服务,必要时调整网络配置
  • 延长超时时间:构建Application时增加超时参数,避免因网络延迟触发超时:
    app = Application.builder().token('your token').read_timeout(30).connect_timeout(30).build()
    
  • 配置代理(若需):如果依赖代理访问Telegram,需在代码中配置代理:
    from telegram.request import HTTPXRequest
    request = HTTPXRequest(proxy_url="http://你的代理地址:端口")
    app = Application.builder().token('your token').request(request).build()
    

内容的提问来源于stack exchange,提问作者M.Sepehr Shahriyari

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 16:37:12