0

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

3
  • filters.command(["my_command", "my_command@myBot"] Commented Nov 11, 2021 at 14:30
  • ok, but I prefer not to write the bot name in the code (I have lots of bots with the same code), is there a way to filter it without hard-coding the name? Commented Nov 11, 2021 at 14:41
  • You could write a loop that will duplicate each command with the name of the bot Commented Nov 11, 2021 at 14:41

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

Ok thanks, so the best solution would be to get the bot name from pyrogram and create the string dynamically
I'm not quite familiar with pyrogram, so I can't tell you how exactly. It is available though the telegram Api so there must be a option for it in pyrogram.
Please see my edit @yuko, I guess the user.username should be wat you're looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.