diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-07-19 10:58:03 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-07-19 10:58:03 +0300 |
| commit | 0086f5016f1255a8d311ed5c6adfaf8f5c4fcf7e (patch) | |
| tree | 69a61f133785e3c2758b66be358ce755aee8856d | |
| parent | 5a636abf7e98a5687811167917d0e07103599bc8 (diff) | |
Skip updates without expected keys
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Dockerfile | 1 | ||||
| -rw-r--r-- | telepot/exception.py | 3 | ||||
| -rw-r--r-- | telepot/loop.py | 28 |
4 files changed, 22 insertions, 11 deletions
@@ -2,3 +2,4 @@ *.pyc .env photo/ +__pycache__/ @@ -7,6 +7,7 @@ COPY requirements.txt ./ RUN pip install -r requirements.txt COPY *.png /app/ +COPY telepot /app/telepot COPY taulubot.py /app/taulubot.py CMD ["python", "taulubot.py"] diff --git a/telepot/exception.py b/telepot/exception.py index 820a522..04104c3 100644 --- a/telepot/exception.py +++ b/telepot/exception.py @@ -62,6 +62,9 @@ class IdleTerminate(WaitTooLong): class StopListening(TelepotException): pass +class UpdateKeyError(TelepotException): + pass + class TelegramError(TelepotException): """ To indicate erroneous situations, Telegram returns a JSON object containing diff --git a/telepot/loop.py b/telepot/loop.py index 4b1331b..ad13491 100644 --- a/telepot/loop.py +++ b/telepot/loop.py @@ -64,7 +64,10 @@ class GetUpdatesLoop(RunForeverAsThread): # No sort. Trust server to give messages in correct order. for update in result: - self._update_handler(update) + try: + self._update_handler(update) + except exception.UpdateKeyError: + print(f"Update with id {update['update_id']} caused an error, skipping", file=sys.stderr) offset = update['update_id'] + 1 except exception.BadHTTPResponse as e: @@ -100,16 +103,19 @@ def _dictify27(data): _dictify = _dictify3 if sys.version_info >= (3,) else _dictify27 def _extract_message(update): - key = _find_first_key(update, ['message', - 'edited_message', - 'channel_post', - 'edited_channel_post', - 'callback_query', - 'inline_query', - 'chosen_inline_result', - 'shipping_query', - 'pre_checkout_query']) - return key, update[key] + try: + key = _find_first_key(update, ['message', + 'edited_message', + 'channel_post', + 'edited_channel_post', + 'callback_query', + 'inline_query', + 'chosen_inline_result', + 'shipping_query', + 'pre_checkout_query']) + return key, update[key] + except KeyError as ex: + raise exception.UpdateKeyError from ex def _infer_handler_function(bot, h): if h is None: |
