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:


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


>>> ( |


>>> ~ 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).

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

Filters.forwarded = <telegram.ext.filters._Forwarded object> = <telegram.ext.filters._Game object> = <telegram.ext.filters._Group object>
Filters.location = <telegram.ext.filters._Location object> = <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> = <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
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.

  • 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.