Skip to content

Commit

Permalink
handle asyncio.TimeoutError, add logging, disable notifications on qu…
Browse files Browse the repository at this point in the history
…estions
  • Loading branch information
wetterkrank committed Jul 19, 2023
1 parent 0af9682 commit 94b9905
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
9 changes: 6 additions & 3 deletions dasbot/broadcaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ def __init__(self, ui, chats_repo, dictionary):
async def send_quiz(self, chat: Chat) -> bool:
"""
:param chat: chat with a pending quiz
:return: `true` if quiz is successfully sent, `false` otherwise
:return: `True` if quiz is successfully sent, `False` otherwise
"""
try:
scores = self.chats_repo.load_scores(chat.id)
chat.quiz = Quiz.new(chat.quiz_length, scores, self.dictionary)
chat.quiz_scheduled_time = util.next_quiz_time(chat.quiz_scheduled_time)
self.chats_repo.save_chat(chat)
if chat.quiz.has_questions:
log.debug("Broadcast: sending message to %s", chat.id)
await self.ui.daily_hello(chat)
await self.ui.ask_question(chat)
await asyncio.sleep(.5) # FYI, TG limit: 30 messages/second
self.chats_repo.save_chat(chat)
await asyncio.sleep(1) # FYI, TG limit: 30 messages/second
return True
else:
return False
Expand All @@ -44,14 +45,16 @@ async def send_quiz(self, chat: Chat) -> bool:
except TelegramAPIError as err:
log.error("Error: %s, chat id: %s", err, chat.id)
return False
except TimeoutError as err:
except (TimeoutError, asyncio.TimeoutError) as err:
log.error("Error: %s", err)
await asyncio.sleep(30)
return False

# Regularly called rouine that sends out the (over)due quizzes
async def broadcast(self):
pending_chats = self.chats_repo.get_pending_chats()
# Consider adding +1 day to overdue chats if bot is started
# after a downtime, so they get their quizzes on preferred time
log.debug("Broadcast: %s pending", len(pending_chats))
sent = 0
for chat in pending_chats:
Expand Down
2 changes: 1 addition & 1 deletion dasbot/db/chats_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def save_chat(self, chat: Chat, update_last_seen=False):
log.debug("saved chat %s, result: %s", chat.id, result.raw_result)
return result

# TODO: return ids instead of full objects, or do it in batches
def get_pending_chats(self, now=None):
"""
:param now: timestamp when the function is called
Expand Down Expand Up @@ -97,6 +98,5 @@ def save_score(self, chat: Chat, word, score):
# log.debug("saved score for chat %s, result: %s", chat.id, result.raw_result)
return result


if __name__ == "__main__":
pass
7 changes: 5 additions & 2 deletions dasbot/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ async def daily_hello(self, chat):
async def ask_question(self, chat):
text = f"{chat.quiz.pos}/{chat.quiz.length}\. "
text += f"What's the article for *{md(chat.quiz.question)}*?"
result = await self.bot.send_message(chat.id, text, reply_markup=Interface.quiz_kb(), parse_mode='MarkdownV2')
result = await self.bot.send_message(chat.id, text,
reply_markup=Interface.quiz_kb(),
parse_mode='MarkdownV2',
disable_notification=True)
log.debug("message sent, result: %s", result)

async def give_hint(self, quiz, message, dictionary):
Expand All @@ -47,7 +50,7 @@ async def give_hint(self, quiz, message, dictionary):
async def give_feedback(self, chat, message, correct):
text = "Correct, " if correct else "❌ Incorrect, "
text += f"*{md(chat.quiz.answer)} {md(chat.quiz.question)}*"
if correct: text += " ✅"
if correct: text += " ✅"
await message.answer(text, parse_mode='MarkdownV2')

async def announce_result(self, chat):
Expand Down

0 comments on commit 94b9905

Please sign in to comment.