Skip to content

Filters

moonlygram.ext.filters

Message filters for MessageHandler.

A filter decides whether a handler runs for a given Message. Filters are Filter instances and combine with & (and), | (or) and ~ (not)::

filters.command("ban") & filters.group

Any callable Message -> bool also works as a predicate, but only Filter instances support the combinator operators.

Filter

A predicate over a Message that supports &, | and ~.

Args: predicate: Callable returning whether the message matches. name: Label used in repr, for debugging.

command

command(*names: str) -> Filter

Match /name or /name@botusername at the start of a message.

Pass one or more command names; the filter matches any of them.

regex

regex(pattern: str | Pattern[str]) -> Filter

Match messages whose text the regular expression finds (re.search).

caption_regex

caption_regex(pattern: str | Pattern[str]) -> Filter

Match messages whose caption the regular expression finds (re.search).

user

user(*user_ids: int) -> Filter

Match messages sent by any of the given user ids.

chat

chat(*chat_ids: int) -> Filter

Match messages in any of the given chat ids.

username

username(*usernames: str) -> Filter

Match messages whose sender has one of the usernames (with or without @).

chat_username

chat_username(*usernames: str) -> Filter

Match messages whose chat has one of the usernames (with or without @).

forwarded_from

forwarded_from(*ids: int) -> Filter

Match messages forwarded from any of the given user or chat ids.

Matches the original sender recorded in forward_origin, whether that is a user, a sender chat, or a channel.

entity

entity(*types: str) -> Filter

Match messages carrying a text entity of any of the given types.

Types are the Bot API entity strings, such as "url", "mention", or "hashtag".

mime_type

mime_type(*types: str) -> Filter

Match a document, audio, video, animation, or voice with one of the mime types.