解决python-telegram-bot V20.2中MessageFilter.check_update()参数缺失错误
python-telegram-bot V20.2 聊天分享事件处理的过滤器错误解决
问题场景
我正在使用python-telegram-bot V20.2开发类似Telegram官方「聊天与用户选择」的功能,在处理用户分享群组/频道聊天的事件时遇到问题。以下是我的代码:
from telegram import Update, KeyboardButton, KeyboardButtonRequestChat, ReplyKeyboardMarkup from telegram.ext import ApplicationBuilder, CommandHandler, CallbackContext, MessageHandler, filters bot_token = "bot_token" # python-telegram-bot setup application = ApplicationBuilder().token(bot_token).build() # python-telegram-bot command handler async def start(update: Update, context: CallbackContext): request_chat = KeyboardButtonRequestChat(1, True) keyboard = [[KeyboardButton("Request Chat", request_chat=request_chat)]] reply_markup = ReplyKeyboardMarkup(keyboard) await update.message.reply_text('Please share your contact details', reply_markup=reply_markup) async def request_contact(update: Update, context: CallbackContext): chat = update.message.chat_shared if chat.request_id == 1: print(chat.chat_id) application.add_handler(CommandHandler('start', start)) application.add_handler(MessageHandler(filters._Contact, request_contact)) #here is the problem! print('Running...') application.run_polling()
报错信息
运行机器人、调用/start命令并分享聊天后,出现如下错误:
No error handlers are registered, logging exception. Traceback (most recent call last): File "/home/amin/anaconda3/envs/telegramBot/lib/python3.11/site-packages/telegram/ext/_application.py", line 1099, in process_update check = handler.check_update(update) # Should the handler handle this update? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/amin/anaconda3/envs/telegramBot/lib/python3.11/site-packages/telegram/ext/_messagehandler.py", line 99, in check_update return self.filters.check_update(update) or False ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: MessageFilter.check_update() missing 1 required positional argument: 'update'
错误原因与解决方案
错误原因
- 误用内部类过滤器:
filters._Contact是库的内部实现类(以下划线开头),不允许直接在业务代码中实例化使用,必须使用库提供的公开过滤器常量。 - 过滤器类型不匹配:当前场景是处理聊天分享事件,但误用了联系人相关的过滤器,导致事件捕获逻辑完全错误。
修正后的代码
将过滤器替换为filters.CHAT_SHARED,同时可以把处理函数名改为更贴合功能的request_chat:
from telegram import Update, KeyboardButton, KeyboardButtonRequestChat, ReplyKeyboardMarkup from telegram.ext import ApplicationBuilder, CommandHandler, CallbackContext, MessageHandler, filters bot_token = "bot_token" # python-telegram-bot setup application = ApplicationBuilder().token(bot_token).build() # python-telegram-bot command handler async def start(update: Update, context: CallbackContext): request_chat = KeyboardButtonRequestChat(1, True) keyboard = [[KeyboardButton("Request Chat", request_chat=request_chat)]] reply_markup = ReplyKeyboardMarkup(keyboard) # 修正提示文案匹配聊天分享场景 await update.message.reply_text('Please share a chat', reply_markup=reply_markup) async def request_chat(update: Update, context: CallbackContext): chat = update.message.chat_shared if chat.request_id == 1: print(f"Shared chat ID: {chat.chat_id}") application.add_handler(CommandHandler('start', start)) # 替换为正确的聊天分享过滤器 application.add_handler(MessageHandler(filters.CHAT_SHARED, request_chat)) print('Running...') application.run_polling()
关键说明
filters.CHAT_SHARED是python-telegram-bot提供的公开过滤器,专门用于捕获用户分享聊天(群组/频道)的消息事件。- 以下划线开头的类均为库内部实现细节,禁止在业务代码中直接调用,避免出现实例化或版本兼容问题。
内容的提问来源于stack exchange,提问作者Amin Sanei
相关产品推荐
相关产品推荐

