Repository: python-telegram-bot/python-telegram-bot

Issue:
Filters.regex fails on non-text messages
<!--
Thanks for reporting issues of python-telegram-bot!

Use this template to notify us if you found a bug, or if you want to request a new feature.
If you're looking for help with programming your bot using our library, feel free to ask your
questions in out telegram group at: https://t.me/pythontelegrambotgroup

To make it easier for us to help you please enter detailed information below.

Please note, we only support the latest version of python-telegram-bot and
master branch. Please make sure to upgrade & recreate the issue on the latest
version prior to opening an issue.
-->
### Steps to reproduce
1. given a handler like the following:
`def msg(bot, update):
    answer = random.choice(bot_misc.ANSWERS)
    update.message.reply_text(answer)
updater.dispatcher.add_handler(MessageHandler(Filters.regex('Hi'), msg))`

2. send the bot some kind of media (like a sticker, an image or a voice-message)

3. error (success)

### Expected behaviour
I usually expect a regex filter to imply Filters.text (that the message is text), because you can only regex over text...

### Actual behaviour
the regex filter fails with the following error when it tries to process the update:
`2018-05-21 18:15:43,279 - telegram.ext.dispatcher - ERROR - An uncaught error was raised while processing the update
Traceback (most recent call last):
  File "/home/thorbijoern/env/ptb/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 278, in process_update
    for handler in (x for x in self.handlers[group] if x.check_update(update)):
  File "/home/thorbijoern/env/ptb/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 278, in <genexpr>
    for handler in (x for x in self.handlers[group] if x.check_update(update)):
  File "/home/thorbijoern/env/ptb/lib/python3.6/site-packages/telegram/ext/messagehandler.py", line 152, in check_update
    res = self.filters(message)
  File "/home/thorbijoern/env/ptb/lib/python3.6/site-packages/telegram/ext/filters.py", line 65, in __call__
    return self.filter(message)
  File "/home/thorbijoern/env/ptb/lib/python3.6/site-packages/telegram/ext/filters.py", line 201, in filter
    return bool(self.pattern.search(message.text))
TypeError: expected string or bytes-like object`


### workaround
a workaround would be just using `(Filters.text & Filters.regex('foo'))`.


Discussion:
Although there is a small addition to the expected behavior (it should also work on captions, not only text), it's bad behavior that it does not check this.
Thanks for  the report.

Resolving patch:
diff --git a/telegram/ext/filters.py b/telegram/ext/filters.py
--- a/telegram/ext/filters.py
+++ b/telegram/ext/filters.py
@@ -198,7 +198,9 @@ def __init__(self, pattern):
         # 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'


Test patch:
diff --git a/tests/test_filters.py b/tests/test_filters.py
--- a/tests/test_filters.py
+++ b/tests/test_filters.py
@@ -68,6 +68,9 @@ def test_filters_regex(self, message):
         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'))