pythontelegramaiogram

How to make aiogram 3 filter to enable in handler only one command or without any command execution


Need to make a dispatcher filter to execute a code only if message.text has inside a specified command or doesn't have any command.

I have tried this and many others combinations of aiogram 3 F:

@dp.message(F.text and (~(F.entities[...].type == MessageEntityType.BOT_COMMAND) or F.text.startswith("/mycommand")))

but it doesn't work properly.

Any suggestions? How to mix F filter parameters correctly?


Solution

  • from aiogram.filters import Command, or_f
    
    
    @dp.message(or_f(Command("mycommand"), ~F.text.startswith('/')))
    

    The filter is composed using the or_f function, which combines multiple filters with a logical OR operator. The filters applied are:

    Overall, this filter is designed to capture messages that either match the command "mycommand" or are not traditional command messages.