skip to Main Content

If I filter a command using something like filters.command(["my_command"], in groups the bot gets notified when I execute the command /my_command, but if I address it to the bot (e.g. /my_command@MyBot) it won’t get notified.

How can I modify the filter to get notified in both cases (independently on the bot name)?

Thanks

2

Answers


  1. You’ll need to pass each option to filter.commands, including the variants with the bot name.


    If you’re looking for a more dynamic solution, you can use something like

    commands = [ 'hello', 'world' ]
    myBot = 'myBotName'
    
    for i in range(len(commands)):
        commands.append(commands[i] + "@" + myBot)
    
    print(commands)
    // ['hello', 'world', 'hello@myBotName', 'world@myBotName']
    

    That will loop over a list of commands, and the same command with the bot name attached.

    For even more copy/paste logic, you can retrieve the bot name from pyrogram itself.


    If we take a look at Pyrogram’s User class; we’ll see the following data:

    • username (str, optional) – User’s or bot’s username.

    [Link to documentation]

    This seems like a perfect fit to automate the myBot variable in my example above.

    Login or Signup to reply.
  2. use regex

    with app:
        bot_username = app.get_me().username
    
    @app.on_message( filters.regex(f'^/my_command($|@{bot_username}$)') )
    def handler( client, message ):
        pass
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search