From 0086f5016f1255a8d311ed5c6adfaf8f5c4fcf7e Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sat, 19 Jul 2025 10:58:03 +0300 Subject: Skip updates without expected keys --- .gitignore | 1 + Dockerfile | 1 + telepot/exception.py | 3 +++ telepot/loop.py | 28 +++++++++++++++++----------- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 06f5b02..4f80247 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.pyc .env photo/ +__pycache__/ diff --git a/Dockerfile b/Dockerfile index 32d3d05..e1b9d03 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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: -- cgit v1.3