From 9a12738f0ed60f814c50bbc856032a20876f8d3b Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 31 Oct 2017 13:52:43 +0100 Subject: [PATCH] Fix .get_message_history not working with limit=0 --- telethon/telegram_client.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 994a59cc..0452b72b 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -485,7 +485,16 @@ class TelegramClient(TelegramBareClient): :return: A tuple containing total message count and two more lists ([messages], [senders]). Note that the sender can be null if it was not found! """ + entity = self.get_input_entity(entity) limit = float('inf') if limit is None else int(limit) + if limit == 0: + # No messages, but we still need to know the total message count + result = self(GetHistoryRequest( + peer=self.get_input_entity(entity), limit=1, + offset_date=None, offset_id=0, max_id=0, min_id=0, add_offset=0 + )) + return getattr(result, 'count', len(result.messages)), [], [] + total_messages = 0 messages = [] entities = {} @@ -493,7 +502,7 @@ class TelegramClient(TelegramBareClient): # Telegram has a hard limit of 100 real_limit = min(limit - len(messages), 100) result = self(GetHistoryRequest( - peer=self.get_input_entity(entity), + peer=entity, limit=real_limit, offset_date=offset_date, offset_id=offset_id,