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

Telegram Bot报错:module 'telegram.ext.filters'无text属性求助

解决Telegram Bot中AttributeError: module 'telegram.ext.filters' has no attribute 'text'错误

这个错误是因为python-telegram-bot不同版本的过滤器(filters)API结构差异导致的,代码写法与安装的库版本不匹配。以下是针对不同版本的修复方案:

方案1:适配python-telegram-bot v13.x版本

若你回退到了v13.x版本,需修改两处代码:

  1. 调整导入语句:将filters替换为大写的Filters
    from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
    
  2. 修改过滤器调用:把filters.text和filters.command改为Filters.text和Filters.command
    updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_payment))
    

方案2:适配python-telegram-bot v14.x及以上版本

若你使用最新版本,需调整导入和过滤器写法:

  1. 调整导入语句:从telegram模块导入filters,而非telegram.ext
    from telegram import Update, filters
    from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext
    
  2. 修改过滤器调用:使用大写的filters.TEXT和filters.COMMAND(v14+版本的过滤器常量为大写)
    updater.dispatcher.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_payment))
    

    注:v14+版本推荐使用Application替代Updater,更符合新API规范,示例代码如下:

    def main() -> None:
        application = Application.builder().token(TELEGRAM_TOKEN).build()
        application.add_handler(CommandHandler("start", start))
        application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_payment))
        application.run_polling()
    

额外排查点

  • 执行pip show python-telegram-bot确认当前安装的版本,确保代码写法与版本匹配
  • 检查是否存在多版本库冲突,建议使用虚拟环境隔离依赖
  • 确认没有拼写错误(比如大小写、模块路径)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 15:04:57