From 7f84374e98a57569eba66745b10fbd177f1be4b7 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 26 May 2017 16:39:59 +0200 Subject: [PATCH] Attempt at fixing msg_id too low/high (#95) --- telethon/network/mtproto_sender.py | 11 ++++++++++- telethon/tl/session.py | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/telethon/network/mtproto_sender.py b/telethon/network/mtproto_sender.py index ecefe2e5..67fab922 100644 --- a/telethon/network/mtproto_sender.py +++ b/telethon/network/mtproto_sender.py @@ -333,7 +333,16 @@ class MtProtoSender: reader.read_int() # request_sequence error_code = reader.read_int() - raise BadMessageError(error_code) + error = BadMessageError(error_code) + if error_code in (16, 17): + # sent msg_id too low or too high (respectively). + # Use the current msg_id to determine the right time offset. + self.session.update_time_offset(correct_msg_id=msg_id) + self.session.save() + self.logger.warning('Read Bad Message error: ' + str(error)) + self.logger.info('Attempting to use the correct time offset.') + else: + raise error def handle_rpc_result(self, msg_id, sequence, reader, request): self.logger.debug('Handling RPC result, request is%s None', ' not' if request else '') diff --git a/telethon/tl/session.py b/telethon/tl/session.py index 54409419..b910b4c2 100644 --- a/telethon/tl/session.py +++ b/telethon/tl/session.py @@ -65,3 +65,9 @@ class Session: self.last_message_id = new_msg_id return new_msg_id + + def update_time_offset(self, correct_msg_id): + """Updates the time offset based on a known correct message ID""" + now = int(time.time()) + correct = correct_msg_id >> 32 + self.time_offset = correct - now