KannaX Callback Decorators
In addition to available Pyrogram Decorators, kannax comes with its own custom decorators to handle callback functions.
@kannax.on_cmd
@kannax.on_cmdDecorator for handling messages containing command text and custom flags or command prefixes.
Parameters:
~ $ ~ [Mandatory] ~ $ ~
command: strCommand name to be executed (without a command trigger prefix)about: str | dictDescribe your function, this will be used to parse help strings. This can be a single string or a dictionary containing keys and values as mentioned below.Options Available for about (dict):
header: strTitle of your command[Example]description: strDescribe the working of your command[Example]flags: str | dictMention available flags in your command[Example]options: str | dictMention available options that can be used with your command[Example]types: str | listSpecific types with which your command can be triggered[Example]usage: strSyntax with which user should trigger command[Example]others: strAdditional note that you would like to give[Example]wild card: str | dict | listThis can be used to give customised title and description other than above defaults. Syntax: The title you want to declare should be key of dictionary and value should be a string, or another dictionary or a list.[Example]
{tr}: This option can be used in usage or examples to replace default.prefix of command trigger with custom user definedConfig.CMD_TRIGGER
~ $ ~ [Optional] ~ $ ~
group: intThe group identifier of callable function.[default: 0]name: strA name for your command.[default: '']trigger: strA custom trigger for your command. Useful when you want commands to be triggered only by some specific triggers only.[default: Config.CMD_TRIGGER]filter_me: boolSpecify who can access this command. IfFalseeveryone can access this command else ifTrueonly userbot owner and sudo-users* can access this command.[default: True]allow_private: boolSpecify if your command should work in private chats or not.[default: True]allow_bots: boolSpecify if your command is intended to work for bots chats.[default: True]allow_groups: boolSpecify if your command works in group chats.[default: True]allow_channels: boolSpecify if your command works in channels.[default: True]
All above
allow_*keyword arguments are simply provided to prohibit specific chat types to ensure the proper functioning of your callable function.
only_admins: boolSpecify if your command (i.e. callable function) needs admin privileges in current chat (groups or channel) to be used.[default: False]allow_via_bot: boolkannax supports bot account session too. And since due to some Telegram Restrictions bots can’t trigger some API calls, and in order to avoid BotMethodInvaild error this argument can be passed asTrueto prohibit command execution if the client is set to bot.[default: True]check_client: boolBy passingTruekannax will check if current client is bot or not.[default: False]check_downpath: boolIfTrue, kannax will make sure thatConfig.DOWN_PATHexists.[default: False]check_change_info_perm: boolPassTrueto check if the current kannax client has Change Info Privileges in current chat before further execution of the program.[default: False]check_edit_perm: boolPassTrueto check if current client has Edit Permission in current Channel before further execution.[default: False]check_delete_perm: boolPassTrueto check if current client has Delete Permissions in current chat.[default: False]check_restrict_perm: boolPassTrueto check if current client can Restrict Users in current chat.[default: False]check_invite_perm: boolPassTrueto check if the current client has Privileges to Add Users to export a private invite link, etc…[default: False]check_pin_perm: boolPassTrueto check if the current client has Privileges to Pin Messages in the current chat.[default: False]prefix: strSet a custom prefix to detect flags in a message.[default: '-']del_pre: boolPassTrueto get adictof flags without prefix.[default: False]
[*]: Works only if the command is allowed to be triggered by sudo users viaaddscmd
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 kannax client to your program as on_cmd can be accessed as an attribute of kannax client.
from kannaximport kannax
Now we have imported kannax client. For example let’s make an echo command that will take an input and send it back to the current chat.
from kannax import kannax, Message
@kannax.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 kannax.send_message(message.chat.id, text)
else:
await message.edit("Give some text")
Point to Remember:
Unlike pyrogram decorators,
kannaxdecorators return only one positional argument i.eMessage. Just in case you want to use client methods you can either directly access them via importedkannax(as shown in above example) OR useclientattribute of modifiedMessageclass ofkannax(as shown below)
await message.client.send_message(message.chat.id, text)
# using message.client is recommended as kannax 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 kannax import kannax, Message
@kannax.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:
It should work only in groups.
Can only be triggered by
>prefix.Change
-flag prefix to*.Anyone can use this command.
To do this all we need to do is take the same code and alter some parameters,
# skipping to decorator
@kannax.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 kannax.on_cmd for more examples check out our Plugins in main repo or unofficial plugins repository
@kannax.on_filters
@kannax.on_filtersDecorator for handling filters.
Parameters:
~ $ ~ [Mandatory] ~ $ ~
filters: pyrogram.filtersOne or more pyrogram filters.
~ $ ~ [Optional] ~ $ ~
Same as in on_cmd
on_cmdgroup: intallow_private: boolallow_bots: boolallow_groups: boolallow_channels: boolonly_admins: boolallow_via_bot: boolcheck_client: bool[default: True]check_downpath: boolcheck_change_info_perm: boolcheck_edit_perm: boolcheck_delete_perm: boolcheck_restrict_perm: boolcheck_invite_perm: boolcheck_pin_perm: bool
Examples:
Let’s make a simple program to detect incoming audio and video files in any chat but not in channels. So …
from kannax import filters, Message
@kannax.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 @kannaxsup. So we only need to add an extra filter to filter that chat.
from kannax import filters, Message
@kannax.on_filters(filters.chat(['@kannaxsup']) & (filters.audio | filters.video))
async def my_filter(message: Message):
# rest same
That’s all for kannax.on_filters for more examples check out our Plugins in main repo or unofficial plugins repository
Last updated
Was this helpful?