Add entities filter

Should ideally superseed #375.
This commit is contained in:
Jacob Bom 2016-09-13 20:09:46 +02:00
parent 5285f63e4a
commit 4e60008086

View file

@ -85,6 +85,33 @@ class Filters(object):
def forwarded(message):
return bool(message.forward_date)
@staticmethod
def entities(entity_types):
"""Filters messages to only allow those which have a :class:`telegram.MessageEntity`
that match `entity_types`.
Note:
This filter functions as an OR filter, meaning that just one of the entity_type.
If you want AND filtering instead, you have to specify multiple of this filter in the
`filters` attribute. Example:
>>> MessageHandler([Filters.entities(TEXT_MENTION, MENTION),
... Filters.entities(HASHTAG)], callback)
Will require either a one type of mention AND a hashtag in the message.
Args:
entity_types: List of entity types to check for. All types can be found as constants
in :class:`telegram.MessageEntity`.
Returns: function to use as filter
"""
def entities_filter(message):
return any([entity_types in entity.type for entity in message.entities])
return entities_filter
class MessageHandler(Handler):
"""