Search code examples
pythonpython-3.xtelegram-botpython-telegram-bot

How to correctly filter messages using MessageHandler from telegram.ext


Im doing my bot using Python3 and telegram-python-bot v.21.0.1 library using modules telegram and telegram.ext. Now Im facing problem with creating custom filter for filter out messages from admin\owner of this chat.

Here is my code:

async def handle_message(update: Update, context: CallbackContext):
    user_status = await context.bot.get_chat_member(update.effective_chat.id, update.effective_user.id)
    status_message = f"User status {update.effective_user.mention_html()} in this chat: {user_status.status}"
    await context.bot.send_message(chat_id=update.effective_chat.id,
                                   text=status_message,
                                   parse_mode='HTML')


class NotPrivilegedUserFilter(filters.BaseFilter):
    def filter(self, message):
        return False


application = Application.builder().token(TOKEN).build()
my_filter = NotPrivilegedUserFilter()
application.add_handler(MessageHandler(filters=my_filter, callback=handle_message))
application.run_polling()

As far as I understand, to create your own filter, you need to create your own subclass from BaseFilter and implement the filter method, which will return a bool. In the example above, everything is simple, it always returns false, just to make sure it works. Then the MessageHandler gets the message and logically should pass it into filters, in this case, into the initialized NotPrivilegedUserFilter as the variable my_filter. However, in runtime, this does not work, there are no errors or exceptions, and my handle_message callback, which processes the message (writes the status of the member who wrote in the chat), is always called.

Actually, the question is - what am I doing wrong? My task is to filter out messages sent by members with the status of ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER.

I've read the documentation, and it seems to be described exactly as I do, plus I've read the docstrings in the library in the BaseFilter and MessageHandler classes. I asked ChatGPT, but it couldn't come up with anything either; I tried all the options he suggested. Also, I debugged with prints (lol) the class NotPrivilegedUserFilter to make sure it gets called during the operation of MessageHandler, but that did not happen. I also looked at code examples on the python-telegram-bot project's GitHub and found no examples of creating custom filters.

I looked at what options are offered on Stack Overflow... Well, you can of course create a function that returns a list of chat admin ids and other privileged users and simply filter by checking the id against the list, but the question is how to write your own filters. It used to be possible to do so, maybe something has changed, or I am misunderstanding something.


Solution

  • You should instead subclass from MessageFilter. This is also detailed in dhe class description of BaseHandler and on this wiki page.


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