pyrogram - 过滤发送给机器人的命令

pyrogram - Filter commands addressed to the bot

如果我使用 filters.command(["my_command"] 之类的东西过滤命令,当我执行命令 /my_command 时,在组中机器人会收到通知,但是如果我将它发送给机器人(例如 /my_command@MyBot) 它不会收到通知。

如何修改过滤器以在两种情况下(独立于机器人名称)得到通知?

谢谢

您需要将每个选项传递给 filter.commands,包括带有机器人名称的变体。


如果您正在寻找更动态的解决方案,您可以使用类似

的方法
commands = [ 'hello', 'world' ]
myBot = 'myBotName'

for i in range(len(commands)):
    commands.append(commands[i] + "@" + myBot)

print(commands)
// ['hello', 'world', 'hello@myBotName', 'world@myBotName']

这将遍历命令列表,以及附有机器人名称的相同命令。

对于更多 copy/paste 逻辑,您可以从 pyrogram 本身检索机器人名称。


如果我们看一下 Pyrogram 的 User class;我们将看到以下数据:

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

[Link to documentation]

这似乎非常适合在我上面的示例中自动执行 myBot 变量。

使用正则表达式

with app:
    bot_username = app.get_me().username

@app.on_message( filters.regex(f'^/my_command($|@{bot_username}$)') )
def handler( client, message ):
    pass