mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-22 22:45:09 +01:00
Change entities filter to be singular.
Also remove the faulty example completely since it should be no longer needed.
This commit is contained in:
parent
97bb04cd38
commit
e16c1da6b1
2 changed files with 11 additions and 26 deletions
|
@ -86,29 +86,19 @@ class Filters(object):
|
|||
return bool(message.forward_date)
|
||||
|
||||
@staticmethod
|
||||
def entities(entity_types):
|
||||
def entity(entity_type):
|
||||
"""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.
|
||||
where their `type` matches `entity_type`.
|
||||
|
||||
Args:
|
||||
entity_types: List of entity types to check for. All types can be found as constants
|
||||
entity_type: Entity type 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.type in entity_types for entity in message.entities])
|
||||
return any([entity.type == entity_type for entity in message.entities])
|
||||
|
||||
return entities_filter
|
||||
|
||||
|
@ -168,7 +158,8 @@ class MessageHandler(Handler):
|
|||
|
||||
return self.callback(dispatcher.bot, update, **optional_args)
|
||||
|
||||
# old non-PEP8 Handler methods
|
||||
# old non-PEP8 Handler methods
|
||||
|
||||
m = "telegram.MessageHandler."
|
||||
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
|
||||
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")
|
||||
|
|
|
@ -155,22 +155,16 @@ class FiltersTest(BaseTest, unittest.TestCase):
|
|||
e = functools.partial(MessageEntity, offset=0, length=0)
|
||||
|
||||
self.message.entities = [e(MessageEntity.MENTION)]
|
||||
self.assertTrue(Filters.entities([MessageEntity.MENTION])(self.message))
|
||||
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message))
|
||||
|
||||
self.message.entities = []
|
||||
self.assertFalse(Filters.entities([MessageEntity.MENTION])(self.message))
|
||||
self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message))
|
||||
|
||||
self.message.entities = [e(MessageEntity.BOLD)]
|
||||
self.assertFalse(Filters.entities([MessageEntity.MENTION])(self.message))
|
||||
self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message))
|
||||
|
||||
self.message.entities = [e(MessageEntity.MENTION)]
|
||||
self.assertTrue(
|
||||
Filters.entities([MessageEntity.MENTION, MessageEntity.BOLD])(self.message))
|
||||
self.message.entities = [e(MessageEntity.BOLD)]
|
||||
self.assertTrue(
|
||||
Filters.entities([MessageEntity.MENTION, MessageEntity.BOLD])(self.message))
|
||||
self.assertFalse(
|
||||
Filters.entities([MessageEntity.MENTION, MessageEntity.TEXT_MENTION])(self.message))
|
||||
self.message.entities = [e(MessageEntity.BOLD), e(MessageEntity.MENTION)]
|
||||
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in a new issue