telegram.ext.filters module

This module contains the Filters for use with the MessageHandler class

class telegram.ext.filters.BaseFilter

Bases: object

Base class for all Message Filters

Subclassing from this class filters to be combined using bitwise operators:

And:

>>> (Filters.text & Filters.entity(MENTION))

Or:

>>> (Filters.audio | Filters.video)

Not:

>>> ~ Filters.command

Also works with more than two filters:

>>> (Filters.text & (Filters.entity(URL) | Filters.entity(TEXT_LINK)))
>>> Filters.text & (~ Filters.forwarded)

If you want to create your own filters create a class inheriting from this class 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).

filter(message)
class telegram.ext.filters.Filters

Bases: object

Predefined filters for use with the filter argument of telegram.ext.MessageHandler.

all = <telegram.ext.filters._All object>
audio = <telegram.ext.filters._Audio object>
command = <telegram.ext.filters._Command object>
contact = <telegram.ext.filters._Contact object>
document = <telegram.ext.filters._Document object>
class entity(entity_type)

Bases: telegram.ext.filters.BaseFilter

Filters messages to only allow those which have a telegram.MessageEntity where their type matches entity_type.

Parameters:entity_type – Entity type to check for. All types can be found as constants in telegram.MessageEntity.

Returns: function to use as filter

filter(message)
Filters.forwarded = <telegram.ext.filters._Forwarded object>
Filters.game = <telegram.ext.filters._Game object>
Filters.group = <telegram.ext.filters._Group object>
class Filters.language(lang)

Bases: telegram.ext.filters.BaseFilter

Filters messages to only allow those which are from users with a certain language code. Note that according to telegrams documentation, every single user does not have the language_code attribute.

Parameters:lang (str|list) – Which language code(s) to allow through. This will be matched using .startswith meaning that ‘en’ will match both ‘en_US’ and ‘en_GB’
filter(message)
Filters.location = <telegram.ext.filters._Location object>
Filters.photo = <telegram.ext.filters._Photo object>
Filters.private = <telegram.ext.filters._Private object>
Filters.reply = <telegram.ext.filters._Reply object>
Filters.status_update = <telegram.ext.filters._StatusUpdate object>
Filters.sticker = <telegram.ext.filters._Sticker object>
Filters.text = <telegram.ext.filters._Text object>
Filters.venue = <telegram.ext.filters._Venue object>
Filters.video = <telegram.ext.filters._Video object>
Filters.voice = <telegram.ext.filters._Voice object>
class telegram.ext.filters.InvertedFilter(f)

Bases: telegram.ext.filters.BaseFilter

Represents a filter that has been inverted.

Parameters:f – The filter to invert
filter(message)
class telegram.ext.filters.MergedFilter(base_filter, and_filter=None, or_filter=None)

Bases: telegram.ext.filters.BaseFilter

Represents a filter consisting of two other filters.

Parameters:
  • base_filter – Filter 1 of the merged filter
  • and_filter – Optional filter to “and” with base_filter. Mutually exclusive with or_filter.
  • or_filter – Optional filter to “or” with base_filter. Mutually exclusive with and_filter.
filter(message)