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

Python Telegram Bot脚本TypeError:Updater初始化遇意外关键字参数

解决Telegram Bot脚本的TypeError: Updater.init() got an unexpected keyword argument 'token'
  • 版本不匹配是核心问题
    你使用的python-telegram-bot如果是v13及以上版本,Updater类的初始化逻辑已经彻底变更,不再支持直接传入token关键字参数。v13+版本官方推荐用Application类构建bot实例,示例代码:

    from telegram.ext import ApplicationBuilder
    
    def main():
        # 替换成你的真实Bot Token
        application = ApplicationBuilder().token("your_bot_token_here").build()
        # 启动轮询
        application.run_polling()
    
    if __name__ == '__main__':
        main()
    

    要是非得用Updater,可以从Application实例中获取:

    application = ApplicationBuilder().token("your_bot_token_here").build()
    updater = application.updater
    
  • 代码里还有两处语法错误

    1. use _context=True中间多了空格,正确写法是use_context=True(不过v13+版本已经默认启用上下文,这个参数可以不用加了)
    2. updater .dispatcher中间有空格,正确写法是updater.dispatcher
  • 如果要兼容旧版本
    如果你需要继续使用v12及以下版本的库,可以重新安装指定版本:

    pip install python-telegram-bot==12.8
    

    修正语法错误后,原来的初始化代码就能正常运行:

    updater = Updater(token='your_bot_token_here', use_context=True)
    dispatcher = updater.dispatcher
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 03:02:37