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

python-telegram-bot库Updater类报错:TypeError意外关键字参数'token'

解决python-telegram-bot Updater实例化TypeError问题

错误原因

该问题是python-telegram-bot版本迭代导致的API变更:

  • v12及更早版本:Updater类支持直接传入token关键字参数
  • v13.x版本:token参数被移除,需通过bot参数传入令牌或Bot实例
  • v20+版本:Updater已被弃用,官方推荐使用Application类替代

解决方案

情况1:使用v13.x版本

修改实例化代码,将token参数替换为bot参数:

from telegram.ext import Updater

TOKEN = "你的机器人令牌"
# 方式1:直接传入令牌字符串给bot参数
updater = Updater(bot=TOKEN, use_context=True)

# 方式2:先创建Bot实例再传入(更灵活)
from telegram import Bot
bot = Bot(token=TOKEN)
updater = Updater(bot=bot, use_context=True)

情况2:使用v20+版本

弃用Updater,改用Application类实现功能:

from telegram.ext import ApplicationBuilder

TOKEN = "你的机器人令牌"
# 构建Application实例
application = ApplicationBuilder().token(TOKEN).build()

# 后续添加处理器、启动服务的示例(替代原Updater的用法)
# from telegram.ext import CommandHandler
# def start(update, context):
#     update.message.reply_text('Hello!')
# application.add_handler(CommandHandler("start", start))
# application.run_polling()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 07:35:14