diff --git a/core.py b/core.py index d11d961..4fcff46 100644 --- a/core.py +++ b/core.py @@ -29,11 +29,44 @@ def main(): # Current update offset; if None it will get the last 100 unparsed messages next_update = None + # TimedOut / NetworkError counter, increases by 1 every time get_updates fails and resets to 0 when it succedes + timed_out_counter = 0 + # Main loop of the program while True: # Get a new batch of 100 updates and mark the last 100 parsed as read - # TODO: handle possible errors - updates = bot.get_updates(offset=next_update, timeout=int(config["Telegram"]["long_polling_timeout"])) + try: + updates = bot.get_updates(offset=next_update, timeout=int(config["Telegram"]["long_polling_timeout"])) + # If the method times out... + except telegram.error.TimedOut: + # Increase the TimedOut counter + timed_out_counter += 1 + # Notify on stdout + print(f"WARNING: get_updates timed out ({timed_out_counter} time{'s' if timed_out_counter != 1 else ''})") + # Try again + continue + # If the method raises a NetworkError (connection problems)... + except telegram.error.NetworkError: + # Increase the TimedOut counter + timed_out_counter += 1 + # Notify on stdout + print(f"ERROR: get_updates raised a NetworkError ({timed_out_counter} time{'s' if timed_out_counter != 1 else ''})") + # Wait some time before retrying + time.sleep(3) + continue + # If Telegram returns an error... + except telegram.error.TelegramError as e: + # Increase the TimedOut counter + timed_out_counter += 1 + # Notify on stdout + print(f"ERROR: telegram returned an error while trying to get_updates ({e.message}) ({timed_out_counter} time{'s' if timed_out_counter != 1 else ''})") + # Wait some time before retrying + time.sleep(3) + continue + # If all goes well... + else: + # Reset the TimedOut counter + timed_out_counter = 0 # Parse all the updates for update in updates: # If the update is a message...