Skip to content

Commit 8acff56

Browse files
Dmitry Grigoryevjsmnbom
authored andcommitted
Fix Filters.regex failing on non-text message (python-telegram-bot#1158)
* Fix python-telegram-bot#1115 * Improve regex filter test
1 parent d4b5bd4 commit 8acff56

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

telegram/ext/filters.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ def __init__(self, pattern):
198198
# the matched groups and groupdict to the context object.
199199

200200
def filter(self, message):
201-
return bool(self.pattern.search(message.text))
201+
if message.text:
202+
return bool(self.pattern.search(message.text))
203+
return False
202204

203205
class _Reply(BaseFilter):
204206
name = 'Filters.reply'

tests/test_filters.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ def test_filters_regex(self, message):
6868
message.text = 'i love python'
6969
assert Filters.regex(r'.\b[lo]{2}ve python')(message)
7070

71+
message.text = None
72+
assert not Filters.regex(r'fail')(message)
73+
7174
def test_filters_reply(self, message):
7275
another_message = Message(1, User(1, 'TestOther', False), datetime.datetime.now(),
7376
Chat(0, 'private'))

0 commit comments

Comments
 (0)