Skip to content

Commit 8da0575

Browse files
authored
Debug issue #32
1 parent c51f00a commit 8da0575

1 file changed

Lines changed: 38 additions & 35 deletions

File tree

tg_bot/__main__.py

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -639,51 +639,54 @@ def process_update(self, update):
639639

640640
now = datetime.datetime.utcnow()
641641
LOGGER.info(update)
642-
cnt = CHATS_CNT.get(update.effective_chat.id, 0)
642+
try:
643+
cnt = CHATS_CNT.get(update.effective_chat.id, 0)
643644

644-
t = CHATS_TIME.get(update.effective_chat.id, datetime.datetime(1970, 1, 1))
645-
if t and now > t + datetime.timedelta(0, 1):
646-
CHATS_TIME[update.effective_chat.id] = now
647-
cnt = 0
648-
else:
649-
cnt += 1
645+
t = CHATS_TIME.get(update.effective_chat.id, datetime.datetime(1970, 1, 1))
646+
if t and now > t + datetime.timedelta(0, 1):
647+
CHATS_TIME[update.effective_chat.id] = now
648+
cnt = 0
649+
else:
650+
cnt += 1
650651

651-
if cnt > 10:
652-
return
652+
if cnt > 10:
653+
return
653654

654-
CHATS_CNT[update.effective_chat.id] = cnt
655-
for group in self.groups:
656-
try:
657-
for handler in (x for x in self.handlers[group] if x.check_update(update)):
658-
handler.handle_update(update, self)
655+
CHATS_CNT[update.effective_chat.id] = cnt
656+
for group in self.groups:
657+
try:
658+
for handler in (x for x in self.handlers[group] if x.check_update(update)):
659+
handler.handle_update(update, self)
660+
break
661+
662+
# Stop processing with any other handler.
663+
except DispatcherHandlerStop:
664+
self.logger.debug("Stopping further handlers due to DispatcherHandlerStop")
659665
break
660666

661-
# Stop processing with any other handler.
662-
except DispatcherHandlerStop:
663-
self.logger.debug("Stopping further handlers due to DispatcherHandlerStop")
664-
break
667+
# Dispatch any error.
668+
except TelegramError as te:
669+
self.logger.warning(
670+
"A TelegramError was raised while processing the Update"
671+
)
665672

666-
# Dispatch any error.
667-
except TelegramError as te:
668-
self.logger.warning(
669-
"A TelegramError was raised while processing the Update"
670-
)
673+
try:
674+
self.dispatch_error(update, te)
675+
except DispatcherHandlerStop:
676+
self.logger.debug("Error handler stopped further handlers")
677+
break
678+
except Exception:
679+
self.logger.exception(
680+
"An uncaught error was raised while handling the error"
681+
)
671682

672-
try:
673-
self.dispatch_error(update, te)
674-
except DispatcherHandlerStop:
675-
self.logger.debug("Error handler stopped further handlers")
676-
break
683+
# Errors should not stop the thread.
677684
except Exception:
678685
self.logger.exception(
679-
"An uncaught error was raised while handling the error"
686+
"An uncaught error was raised while processing the update"
680687
)
681-
682-
# Errors should not stop the thread.
683-
except Exception:
684-
self.logger.exception(
685-
"An uncaught error was raised while processing the update"
686-
)
688+
except KeyError:
689+
LOGGER.info("We have received an invalid update from telegram, skipping")
687690

688691

689692
if __name__ == "__main__":

0 commit comments

Comments
 (0)