From b5b09884b13339949cd956faae001675719018d4 Mon Sep 17 00:00:00 2001 From: Eldinnie Date: Sun, 23 Apr 2017 23:22:05 +0200 Subject: [PATCH] Group filters (#575) * stripping token of whitespaces before starting bot * Line feed * show exception that's caused (fixes flake8 failing) * Add private/group filters Add filters: - Filters.private (for messages in private chats) - Filters.group (for messages in group chats) * use constants --- telegram/ext/conversationhandler.py | 1 + telegram/ext/filters.py | 15 +++++++++++++++ tests/test_filters.py | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/telegram/ext/conversationhandler.py b/telegram/ext/conversationhandler.py index 1bcabc4f5..69e15b703 100644 --- a/telegram/ext/conversationhandler.py +++ b/telegram/ext/conversationhandler.py @@ -183,6 +183,7 @@ class ConversationHandler(Handler): res = new_state.result(timeout=self.run_async_timeout) except Exception as exc: self.logger.exception("Promise function raised exception") + self.logger.exception("{}".format(exc)) error = True if not error and new_state.done.is_set(): diff --git a/telegram/ext/filters.py b/telegram/ext/filters.py index 4b3cbd482..444feee66 100644 --- a/telegram/ext/filters.py +++ b/telegram/ext/filters.py @@ -17,6 +17,7 @@ # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. """ This module contains the Filters for use with the MessageHandler class """ +from telegram import Chat class BaseFilter(object): @@ -248,3 +249,17 @@ class Filters(object): def filter(self, message): return any([entity.type == self.entity_type for entity in message.entities]) + + class _Private(BaseFilter): + + def filter(self, message): + return message.chat.type == Chat.PRIVATE + + private = _Private() + + class _Group(BaseFilter): + + def filter(self, message): + return message.chat.type in [Chat.GROUP, Chat.SUPERGROUP] + + group = _Group() diff --git a/tests/test_filters.py b/tests/test_filters.py index ec38c4ec2..770d6532c 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -178,6 +178,18 @@ class FiltersTest(BaseTest, unittest.TestCase): self.message.entities = [self.e(MessageEntity.BOLD), self.e(MessageEntity.MENTION)] self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message)) + def test_private_filter(self): + self.assertTrue(Filters.private(self.message)) + self.message.chat.type = "group" + self.assertFalse(Filters.private(self.message)) + + def test_group_fileter(self): + self.assertFalse(Filters.group(self.message)) + self.message.chat.type = "group" + self.assertTrue(Filters.group(self.message)) + self.message.chat.type = "supergroup" + self.assertTrue(Filters.group(self.message)) + def test_and_filters(self): self.message.text = 'test' self.message.forward_date = True