Command#

class aiogram.filters.command.Command(*, commands: Union[Sequence[Union[str, re.Pattern]], str, re.Pattern], commands_prefix: str = '/', commands_ignore_case: bool = False, commands_ignore_mention: bool = False, command_magic: Optional[magic_filter.magic.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 the text.

commands: Union[Sequence[CommandPatternType], CommandPatternType]#

List of commands (string or compiled regexp patterns)

commands_prefix: str#

Prefix for command. Prefix is always is single char but here you can pass all of allowed prefixes, for example: "/!" will work with commands prefixed by "/" or "!".

commands_ignore_case: bool#

Ignore case (Does not work with regexp, use flags instead)

commands_ignore_mention: bool#

Ignore bot mention. By default bot can not handle commands intended for other bots

command_magic: Optional[MagicFilter]#

Validate command object via Magic filter after all checks done

update_handler_flags(flags: Dict[str, Any]) None[source]#
extract_command(text: str) aiogram.filters.command.CommandObject[source]#
validate_prefix(command: aiogram.filters.command.CommandObject) None[source]#
async validate_mention(bot: Bot, command: CommandObject) None[source]#
validate_command(command: aiogram.filters.command.CommandObject) aiogram.filters.command.CommandObject[source]#
async parse_command(text: str, bot: Bot) CommandObject[source]#

Extract command from the text and validate

Parameters
  • text

  • bot

Returns

do_magic(command: aiogram.filters.command.CommandObject) Any[source]#
class Config[source]#
arbitrary_types_allowed = True#

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#

  1. Filter single variant of commands: Command(commands=["start"]) or Command(commands="start")

  2. Handle command by regexp pattern: Command(commands=[re.compile(r"item_(\d+)")])

  3. Match command by multiple variants: Command(commands=["item", re.compile(r"item_(\d+)")])

  4. Handle commands in public chats intended for other bots: Command(commands=["command"], commands_ignore_mention=True)

Warning

Command cannot include spaces or any whitespace

Allowed handlers#

Allowed update types for this filter:

  • message

  • edited_message