Userge Callback Decorators

In addition to available Pyrogram Decorators, Userge comes with its own custom decorators to handle callback functions.

@userge.on_cmd

Decorator for handling messages containing command text and custom flags or command prefixes.

Parameters:

~ $ ~ [Mandatory] ~ $ ~

~ $ ~ [Optional] ~ $ ~

All above allow_* keyword arguments are simply provided to prohibit specific chat types to ensure the proper functioning of your callable function.

[*]: Works only if the command is allowed to be triggered by sudo users via addscmd

Examples:

Here, we will be explaining how to use on_cmd briefly with some example which will try to cover almost all above mentioned parameters.

Firstly import userge client to your program as on_cmd can be accessed as an attribute of userge client.


from userge import userge

Now we have imported userge client. For example let’s make an echo command that will take an input and send it back to the current chat.


from userge import userge, Message

@userge.on_cmd("echo", about={
    'header': "Echo",
    'description': "Just give me some text and i will send it back",
    'usage': '{tr}echo [some text]',
    'examples': '{tr}echo Hi'})
async def echo(message: Message):  # Message is just for type hinting 
    text = message.input_str
    if text:
        await userge.send_message(message.chat.id, text)
    else:
        await message.edit("Give some text")

Point to Remember:


await message.client.send_message(message.chat.id, text)
# using message.client is recommended as userge support dual client (i.e user client and bot client)
# by using this method it ensures that the correct client make requests to API

Let’s say, now we need our command to take input from the replied message when a specific flag is passed then,


from userge import userge, Message

@userge.on_cmd("echo", about={
    'header': "Echo",
    'description': "Give any text i will repeat it",
    'flags': {'-r': "I will take text from replied message if u send command with this flag"},
    'usage': "{tr}echo [flag (optional)]",
    'examples': ['{tr}echo Hi', '{tr}echo -r']})
async def echo(message: Message):
    text = message.input_str
    if '-r' in message.flags and message.reply_to_message:
        text = message.reply_to_message.text
    # ... rest of the code for echo.

However, now we want the following modification in echo command:

To do this all we need to do is take the same code and alter some parameters,


# skipping to decorator 
@userge.on_cmd("echo", about={"header": "Echo"  # and so on ...
    },
    # To make it work only in groups we will have to disable channels, bot and private chats. 
    allow_private=False, allow_channels=False, allow_bots=False,
    # now to change trigger prefix
    trigger='>',
    # change flag prefix
    prefix='*',
    # allow all users to trigger command
    filter_me=False)
async def echo(message: Message):
    # ... rest of the code

That’s all for userge.on_cmd for more examples check out our Plugins in main repo or unofficial plugins repository

@userge.on_filters

Decorator for handling filters.

Parameters:

~ $ ~ [Mandatory] ~ $ ~

~ $ ~ [Optional] ~ $ ~

Same as in on_cmd

Examples:

Let’s make a simple program to detect incoming audio and video files in any chat but not in channels. So …


from userge import filters, Message

@userge.on_filters(filters.audio | filters.video, allow_channels=False)
async def my_filter(message: Message):
    if message.audio:
        m_type = "audio"
    else:
        m_type = "video"
    await message.reply(f"{m_type} detected!")

Now let’s optimize it for a specific chat. Let say I want to filter from @usergeot. So we only need to add an extra filter to filter that chat.


from userge import filters, Message

@userge.on_filters(filters.chat(['@usergeot']) & (filters.audio | filters.video))
async def my_filter(message: Message):
    # rest same

That’s all for userge.on_filters for more examples check out our Plugins in main repo or unofficial plugins repository