Filters

class pyrogram.Filters

This class provides access to all library-defined Filters available in Pyrogram.

The Filters listed here are intended to be used with the MessageHandler only. At the moment, if you want to filter updates coming from different Handlers you have to create your own filters with Filters.create() and use them in the same way.

create(func: callable, **kwargs) → type

Use this method to create a Filter.

Custom filters give you extra control over which updates are allowed or not to be processed by your handlers.

Parameters:
  • name (str) – Your filter’s name. Can be anything you like.
  • func (callable) – A function that accepts two arguments (filter, update) and returns a Boolean: True if the update should be handled, False otherwise. The “update” argument type will vary depending on which Handler is coming from. For example, in a MessageHandler the update type will be a Message; in a CallbackQueryHandler the update type will be a CallbackQuery. Your function body can then access the incoming update and decide whether to allow it or not.
  • **kwargs (any, optional) – Any keyword argument you would like to pass. Useful for custom filters that accept parameters (e.g.: Filters.command(), Filters.regex()).
bot = <pyrogram.client.filters.filters.Bot object>

Filter messages coming from bots

incoming = <pyrogram.client.filters.filters.Incoming object>

Filter incoming messages.

outgoing = <pyrogram.client.filters.filters.Outgoing object>

Filter outgoing messages.

text = <pyrogram.client.filters.filters.Text object>

Filter text messages.

reply = <pyrogram.client.filters.filters.Reply object>

Filter messages that are replies to other messages.

forwarded = <pyrogram.client.filters.filters.Forwarded object>

Filter messages that are forwarded.

caption = <pyrogram.client.filters.filters.Caption object>

Filter media messages that contain captions.

edited = <pyrogram.client.filters.filters.Edited object>

Filter edited messages.

audio = <pyrogram.client.filters.filters.Audio object>

Filter messages that contain Audio objects.

document = <pyrogram.client.filters.filters.Document object>

Filter messages that contain Document objects.

photo = <pyrogram.client.filters.filters.Photo object>

Filter messages that contain Photo objects.

sticker = <pyrogram.client.filters.filters.Sticker object>

Filter messages that contain Sticker objects.

animation = <pyrogram.client.filters.filters.GIF object>

Filter messages that contain Animation objects.

video = <pyrogram.client.filters.filters.Video object>

Filter messages that contain Video objects.

voice = <pyrogram.client.filters.filters.Voice object>

Filter messages that contain Voice note objects.

video_note = <pyrogram.client.filters.filters.Voice object>

Filter messages that contain VideoNote objects.

contact = <pyrogram.client.filters.filters.Contact object>

Filter messages that contain Contact objects.

location = <pyrogram.client.filters.filters.Location object>

Filter messages that contain Location objects.

venue = <pyrogram.client.filters.filters.Venue object>

Filter messages that contain Venue objects.

private = <pyrogram.client.filters.filters.Private object>

Filter messages sent in private chats.

group = <pyrogram.client.filters.filters.Group object>

Filter messages sent in group or supergroup chats.

channel = <pyrogram.client.filters.filters.Channel object>

Filter messages sent in channels.

new_chat_members = <pyrogram.client.filters.filters.NewChatMembers object>

Filter service messages for new chat members.

left_chat_member = <pyrogram.client.filters.filters.LeftChatMember object>

Filter service messages for members that left the chat.

new_chat_title = <pyrogram.client.filters.filters.NewChatTitle object>

Filter service messages for new chat titles.

new_chat_photo = <pyrogram.client.filters.filters.NewChatPhoto object>

Filter service messages for new chat photos.

delete_chat_photo = <pyrogram.client.filters.filters.DeleteChatPhoto object>

Filter service messages for deleted photos.

group_chat_created = <pyrogram.client.filters.filters.GroupChatCreated object>

Filter service messages for group chat creations.

supergroup_chat_created = <pyrogram.client.filters.filters.SupergroupChatCreated object>

Filter service messages for supergroup chat creations.

channel_chat_created = <pyrogram.client.filters.filters.ChannelChatCreated object>

Filter service messages for channel chat creations.

migrate_to_chat_id = <pyrogram.client.filters.filters.MigrateToChatId object>

Filter service messages that contain migrate_to_chat_id.

migrate_from_chat_id = <pyrogram.client.filters.filters.MigrateFromChatId object>

Filter service messages that contain migrate_from_chat_id.

pinned_message = <pyrogram.client.filters.filters.PinnedMessage object>

Filter service messages for pinned messages.

reply_keyboard = <pyrogram.client.filters.filters.ReplyKeyboard object>

Filter messages containing reply keyboard markups

inline_keyboard = <pyrogram.client.filters.filters.InlineKeyboard object>

Filter messages containing inline keyboard markups

mentioned = <pyrogram.client.filters.filters.Mentioned object>

Filter messages containing mentions

service = <pyrogram.client.filters.filters.Service object>

Filter service messages. A service message contains any of the following fields set

  • left_chat_member
  • new_chat_title
  • new_chat_photo
  • delete_chat_photo
  • group_chat_created
  • supergroup_chat_created
  • channel_chat_created
  • migrate_to_chat_id
  • migrate_from_chat_id
  • pinned_message
media = <pyrogram.client.filters.filters.Media object>

Filter media messages. A media message contains any of the following fields set

  • audio
  • document
  • photo
  • sticker
  • video
  • animation
  • voice
  • video_note
  • contact
  • location
  • venue
static command(command: str, prefix: str = '/', separator: str = ' ', case_sensitive: bool = False)

Filter commands, i.e.: text messages starting with “/” or any other custom prefix.

Parameters:
  • command (str | list) – The command or list of commands as string the filter should look for. Examples: “start”, [“start”, “help”, “settings”]. When a message text containing a command arrives, the command itself and its arguments will be stored in the command field of the Message.
  • prefix (str | list, optional) – A prefix or a list of prefixes as string the filter should look for. Defaults to “/” (slash). Examples: “.”, “!”, [“/”, “!”, “.”]. Can be None or “” (empty string) to allow commands with no prefix at all.
  • separator (str, optional) – The command arguments separator. Defaults to ” ” (white space). Examples: /start first second, /start-first-second, /start.first.second.
  • case_sensitive (bool, optional) – Pass True if you want your command(s) to be case sensitive. Defaults to False. Examples: when True, command=”Start” would trigger /Start but not /start.
static regex(pattern, flags: int = 0)

Filter messages that match a given RegEx pattern.

Parameters:
  • pattern (str) – The RegEx pattern as string, it will be applied to the text of a message. When a pattern matches, all the Match Objects are stored in the matches field of the Message itself.
  • flags (int, optional) – RegEx flags.
class user(users: int = None)

Filter messages coming from one or more users.

You can use set bound methods to manipulate the users container.

Parameters:users (int | str | list) – Pass one or more user ids/usernames to filter users. For you yourself, “me” or “self” can be used as well. Defaults to None (no users).
class chat(chats: int = None)

Filter messages coming from one or more chats.

You can use set bound methods to manipulate the chats container.

Parameters:chats (int | str | list) – Pass one or more chat ids/usernames to filter chats. For your personal cloud (Saved Messages) you can simply use “me” or “self”. Defaults to None (no chats).