1
Fork 0
mirror of https://github.com/Steffo99/greed.git synced 2024-11-22 05:54:18 +00:00

Handle get_updates errors

This commit is contained in:
Steffo 2017-12-14 10:43:44 +01:00
parent aec1391b17
commit b93b615d37
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: C27544372FBB445D

37
core.py
View file

@ -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...