Search code examples
pythontelegram-botpython-telegram-bottelegram-api

how to fix " TypeError: MessageFilter.check_update() missing 1 required positional argument: 'update' "


I'm using python-telegram-bot V20.2 to create somthing like this: Chat and User Selection

I'm having problem handling the event when the user shares a group/channel chat.

here is my code:

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()

After I ran the bot, invoked the \start command, and shared a chat, I encountered this error:

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'

Solution

  • You used filters._Contact, which is a protected class of the filters module. Those are not intended to be used by the user, but are for internal use of the python-telegram-bot library itself. See also PEP8. Please choose one of the filters documented here. I guess you'll want to use filters.StatusUpdate.CHAT_SHARED.


    Disclaimer: I'm currently the maintainer of python-telegram-bot.