summaryrefslogtreecommitdiffstats
path: root/telepot
diff options
context:
space:
mode:
authorJan Tuomi <jan@jantuomi.fi>2025-07-19 10:58:03 +0300
committerJan Tuomi <jan@jantuomi.fi>2025-07-19 10:58:03 +0300
commit0086f5016f1255a8d311ed5c6adfaf8f5c4fcf7e (patch)
tree69a61f133785e3c2758b66be358ce755aee8856d /telepot
parent5a636abf7e98a5687811167917d0e07103599bc8 (diff)
Skip updates without expected keys
Diffstat (limited to 'telepot')
-rw-r--r--telepot/exception.py3
-rw-r--r--telepot/loop.py28
2 files changed, 20 insertions, 11 deletions
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: