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

Python开发Telegram机器人遇TypeError: 'module' object is not callable错误

解决python-telegram-bot中的TypeError: 'module' object is not callable错误

错误根源

你遇到的问题核心是:telegram.ext.updater是模块而非可调用的类,正确的可实例化类是telegram.ext.Updater(注意首字母大写)。

修复步骤

  1. 修正Updater实例化代码
    原错误代码:

    updater = telegram.ext.updater("API_KEY", use_context = True)
    

    替换为:

    updater = telegram.ext.Updater(Token, use_context=True)
    

    同时建议直接使用你已定义的Token变量,避免重复写API密钥字符串。

  2. 可选:简化导入方式(让代码更整洁)
    可以直接导入需要的类,避免每次写完整的模块路径:

    from telegram.ext import Updater, CommandHandler
    

    后续代码中就可以直接用Updater和CommandHandler,无需前缀。

完整修正后代码

from telegram.ext import Updater, CommandHandler

Token = "你的API_KEY"

updater = Updater(Token, use_context=True)
dispatcher = updater.dispatcher

def start(update, context):
    update.message.reply_text("Hello! Thanks for messaging me!")

def help(update, context):
    update.message.reply_text(
        """
        /aboutme -> More info about me.
        /jobs -> See all my open jobs!
        /myinfo -> My email information.
        /latamsalaries -> Current LATAM salaries for devs.
        """
    )

def aboutme(update, context):
    update.message.reply_text("My name is Robert Grootjen, and I'm a technical headhunter! My mission is to connect the best talent in Latin America with topnotch companies in the United States and Canada.")

def jobs(update, context):
    update.message.reply_text("")

def myinfo(update, context):
    update.message.reply_text("Linkedin: https://www.linkedin.com/in/robert-grootjen-08a10b15a/")

def latamsalaries(update, context):
    update.message.reply_text("Jr - $1500 - $3000 USD")

dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('aboutme', aboutme))
dispatcher.add_handler(CommandHandler('jobs', jobs))
dispatcher.add_handler(CommandHandler('myinfo', myinfo))
dispatcher.add_handler(CommandHandler('latamsalaries', latamsalaries))

updater.start_polling()
updater.idle()

额外说明

  • 如果你安装的是python-telegram-bot v13.x及以上版本,use_context=True是默认参数,可以省略不写。
  • 确保你的API_KEY是从Telegram BotFather获取的有效密钥。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 15:40:23