Command#
- class aiogram.filters.command.Command(*values: Union[str, Pattern, BotCommand], commands: Optional[Union[Sequence[Union[str, Pattern, BotCommand]], str, Pattern, BotCommand]] = None, prefix: str = '/', ignore_case: bool = False, ignore_mention: bool = False, magic: Optional[MagicFilter] = None)[source]#
This filter can be helpful for handling commands from the text messages.
Works only with
aiogram.types.message.Message
events which have thetext
.- __init__(*values: Union[str, Pattern, BotCommand], commands: Optional[Union[Sequence[Union[str, Pattern, BotCommand]], str, Pattern, BotCommand]] = None, prefix: str = '/', ignore_case: bool = False, ignore_mention: bool = False, magic: Optional[MagicFilter] = None)[source]#
List of commands (string or compiled regexp patterns)
- Parameters:
prefix – Prefix for command. Prefix is always a single char but here you can pass all of allowed prefixes, for example:
"/!"
will work with commands prefixed by"/"
or"!"
.ignore_case – Ignore case (Does not work with regexp, use flags instead)
ignore_mention – Ignore bot mention. By default, bot can not handle commands intended for other bots
magic – Validate command object via Magic filter after all checks done
When filter is passed the aiogram.filters.command.CommandObject
will be passed to the handler argument command
- class aiogram.filters.command.CommandObject(prefix: str = '/', command: str = '', mention: Optional[str] = None, args: Optional[str] = None, regexp_match: Optional[Match[str]] = None, magic_result: Optional[Any] = None)[source]#
Instance of this object is always has command and it prefix. Can be passed as keyword argument command to the handler
- prefix: str = '/'#
Command prefix
- command: str = ''#
Command without prefix and mention
- mention: Optional[str] = None#
Mention (if available)
- args: Optional[str] = None#
Command argument
- regexp_match: Optional[Match[str]] = None#
Will be presented match result if the command is presented as regexp in filter
- magic_result: Optional[Any] = None#
- property mentioned: bool#
This command has mention?
- property text: str#
Generate original text from object
Usage#
Filter single variant of commands:
Command("start")
Handle command by regexp pattern:
Command(re.compile(r"item_(d+)"))
Match command by multiple variants:
Command("item", re.compile(r"item_(d+)"))
Handle commands in public chats intended for other bots:
Command("command", ignore_mention=True)
Use
aiogram.types.bot_command.BotCommand
object as command referenceCommand(BotCommand(command="command", description="My awesome command")
Warning
Command cannot include spaces or any whitespace
Allowed handlers#
Allowed update types for this filter:
message
edited_message