使用python-telegram-bot库报错:缺失update_queue必填参数
解决Telegram Bot初始化报错问题
问题原因
你遇到的TypeError是因为安装的python-telegram-bot版本为v20+,该版本完全重构了API,旧版本(v13及更早)的Updater初始化写法不再兼容。v20+的Updater不再支持直接传入token作为参数,官方更推荐使用新的Application类替代旧的Updater和Dispatcher。
两种解决方案
方案1:降级到兼容旧代码的版本
如果想继续使用现有代码,只需将python-telegram-bot降级到v13.x的最后一个稳定版本:
pip uninstall python-telegram-bot pip install python-telegram-bot==13.7
降级后你的原有代码就能正常运行。
方案2:改用v20+的新API写法
如果想使用最新版本的库,修改代码为以下异步风格的写法:
from telegram import Update from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes TOKEN = "S*************************************nd" async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): await update.message.reply_text("hello!") if __name__ == '__main__': # 创建应用实例 application = ApplicationBuilder().token(TOKEN).build() # 添加start命令处理器 start_handler = CommandHandler('start', start) application.add_handler(start_handler) # 启动轮询 application.run_polling()
注意:新版本的命令处理函数必须是异步函数,需要用async定义,并在发送消息时使用await关键字。
内容的提问来源于stack exchange,提问作者M Kh
相关产品推荐
相关产品推荐

