This module contains filters for use with telegram.ext.MessageHandler
, telegram.ext.CommandHandler
, or telegram.ext.PrefixHandler
.
Changed in version 20.0:
Filters are no longer callable, if you’re using a custom filter and are calling an existing filter, then switch to the new syntax: filters.{filter}.check_update(update)
.
Removed the Filters
class. The filters are now directly attributes/classes of the filters
module.
The names of all filters has been updated:
Filter classes which are ready for use, e.g Filters.all
are now capitalized, e.g filters.ALL
.
Filters which need to be initialized are now in CamelCase. E.g. filters.User(...)
.
Filters which do both (like Filters.text
) are now split as ready-to-use version filters.TEXT
and class version filters.Text(...)
.
Changed in version 22.0: Removed deprecated attribute CHAT.
All Messages.
Messages that contain telegram.Message.animation
.
Messages that contain telegram.Message.effective_attachment()
.
Added in version 13.6.
Messages that contain telegram.Message.audio
.
Messages that contain telegram.Message.boost_added
.
Shortcut for telegram.ext.filters.Caption()
.
Examples
To allow any caption, simply use MessageHandler(filters.CAPTION, callback_method)
.
Messages that contain telegram.Message.checklist
.
Added in version 22.3.
Shortcut for telegram.ext.filters.Command()
.
Examples
To allow messages starting with a command use MessageHandler(filters.COMMAND, command_at_start_callback)
.
Messages that contain telegram.Message.contact
.
Messages that contain telegram.Message.effect_id
.
Added in version 21.3.
Messages that contain telegram.Message.forward_origin
.
Changed in version 20.8: Now based on telegram.Message.forward_origin
instead of telegram.Message.forward_date
.
Messages that contain telegram.Message.game
.
Messages that contain telegram.Message.giveaway
.
Messages that contain telegram.Message.giveaway_winners
.
Messages that contain telegram.Message.has_media_spoiler
.
Added in version 20.0.
Messages that contain telegram.Message.has_protected_content
.
Added in version 13.9.
Messages that contain telegram.Message.invoice
.
Messages that contain telegram.Message.is_automatic_forward
.
Added in version 13.9.
Messages that contain telegram.Message.is_from_offline
.
Added in version 21.1.
Messages that contain telegram.Message.is_topic_message
.
Added in version 20.0.
Messages that contain telegram.Message.location
.
Messages that contain telegram.Message.paid_media
.
Added in version 21.4.
Messages that contain telegram.Message.passport_data
.
Messages that contain telegram.Message.photo
.
Messages that contain telegram.Message.poll
.
This filter filters any message from a Telegram Premium user
as telegram.Update.effective_user
.
Added in version 20.0.
Messages that contain telegram.Message.reply_to_message
.
Messages that contain telegram.Message.reply_to_story
.
Messages that contain telegram.Message.sender_boost_count
.
Messages that contain telegram.Message.story
.
Added in version 20.5.
Messages that contain telegram.Message.successful_payment
.
Shortcut for telegram.ext.filters.Text()
.
Examples
To allow any text message, simply use MessageHandler(filters.TEXT, callback_method)
.
This filter filters any message that has a telegram.Message.from_user
.
This filter filters any message that have a user who added the bot to their attachment menu
as telegram.Update.effective_user
.
Added in version 20.0.
Messages that contain telegram.Message.venue
.
This filter filters for message that were sent via any bot.
Messages that contain telegram.Message.video
.
Messages that contain telegram.Message.video_note
.
Messages that contain telegram.Message.voice
.
Bases: object
Base class for all Filters.
Filters subclassing from this class can combined using bitwise operators:
And:
filters.TEXT & filters.Entity(MENTION)
Or:
filters.AUDIO | filters.VIDEO
Exclusive Or:
filters.Regex('To Be') ^ filters.Regex('Not 2B')
Not:
Also works with more than two filters:
filters.TEXT & (filters.Entity("url") | filters.Entity("text_link")) filters.TEXT & (~ filters.FORWARDED)
Note
Filters use the same short circuiting logic as python’s and
, or
and not
. This means that for example:
filters.Regex(r'(a?x)') | filters.Regex(r'(b?x)')
With message.text == 'x'
, will only ever return the matches for the first filter, since the second one is never evaluated.
If you want to create your own filters create a class inheriting from either MessageFilter
or UpdateFilter
and implement a filter()
method that returns a boolean: True
if the message should be handled, False
otherwise. Note that the filters work only as class instances, not actual class objects (so remember to initialize your filter classes).
By default, the filters name (what will get printed when converted to a string for display) will be the class name. If you want to overwrite this assign a better name to the name
class variable.
Added in version 20.0: Added the arguments name
and data_filter
.
name (str
) – Name for this filter. Defaults to the type of filter.
data_filter (bool
) – Whether this filter is a data filter. A data filter should return a dict with lists. The dict will be merged with telegram.ext.CallbackContext
’s internal dict in most cases (depends on the handler).
Defines AND bitwise operator for BaseFilter
object. The combined filter accepts an update only if it is accepted by both filters. For example, filters.PHOTO & filters.CAPTION
will only accept messages that contain both a photo and a caption.
Defines OR bitwise operator for BaseFilter
object. The combined filter accepts an update only if it is accepted by any of the filters. For example, filters.PHOTO | filters.CAPTION
will only accept messages that contain photo or caption or both.
Defines XOR bitwise operator for BaseFilter
object. The combined filter accepts an update only if it is accepted by any of the filters and not both of them. For example, filters.PHOTO ^ filters.CAPTION
will only accept messages that contain photo or caption, not both of them.
Defines NOT bitwise operator for BaseFilter
object. The combined filter accepts an update only if it is accepted by any of the filters. For example, ~ filters.PHOTO
will only accept messages that do not contain photo.
Gives name for this filter.
Whether this filter is a data filter.
Name for this filter.
Checks if the specified update should be handled by this filter.
Changed in version 21.1: This filter now also returns True
if the update contains business_message
or edited_business_message
.
update (telegram.Update
) – The update to check.
True
if the update contains one of channel_post
, message
, edited_channel_post
, edited_message
, telegram.Update.business_message
, telegram.Update.edited_business_message
, or False
otherwise.
Bases: telegram.ext.filters.MessageFilter
Messages with a caption. If a list of strings is passed, it filters messages to only allow those whose caption is appearing in the given list.
Examples
MessageHandler(filters.Caption(['PTB rocks!', 'PTB']), callback_method_2)
Bases: telegram.ext.filters.MessageFilter
Filters media messages to only allow those which have a telegram.MessageEntity
where their type
matches entity_type.
Examples
MessageHandler(filters.CaptionEntity("hashtag"), callback_method)
entity_type (str
) – Caption Entity type to check for. All types can be found as constants in telegram.MessageEntity
.
Bases: telegram.ext.filters.MessageFilter
Filters updates by searching for an occurrence of pattern
in the message caption.
This filter works similarly to Regex
, with the only exception being that it applies to the message caption instead of the text.
Examples
Use MessageHandler(filters.PHOTO & filters.CaptionRegex(r'help'), callback)
to capture all photos with caption containing the word ‘help’.
Note
This filter will not work on simple text messages, but only on media with caption.
pattern (str
| re.Pattern
) – The regex pattern.
Bases: telegram.ext.filters.MessageFilter
Filters messages to allow only those which are from a specified chat ID or username.
Examples
MessageHandler(filters.Chat(-1234), callback_method)
Warning
chat_ids
will give a copy of the saved chat ids as frozenset
. This is to ensure thread safety. To add/remove a chat, you should use add_chat_ids()
, and remove_chat_ids()
. Only update the entire set by filter.chat_ids = new_set
, if you are entirely sure that it is not causing race conditions, as this will complete replace the current set of allowed chats.
chat_id (int
| Collection[int
], optional) – Which chat ID(s) to allow through.
username (str
| Collection[str
], optional) – Which username(s) to allow through. Leading '@'
s in usernames will be discarded.
allow_empty (bool
, optional) – Whether updates should be processed, if no chat is specified in chat_ids
and usernames
. Defaults to False
.
Which chat ID(s) to allow through.
set(int
)
Whether updates should be processed, if no chat is specified in chat_ids
and usernames
.
RuntimeError – If chat_id
and username
are both present.
Add one or more chats to the allowed chat ids.
Remove one or more chats from allowed chat ids.
Add one or more chats to the allowed usernames.
Name for this filter.
Remove one or more chats from allowed usernames.
Which username(s) to allow through.
Warning
usernames
will give a copy of the saved usernames as frozenset
. This is to ensure thread safety. To add/remove a user, you should use add_usernames()
, and remove_usernames()
. Only update the entire set by filter.usernames = new_set
, if you are entirely sure that it is not causing race conditions, as this will complete replace the current set of allowed users.
frozenset(str
)
Bases: object
Subset for filtering the type of chat.
Examples
Use these filters like: filters.ChatType.CHANNEL
or filters.ChatType.SUPERGROUP
etc.
Caution
filters.ChatType
itself is not a filter, but just a convenience namespace.
Updates from channel.
Updates from group.
Update from group or supergroup.
Update from private chats.
Updates from supergroup.
Bases: telegram.ext.filters.MessageFilter
Messages with a telegram.MessageEntity.BOT_COMMAND
. By default, only allows messages starting with a bot command. Pass False
to also allow messages that contain a bot command anywhere in the text.
Examples
MessageHandler(filters.Command(False), command_anywhere_callback)
only_start (bool
, optional) – Whether to only allow messages that start with a bot command. Defaults to True
.
Bases: telegram.ext.filters.MessageFilter
Dice Messages. If an integer or a list of integers is passed, it filters messages to only allow those whose dice value is appearing in the given list.
Added in version 13.4.
Examples
To allow any dice message, simply use MessageHandler(filters.Dice.ALL, callback_method)
.
To allow any dice message, but with value 3 or 4, use MessageHandler(filters.Dice([3, 4]), callback_method)
To allow only dice messages with the emoji 🎲, but any value, use MessageHandler(filters.Dice.DICE, callback_method)
.
To allow only dice messages with the emoji 🎯 and with value 6, use MessageHandler(filters.Dice.Darts(6), callback_method)
.
To allow only dice messages with the emoji ⚽ and with value 5 or 6, use MessageHandler(filters.Dice.Football([5, 6]), callback_method)
.
Note
Dice messages don’t have text. If you want to filter either text or dice messages, use filters.TEXT | filters.Dice.ALL
.
values (int
| Collection[int
], optional) – Which values to allow. If not specified, will allow the specified dice message.
Dice messages with any value and any emoji.
Bases: telegram.ext.filters.MessageFilter
Dice messages with the emoji 🏀. Supports passing a list of integers.
Dice messages with the emoji 🏀. Matches any dice value.
Bases: telegram.ext.filters.MessageFilter
Dice messages with the emoji 🎳. Supports passing a list of integers.
Dice messages with the emoji 🎳. Matches any dice value.
Bases: telegram.ext.filters.MessageFilter
Dice messages with the emoji 🎯. Supports passing a list of integers.
Dice messages with the emoji 🎯. Matches any dice value.
Bases: telegram.ext.filters.MessageFilter
Dice messages with the emoji 🎲. Supports passing a list of integers.
Dice messages with the emoji 🎲. Matches any dice value.
Bases: telegram.ext.filters.MessageFilter
Dice messages with the emoji ⚽. Supports passing a list of integers.
Dice messages with the emoji ⚽. Matches any dice value.
Bases: telegram.ext.filters.MessageFilter
Dice messages with the emoji 🎰. Supports passing a list of integers.
Dice messages with the emoji 🎰. Matches any dice value.
Bases: object
Subset for messages containing a document/file.
Examples
Use these filters like: filters.Document.MP3
, filters.Document.MimeType("text/plain")
etc. Or just use filters.Document.ALL
for all document messages.
Caution
filters.Document
itself is not a filter, but just a convenience namespace.
Messages that contain a telegram.Message.document
.
Bases: telegram.ext.filters.MessageFilter
Filters documents by their category in the mime-type attribute.
Example
filters.Document.Category('audio/')
returns True
for all types of audio sent as a file, for example 'audio/mpeg'
or 'audio/x-wav'
.
Note
This Filter only filters by the mime_type of the document, it doesn’t check the validity of the document. The user can manipulate the mime-type of a message and send media with wrong types that don’t fit to this handler.
Use as filters.Document.APPLICATION
.
Use as filters.Document.AUDIO
.
Use as filters.Document.IMAGE
.
Use as filters.Document.VIDEO
.
Use as filters.Document.TEXT
.
Bases: telegram.ext.filters.MessageFilter
This filter filters documents by their file ending/extension.
file_extension (str
| None
) – Media file extension you want to filter.
case_sensitive (bool
, optional) – Pass True
to make the filter case sensitive. Default: False
.
Example
filters.Document.FileExtension("jpg")
filters files with extension ".jpg"
.
filters.Document.FileExtension(".jpg")
filters files with extension "..jpg"
.
filters.Document.FileExtension("Dockerfile", case_sensitive=True)
filters files with extension ".Dockerfile"
minding the case.
filters.Document.FileExtension(None)
filters files without a dot in the filename.
Note
This Filter only filters by the file ending/extension of the document, it doesn’t check the validity of document.
The user can manipulate the file extension of a document and send media with wrong types that don’t fit to this handler.
Case insensitive by default, you may change this with the flag case_sensitive=True
.
Extension should be passed without leading dot unless it’s a part of the extension.
Pass None
to filter files with no extension, i.e. without a dot in the filename.
Bases: telegram.ext.filters.MessageFilter
This Filter filters documents by their mime-type attribute.
Example
filters.Document.MimeType('audio/mpeg')
filters all audio in .mp3 format.
Note
This Filter only filters by the mime_type of the document, it doesn’t check the validity of document. The user can manipulate the mime-type of a message and send media with wrong types that don’t fit to this handler.
Use as filters.Document.APK
.
Use as filters.Document.DOC
.
Use as filters.Document.DOCX
.
Use as filters.Document.EXE
.
Use as filters.Document.MP4
.
Use as filters.Document.GIF
.
Use as filters.Document.JPG
.
Use as filters.Document.MP3
.
Use as filters.Document.PDF
.
Use as filters.Document.PY
.
Use as filters.Document.SVG
.
Use as filters.Document.TXT
.
Use as filters.Document.TARGZ
.
Use as filters.Document.WAV
.
Use as filters.Document.XML
.
Use as filters.Document.ZIP
.
Bases: telegram.ext.filters.MessageFilter
Filters messages to only allow those which have a telegram.MessageEntity
where their type
matches entity_type.
Examples
MessageHandler(filters.Entity("hashtag"), callback_method)
entity_type (str
) – Entity type to check for. All types can be found as constants in telegram.MessageEntity
.
Bases: telegram.ext.filters.MessageFilter
Filters messages to allow only those which are forwarded from the specified chat ID(s) or username(s) based on telegram.Message.forward_origin
and in particular
Added in version 13.5.
Changed in version 20.8: Was previously based on telegram.Message.forward_from
and telegram.Message.forward_from_chat
.
Examples
MessageHandler(filters.ForwardedFrom(chat_id=1234), callback_method)
Warning
chat_ids
will give a copy of the saved chat ids as frozenset
. This is to ensure thread safety. To add/remove a chat, you should use add_chat_ids()
, and remove_chat_ids()
. Only update the entire set by filter.chat_ids = new_set
, if you are entirely sure that it is not causing race conditions, as this will complete replace the current set of allowed chats.
chat_id (int
| Collection[int
], optional) – Which chat/user ID(s) to allow through.
username (str
| Collection[str
], optional) – Which username(s) to allow through. Leading '@'
s in usernames will be discarded.
allow_empty (bool
, optional) – Whether updates should be processed, if no chat is specified in chat_ids
and usernames
. Defaults to False
.
Which chat/user ID(s) to allow through.
set(int
)
Whether updates should be processed, if no chat is specified in chat_ids
and usernames
.
RuntimeError – If both chat_id
and username
are present.
Add one or more chats to the allowed chat ids.
Remove one or more chats from allowed chat ids.
Add one or more chats to the allowed usernames.
Name for this filter.
Remove one or more chats from allowed usernames.
Which username(s) to allow through.
Warning
usernames
will give a copy of the saved usernames as frozenset
. This is to ensure thread safety. To add/remove a user, you should use add_usernames()
, and remove_usernames()
. Only update the entire set by filter.usernames = new_set
, if you are entirely sure that it is not causing race conditions, as this will complete replace the current set of allowed users.
frozenset(str
)
Bases: telegram.ext.filters.MessageFilter
Filters messages to only allow those which are from users with a certain language code.
Note
According to official Telegram Bot API documentation, not every single user has the language_code attribute. Do not count on this filter working on all users.
Examples
MessageHandler(filters.Language("en"), callback_method)
lang (str
| Collection[str
]) – Which language code(s) to allow through. This will be matched using str.startswith
meaning that ‘en’ will match both ‘en_US’ and ‘en_GB’.
Bases: telegram.ext.filters.MessageFilter
Messages containing mentions of specified users or chats.
Examples
MessageHandler(filters.Mention("username"), callback) MessageHandler(filters.Mention(["@username", 123456]), callback)
Added in version 20.7.
mentions (int
| str
| telegram.User
| Collection[int
| str
| telegram.User
]) – Specifies the users and chats to filter for. Messages that do not mention at least one of the specified users or chats will not be handled. Leading '@'
s in usernames will be discarded.
Bases: telegram.ext.filters.BaseFilter
Base class for all Message Filters. In contrast to UpdateFilter
, the object passed to filter()
is telegram.Update.effective_message
.
Please see BaseFilter
for details on how to create custom filters.
Checks if the specified update should be handled by this filter by passing effective_message
to filter()
.
This method must be overwritten.
message (telegram.Message
) – The message that is tested.
Bases: telegram.ext.filters.MessageFilter
Filters updates by searching for an occurrence of pattern
in the message text. The re.search()
function is used to determine whether an update should be filtered.
Refer to the documentation of the re
module for more information.
To get the groups and groupdict matched, see telegram.ext.CallbackContext.matches
.
Examples
Use MessageHandler(filters.Regex(r'help'), callback)
to capture all messages that contain the word ‘help’. You can also use MessageHandler(filters.Regex(re.compile(r'help', re.IGNORECASE)), callback)
if you want your pattern to be case insensitive. This approach is recommended if you need to specify flags on your pattern.
Note
Filters use the same short circuiting logic as python’s and
, or
and not
. This means that for example:
>>> filters.Regex(r'(a?x)') | filters.Regex(r'(b?x)')
With a telegram.Message.text
of x, will only ever return the matches for the first filter, since the second one is never evaluated.
pattern (str
| re.Pattern
) – The regex pattern.
Bases: telegram.ext.filters.MessageFilter
Filters messages to allow only those which are from a specified sender chat’s chat ID or username.
Examples
To filter for messages sent to a group by a channel with ID -1234
, use MessageHandler(filters.SenderChat(-1234), callback_method)
.
To filter for messages of anonymous admins in a super group with username @anonymous
, use MessageHandler(filters.SenderChat(username='anonymous'), callback_method)
.
To filter for messages sent to a group by any channel, use MessageHandler(filters.SenderChat.CHANNEL, callback_method)
.
To filter for messages of anonymous admins in any super group, use MessageHandler(filters.SenderChat.SUPERGROUP, callback_method)
.
To filter for messages forwarded to a discussion group from any channel or of anonymous admins in any super group, use MessageHandler(filters.SenderChat.ALL, callback)
Note
Remember, sender_chat
is also set for messages in a channel as the channel itself, so when your bot is an admin in a channel and the linked discussion group, you would receive the message twice (once from inside the channel, once inside the discussion group). Since v13.9, the field telegram.Message.is_automatic_forward
will be True
for the discussion group message.
Warning
chat_ids
will return a copy of the saved chat ids as frozenset
. This is to ensure thread safety. To add/remove a chat, you should use add_chat_ids()
, and remove_chat_ids()
. Only update the entire set by filter.chat_ids = new_set
, if you are entirely sure that it is not causing race conditions, as this will complete replace the current set of allowed chats.
chat_id (int
| Collection[int
], optional) – Which sender chat chat ID(s) to allow through.
username (str
| Collection[str
], optional) – Which sender chat username(s) to allow through. Leading '@'
s in usernames will be discarded.
allow_empty (bool
, optional) – Whether updates should be processed, if no sender chat is specified in chat_ids
and usernames
. Defaults to False
.
Which sender chat chat ID(s) to allow through.
set(int
)
Whether updates should be processed, if no sender chat is specified in chat_ids
and usernames
.
RuntimeError – If both chat_id
and username
are present.
All messages with a telegram.Message.sender_chat
.
Messages whose sender chat is a super group.
Messages whose sender chat is a channel.
Add one or more sender chats to the allowed chat ids.
Remove one or more sender chats from allowed chat ids.
Add one or more chats to the allowed usernames.
Name for this filter.
Remove one or more chats from allowed usernames.
Which username(s) to allow through.
Warning
usernames
will give a copy of the saved usernames as frozenset
. This is to ensure thread safety. To add/remove a user, you should use add_usernames()
, and remove_usernames()
. Only update the entire set by filter.usernames = new_set
, if you are entirely sure that it is not causing race conditions, as this will complete replace the current set of allowed users.
frozenset(str
)
Bases: object
Subset for messages containing a status update.
Examples
Use these filters like: filters.StatusUpdate.NEW_CHAT_MEMBERS
etc. Or use just filters.StatusUpdate.ALL
for all status update messages.
Caution
filters.StatusUpdate
itself is not a filter, but just a convenience namespace.
Changed in version 22.0: Removed deprecated attribute USER_SHARED.
Messages that contain any of the below.
Messages that contain telegram.Message.chat_background_set
.
Messages that contain telegram.Message.group_chat_created
, telegram.Message.supergroup_chat_created
or telegram.Message.channel_chat_created
.
Messages that contain telegram.Message.chat_shared
.
Added in version 20.1.
Messages that contain telegram.Message.checklist_tasks_added
.
Added in version 22.3.
Messages that contain telegram.Message.checklist_tasks_done
.
Added in version 22.3.
Messages that contain telegram.Message.connected_website
.
Messages that contain telegram.Message.direct_message_price_changed
.
Added in version 22.3.
Messages that contain telegram.Message.delete_chat_photo
.
Messages that contain telegram.Message.forum_topic_closed
.
Added in version 20.0.
Messages that contain telegram.Message.forum_topic_created
.
Added in version 20.0.
Messages that contain telegram.Message.forum_topic_edited
.
Added in version 20.0.
Messages that contain telegram.Message.forum_topic_reopened
.
Added in version 20.0.
Messages that contain telegram.Message.general_forum_topic_hidden
.
Added in version 20.0.
Messages that contain telegram.Message.general_forum_topic_unhidden
.
Added in version 20.0.
Messages that contain telegram.Message.gift
.
Added in version 22.1.
Messages that contain telegram.Message.giveaway_created
.
Added in version 20.8.
Messages that contain telegram.Message.giveaway_completed
. .. versionadded:: 20.8
Messages that contain telegram.Message.left_chat_member
.
Messages that contain telegram.Message.message_auto_delete_timer_changed
Added in version 13.4.
Messages that contain telegram.Message.migrate_from_chat_id
or telegram.Message.migrate_to_chat_id
.
Messages that contain telegram.Message.new_chat_members
.
Messages that contain telegram.Message.new_chat_photo
.
Messages that contain telegram.Message.new_chat_title
.
Messages that contain telegram.Message.paid_message_price_changed
.
Added in version 22.1.
Messages that contain telegram.Message.pinned_message
.
Messages that contain telegram.Message.proximity_alert_triggered
.
Messages that contain telegram.Message.refunded_payment
. .. versionadded:: 21.4
Messages that contain telegram.Message.unique_gift
.
Added in version 22.1.
Messages that contain telegram.Message.users_shared
.
Added in version 20.8.
Messages that contain telegram.Message.video_chat_ended
.
Added in version 13.4.
Changed in version 20.0: This filter was formerly named VOICE_CHAT_ENDED
Messages that contain telegram.Message.video_chat_scheduled
.
Added in version 13.5.
Changed in version 20.0: This filter was formerly named VOICE_CHAT_SCHEDULED
Messages that contain telegram.Message.video_chat_started
.
Added in version 13.4.
Changed in version 20.0: This filter was formerly named VOICE_CHAT_STARTED
Messages that contain telegram.Message.video_chat_participants_invited
.
Added in version 13.4.
Changed in version 20.0: This filter was formerly named VOICE_CHAT_PARTICIPANTS_INVITED
Messages that contain telegram.Message.web_app_data
.
Added in version 20.0.
Messages that contain telegram.Message.write_access_allowed
.
Added in version 20.0.
Bases: object
Filters messages which contain a sticker.
Examples
Use this filter like: filters.Sticker.VIDEO
. Or, just use filters.Sticker.ALL
for any type of sticker.
Caution
filters.Sticker
itself is not a filter, but just a convenience namespace.
Messages that contain telegram.Message.sticker
.
Messages that contain telegram.Message.sticker
and is animated
.
Added in version 20.0.
Messages that contain telegram.Message.sticker
and is a static sticker, i.e. does not contain telegram.Sticker.is_animated
or telegram.Sticker.is_video
.
Added in version 20.0.
Messages that contain telegram.Message.sticker
and is a video sticker
.
Added in version 20.0.
Messages that contain telegram.Message.sticker
and have a premium animation
.
Added in version 20.0.
Bases: telegram.ext.filters.MessageFilter
Successful Payment Messages. If a list of invoice payloads is passed, it filters messages to only allow those whose invoice_payload is appearing in the given list.
Examples
MessageHandler(filters.SuccessfulPayment([‘Custom-Payload’]), callback_method)
invoice_payloads (list[str
] | tuple[str
], optional) – Which invoice payloads to allow. Only exact matches are allowed. If not specified, will allow any invoice payload.
Added in version 20.8.
Bases: telegram.ext.filters.MessageFilter
Text Messages. If a list of strings is passed, it filters messages to only allow those whose text is appearing in the given list.
Examples
A simple use case for passing a list is to allow only messages that were sent by a custom telegram.ReplyKeyboardMarkup
:
buttons = ['Start', 'Settings', 'Back'] markup = ReplyKeyboardMarkup.from_column(buttons) ... MessageHandler(filters.Text(buttons), callback_method)
Note
Dice messages don’t have text. If you want to filter either text or dice messages, use filters.TEXT | filters.Dice.ALL
.
Messages containing a command are accepted by this filter. Use filters.TEXT & (~filters.COMMAND)
, if you want to filter only text messages without commands.
Bases: telegram.ext.filters.BaseFilter
Base class for all Update Filters. In contrast to MessageFilter
, the object passed to filter()
is an instance of telegram.Update
, which allows to create filters like telegram.ext.filters.UpdateType.EDITED_MESSAGE
.
Please see telegram.ext.filters.BaseFilter
for details on how to create custom filters.
Checks if the specified update should be handled by this filter.
This method must be overwritten.
update (telegram.Update
) – The update that is tested.
Bases: object
Subset for filtering the type of update.
Examples
Use these filters like: filters.UpdateType.MESSAGE
or filters.UpdateType.CHANNEL_POSTS
etc.
Caution
filters.UpdateType
itself is not a filter, but just a convenience namespace.
Updates with telegram.Update.channel_post
.
Updates with either telegram.Update.channel_post
or telegram.Update.edited_channel_post
.
Updates with telegram.Update.edited_message
, telegram.Update.edited_channel_post
, or telegram.Update.edited_business_message
.
Added in version 20.0.
Changed in version 21.1: Added telegram.Update.edited_business_message
to the filter.
Updates with telegram.Update.edited_channel_post
.
Updates with telegram.Update.edited_message
.
Updates with telegram.Update.message
.
Updates with either telegram.Update.message
or telegram.Update.edited_message
.
Updates with telegram.Update.business_message
.
Added in version 21.1.
Updates with telegram.Update.edited_business_message
.
Added in version 21.1.
Updates with either telegram.Update.business_message
or telegram.Update.edited_business_message
.
Added in version 21.1.
Bases: telegram.ext.filters.MessageFilter
Filters messages to allow only those which are from specified user ID(s) or username(s).
Examples
MessageHandler(filters.User(1234), callback_method)
user_id (int
| Collection[int
], optional) – Which user ID(s) to allow through.
username (str
| Collection[str
], optional) – Which username(s) to allow through. Leading '@'
s in usernames will be discarded.
allow_empty (bool
, optional) – Whether updates should be processed, if no user is specified in user_ids
and usernames
. Defaults to False
.
RuntimeError – If user_id
and username
are both present.
Whether updates should be processed, if no user is specified in user_ids
and usernames
.
Add one or more chats to the allowed usernames.
Name for this filter.
Remove one or more chats from allowed usernames.
Which username(s) to allow through.
Warning
usernames
will give a copy of the saved usernames as frozenset
. This is to ensure thread safety. To add/remove a user, you should use add_usernames()
, and remove_usernames()
. Only update the entire set by filter.usernames = new_set
, if you are entirely sure that it is not causing race conditions, as this will complete replace the current set of allowed users.
frozenset(str
)
Which user ID(s) to allow through.
Warning
user_ids
will give a copy of the saved user ids as frozenset
. This is to ensure thread safety. To add/remove a user, you should use add_user_ids()
, and remove_user_ids()
. Only update the entire set by filter.user_ids = new_set
, if you are entirely sure that it is not causing race conditions, as this will complete replace the current set of allowed users.
frozenset(int
)
Add one or more users to the allowed user ids.
Bases: telegram.ext.filters.MessageFilter
Filters messages to allow only those which are from specified via_bot ID(s) or username(s).
Examples
MessageHandler(filters.ViaBot(1234), callback_method)
bot_id (int
| Collection[int
], optional) – Which bot ID(s) to allow through.
username (str
| Collection[str
], optional) – Which username(s) to allow through. Leading '@'
s in usernames will be discarded.
allow_empty (bool
, optional) – Whether updates should be processed, if no user is specified in bot_ids
and usernames
. Defaults to False
.
RuntimeError – If bot_id
and username
are both present.
Whether updates should be processed, if no bot is specified in bot_ids
and usernames
.
Add one or more chats to the allowed usernames.
Name for this filter.
Remove one or more chats from allowed usernames.
Which username(s) to allow through.
Warning
usernames
will give a copy of the saved usernames as frozenset
. This is to ensure thread safety. To add/remove a user, you should use add_usernames()
, and remove_usernames()
. Only update the entire set by filter.usernames = new_set
, if you are entirely sure that it is not causing race conditions, as this will complete replace the current set of allowed users.
frozenset(str
)
Which bot ID(s) to allow through.
Warning
bot_ids
will give a copy of the saved bot ids as frozenset
. This is to ensure thread safety. To add/remove a bot, you should use add_bot_ids()
, and remove_bot_ids()
. Only update the entire set by filter.bot_ids = new_set
, if you are entirely sure that it is not causing race conditions, as this will complete replace the current set of allowed bots.
frozenset(int
)
Add one or more bots to the allowed bot ids.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4