mirror of
https://github.com/Steffo99/greed.git
synced 2024-11-25 15:24:17 +00:00
Handle get_updates errors
This commit is contained in:
parent
aec1391b17
commit
b93b615d37
1 changed files with 35 additions and 2 deletions
37
core.py
37
core.py
|
@ -29,11 +29,44 @@ def main():
|
||||||
# Current update offset; if None it will get the last 100 unparsed messages
|
# Current update offset; if None it will get the last 100 unparsed messages
|
||||||
next_update = None
|
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
|
# Main loop of the program
|
||||||
while True:
|
while True:
|
||||||
# Get a new batch of 100 updates and mark the last 100 parsed as read
|
# Get a new batch of 100 updates and mark the last 100 parsed as read
|
||||||
# TODO: handle possible errors
|
try:
|
||||||
updates = bot.get_updates(offset=next_update, timeout=int(config["Telegram"]["long_polling_timeout"]))
|
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
|
# Parse all the updates
|
||||||
for update in updates:
|
for update in updates:
|
||||||
# If the update is a message...
|
# If the update is a message...
|
||||||
|
|
Loading…
Reference in a new issue