From e16c1da6b10944070b4a3ae0ec6a37a3f62a732a Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 24 Sep 2016 13:38:56 +0200 Subject: [PATCH] Change entities filter to be singular. Also remove the faulty example completely since it should be no longer needed. --- telegram/ext/messagehandler.py | 21 ++++++--------------- tests/test_filters.py | 16 +++++----------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/telegram/ext/messagehandler.py b/telegram/ext/messagehandler.py index 6289d86f8..01483b7a8 100644 --- a/telegram/ext/messagehandler.py +++ b/telegram/ext/messagehandler.py @@ -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") diff --git a/tests/test_filters.py b/tests/test_filters.py index 9dadac440..47bb95fae 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -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__':