Python开发Telegram机器人遇TypeError: 'module' object is not callable错误
解决python-telegram-bot中的
TypeError: 'module' object is not callable错误 错误根源
你遇到的问题核心是:telegram.ext.updater是模块而非可调用的类,正确的可实例化类是telegram.ext.Updater(注意首字母大写)。
修复步骤
修正Updater实例化代码
原错误代码:updater = telegram.ext.updater("API_KEY", use_context = True)替换为:
updater = telegram.ext.Updater(Token, use_context=True)同时建议直接使用你已定义的
Token变量,避免重复写API密钥字符串。可选:简化导入方式(让代码更整洁)
可以直接导入需要的类,避免每次写完整的模块路径: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-botv13.x及以上版本,use_context=True是默认参数,可以省略不写。 - 确保你的
API_KEY是从Telegram BotFather获取的有效密钥。
内容的提问来源于stack exchange,提问作者Robert Grootjen
相关产品推荐
相关产品推荐

