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

Telegram Bot开发报错:'Updater'对象无'dispatcher'属性求助

问题解决:AttributeError: 'Updater' object has no attribute 'dispatcher'

这个报错的核心原因是你使用的python-telegram-bot版本为v20+,但代码是基于旧版本(v12及更早)编写的,新版本对API结构做了重大调整,Updater类不再包含dispatcher属性。

两种解决办法:

1. 降级到兼容旧代码的版本

执行以下命令安装指定版本的库:

pip install python-telegram-bot==12.8

安装完成后,修正原有代码的一处笔误即可正常运行:

from telegram.ext import *
import keys

print('Starting a bot....')

def start_commmand(update, context):
    update.message.reply_text('Hello! Welcome To Store!')

if __name__ == '__main__':
    updater = Updater(keys.token, True)
    dp = updater.dispatcher

    # 修正笔误:add.handler → add_handler
    dp.add_handler(CommandHandler('start', start_commmand))

    updater.start_polling(1.0)
    updater.idle()

2. 修改代码适配新版本(推荐)

新版本采用异步架构,代码需做如下调整:

from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
import keys

print('Starting a bot....')

# 异步函数需添加async关键字
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # 发送消息需添加await关键字
    await update.message.reply_text('Hello! Welcome To Store!')

if __name__ == '__main__':
    # 使用ApplicationBuilder替代Updater创建实例
    application = ApplicationBuilder().token(keys.token).build()
    
    # 直接通过application添加处理器
    application.add_handler(CommandHandler('start', start_command))
    
    # 启动轮询,新版本无需传入1.0参数
    application.run_polling()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 10:15:27