From 129f5bf1f8bcc5f1d25bf36b4ec0add7538ea326 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 17 May 2018 12:08:52 +0200 Subject: [PATCH] Add an additional check to avoid duplicate iter_messages --- telethon/telegram_client.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index a7421241..4cc3de08 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -1137,6 +1137,7 @@ class TelegramClient(TelegramBareClient): wait_time = 1 if limit > 3000 else 0 have = 0 + last_id = float('inf') batch_size = min(max(batch_size, 1), 100) while have < limit: start = time.time() @@ -1150,9 +1151,15 @@ class TelegramClient(TelegramBareClient): for x in itertools.chain(r.users, r.chats)} for message in r.messages: - if isinstance(message, MessageEmpty): + if isinstance(message, MessageEmpty) or message.id >= last_id: continue + # There has been reports that on bad connections this method + # was returning duplicated IDs sometimes. Using ``last_id`` + # is an attempt to avoid these duplicates, since the message + # IDs are returned in descending order. + last_id = message.id + # Add a few extra attributes to the Message to be friendlier. # To make messages more friendly, always add message # to service messages, and action to normal messages.