Skip to content

Commit 4e60008

Browse files
committed
Add entities filter
Should ideally superseed python-telegram-bot#375.
1 parent 5285f63 commit 4e60008

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

telegram/ext/messagehandler.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,33 @@ def status_update(message):
8585
def forwarded(message):
8686
return bool(message.forward_date)
8787

88+
@staticmethod
89+
def entities(entity_types):
90+
"""Filters messages to only allow those which have a :class:`telegram.MessageEntity`
91+
that match `entity_types`.
92+
93+
Note:
94+
This filter functions as an OR filter, meaning that just one of the entity_type.
95+
If you want AND filtering instead, you have to specify multiple of this filter in the
96+
`filters` attribute. Example:
97+
98+
>>> MessageHandler([Filters.entities(TEXT_MENTION, MENTION),
99+
... Filters.entities(HASHTAG)], callback)
100+
101+
Will require either a one type of mention AND a hashtag in the message.
102+
103+
Args:
104+
entity_types: List of entity types to check for. All types can be found as constants
105+
in :class:`telegram.MessageEntity`.
106+
107+
Returns: function to use as filter
108+
"""
109+
110+
def entities_filter(message):
111+
return any([entity_types in entity.type for entity in message.entities])
112+
113+
return entities_filter
114+
88115

89116
class MessageHandler(Handler):
90117
"""

0 commit comments

Comments
 (0)