Added simple TooManyRequests error handling

This commit is contained in:
vylion 2020-10-26 18:44:29 +01:00
parent f6d0305868
commit 66f3280741
2 changed files with 22 additions and 8 deletions

View file

@ -1,8 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import random import random
import time
from reader import Reader, get_chat_title from reader import Reader, get_chat_title
from telegram.error import TimedOut from telegram.error import TimedOut, NetworkError
def send(bot, cid, text, replying=None, formatting=None, logger=None, **kwargs): def send(bot, cid, text, replying=None, formatting=None, logger=None, **kwargs):
@ -33,10 +34,11 @@ class Speaker(object):
ModeFixed = "FIXED_MODE" ModeFixed = "FIXED_MODE"
ModeChance = "MODE_CHANCE" ModeChance = "MODE_CHANCE"
def __init__(self, username, archivist, logger, nicknames=[], def __init__(self, username, archivist, logger, nicknames=[], mute_time=60,
reply=0.1, repeat=0.05, wakeup=False, mode=ModeFixed reply=0.1, repeat=0.05, wakeup=False, mode=ModeFixed
): ):
self.names = nicknames self.names = nicknames
self.mute_time = mute_time
self.username = username self.username = username
self.archivist = archivist self.archivist = archivist
logger.info("----") logger.info("----")
@ -50,6 +52,7 @@ class Speaker(object):
self.filter_cids = archivist.filter_cids self.filter_cids = archivist.filter_cids
self.bypass = archivist.bypass self.bypass = archivist.bypass
self.current_reader = None self.current_reader = None
self.time_counter = None
def announce(self, bot, announcement, check=(lambda _: True)): def announce(self, bot, announcement, check=(lambda _: True)):
# Sends an announcement to all chats that pass the check # Sends an announcement to all chats that pass the check
@ -100,6 +103,9 @@ class Speaker(object):
return False return False
def should_reply(self, message): def should_reply(self, message):
current_time = int(time.perf_counter())
if self.time_counter is not None and (current_time - self.time_counter) < self.mute_time:
return False
if not self.bypass and self.current_reader.is_restricted(): if not self.bypass and self.current_reader.is_restricted():
user = message.chat.get_member(message.from_user.id) user = message.chat.get_member(message.from_user.id)
if not self.user_is_admin(user): if not self.user_is_admin(user):
@ -179,12 +185,19 @@ class Speaker(object):
self.current_reader.set_period(random.randint(max_period // 4, max_period)) self.current_reader.set_period(random.randint(max_period // 4, max_period))
if random.random() <= self.repeat: if random.random() <= self.repeat:
send(bot, cid, self.speech(), logger=self.logger, **kwargs) send(bot, cid, self.speech(), logger=self.logger, **kwargs)
except TimedOut: except TimedOut as e:
self.current_reader.set_period(self.current_reader.period() + self.archivist.period_inc) self.logger.error("Telegram timed out.")
self.logger.warning("Increased period for chat {} [{}]".format(self.current_reader.title(), cid)) self.logger.exception(e)
except NetworkError as e:
if '429' in e.message:
self.logger.error("Error: TooManyRequests. Going mute for {} seconds.".format(self.mute_time))
self.time_counter = int(time.perf_counter())
else:
self.logger.error("Sending a message caused network error:")
self.logger.exception(e)
except Exception as e: except Exception as e:
self.logger.error("Sending a message caused error:") self.logger.error("Sending a message caused exception:")
raise e self.logger.exception(e)
def get_count(self, update, context): def get_count(self, update, context):
cid = str(update.message.chat.id) cid = str(update.message.chat.id)

View file

@ -58,7 +58,8 @@ def static_reply(text, format=None):
def error(update, context): def error(update, context):
logger.warning('The following update:\n"{}"\n\nCaused the following error:\n"{}"'.format(update, context.error)) logger.warning('The following update:\n"{}"\n\nCaused the following error:\n'.format(update))
logger.exception(context.error)
# raise error # raise error