From 8acff56145fc9882ce076d6d1f2dfcc1654c3171 Mon Sep 17 00:00:00 2001 From: Dmitry Grigoryev Date: Sun, 26 Aug 2018 21:39:01 +0300 Subject: [PATCH] Fix Filters.regex failing on non-text message (#1158) * Fix #1115 * Improve regex filter test --- telegram/ext/filters.py | 4 +++- tests/test_filters.py | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/telegram/ext/filters.py b/telegram/ext/filters.py index 4b3ade78d..782cbd36d 100644 --- a/telegram/ext/filters.py +++ b/telegram/ext/filters.py @@ -198,7 +198,9 @@ class Filters(object): # the matched groups and groupdict to the context object. def filter(self, message): - return bool(self.pattern.search(message.text)) + if message.text: + return bool(self.pattern.search(message.text)) + return False class _Reply(BaseFilter): name = 'Filters.reply' diff --git a/tests/test_filters.py b/tests/test_filters.py index fab1f53c0..cf8c164e7 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -68,6 +68,9 @@ class TestFilters(object): message.text = 'i love python' assert Filters.regex(r'.\b[lo]{2}ve python')(message) + message.text = None + assert not Filters.regex(r'fail')(message) + def test_filters_reply(self, message): another_message = Message(1, User(1, 'TestOther', False), datetime.datetime.now(), Chat(0, 'private'))