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

Issue:
RegexHandler error with stickers, voices, images...
RegexHandler does not check if update.effective_message.text exists.

### Steps to reproduce
1. Add a RegexHandler
2. Run the bot
3. Send a sticker

### Expected behaviour
The handler should not capture the sticker

### Actual behaviour
The handler capture the sticker and gives an error

### Configuration
Does not matter


**Version of Python, python-telegram-bot & dependencies:**

``python-telegram-bot 7.0.0``

### Logs
2017-07-26 14:02:47,301 - telegram.ext.dispatcher - ERROR - An uncaught error was raised while processing the update
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/telegram/ext/dispatcher.py", line 269, in process_update
    if handler.check_update(update):
  File "/usr/local/lib/python3.5/dist-packages/telegram/ext/regexhandler.py", line 150, in check_update
    match = re.match(self.pattern, update.effective_message.text)
  File "/usr/lib/python3.5/re.py", line 163, in match
    return _compile(pattern, flags).match(string)
TypeError: expected string or bytes-like object



Discussion:

Resolving patch:
diff --git a/telegram/ext/regexhandler.py b/telegram/ext/regexhandler.py
--- a/telegram/ext/regexhandler.py
+++ b/telegram/ext/regexhandler.py
@@ -142,11 +142,12 @@ def check_update(self, update):
         Returns:
             :obj:`bool`
         """
-
+        if not isinstance(update, Update) and not update.effective_message:
+            return False
         if any([(self.message_updates and update.message),
                 (self.edited_updates and update.edited_message),
-                (self.channel_post_updates and update.channel_post)]) and (
-                isinstance(update, Update)):
+                (self.channel_post_updates and update.channel_post)]) and \
+                update.effective_message.text:
             match = re.match(self.pattern, update.effective_message.text)
             return bool(match)
         return False


Test patch:
diff --git a/tests/test_updater.py b/tests/test_updater.py
--- a/tests/test_updater.py
+++ b/tests/test_updater.py
@@ -34,8 +34,6 @@
 
 from future.builtins import bytes
 
-from telegram.utils.request import Request as Requester
-
 try:
     # python2
     from urllib2 import urlopen, Request, HTTPError
@@ -47,12 +45,12 @@
 sys.path.append('.')
 
 from telegram import (Update, Message, TelegramError, User, Chat, Bot,
-                      InlineQuery, CallbackQuery, ShippingQuery, PreCheckoutQuery)
+                      InlineQuery, CallbackQuery)
 from telegram.ext import *
 from telegram.ext.dispatcher import run_async
 from telegram.error import Unauthorized, InvalidToken
 from tests.base import BaseTest
-from threading import Lock, Thread, current_thread, Semaphore
+from threading import Lock, Thread, current_thread
 
 # Enable logging
 root = logging.getLogger()
@@ -248,6 +246,23 @@ def test_addRemoveTelegramRegexHandler(self):
         sleep(.1)
         self.assertTrue(None is self.received_message)
 
+    def test_regex_handler_without_message(self):
+        self._setup_updater('Test3')
+        d = self.updater.dispatcher
+        handler = RegexHandler(r'Te.*', self.telegramHandlerTest)
+        d.add_handler(handler)
+
+        # message, no text
+        m = Message(1, User(1, "testuser"), None, Chat(2, "private"), video="My_vid",
+                    caption="test ")
+        d.process_update(Update(1, message=m))
+        self.assertEqual(self.message_count, 0)
+
+        # no message
+        c = InlineQuery(2, User(1, "testuser"), "my_query", offset=15)
+        d.process_update(Update(2, inline_query=c))
+        self.assertEqual(self.message_count, 0)
+
     def test_addRemoveTelegramCommandHandler(self):
         self._setup_updater('', messages=0)
         d = self.updater.dispatcher
@@ -926,7 +941,6 @@ def test_noTokenOrBot(self):
 
 
 class MockBot(object):
-
     def __init__(self,
                  text,
                  messages=1,
@@ -973,12 +987,12 @@ def delete_webhook(self):
             raise self.bootstrap_err
 
     def get_updates(self,
-                   offset=None,
-                   limit=100,
-                   timeout=0,
-                   network_delay=None,
-                   read_latency=2.,
-                   allowed_updates=None):
+                    offset=None,
+                    limit=100,
+                    timeout=0,
+                    network_delay=None,
+                    read_latency=2.,
+                    allowed_updates=None):
 
         if self.raise_error:
             raise TelegramError('Test Error 2')