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代码里还有两处语法错误
use _context=True中间多了空格,正确写法是use_context=True(不过v13+版本已经默认启用上下文,这个参数可以不用加了)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
相关产品推荐
相关产品推荐

