From 0755bda2208360c496c264b7840e6d16299f4a0e Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 28 Dec 2017 02:01:22 +0100 Subject: [PATCH] Stop returning tuples off .get_message_history() Now the information is saved in the modified Message instances, which makes it easier to use (message.sender, message.to...) --- telethon/telegram_client.py | 47 ++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 2f48f7a7..a5ea1025 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -484,8 +484,12 @@ class TelegramClient(TelegramBareClient): Additional message offset (all of the specified offsets + this offset = older messages). - :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! + :return: A list of messages with extra attributes: + .total = total amount of messages in this history + .sender = entity of the sender + .fwd_from.sender = if fwd_from, who sent it originally + .fwd_from.channel = if fwd_from, original channel + .to = entity to which the message was sent """ entity = self.get_input_entity(entity) limit = float('inf') if limit is None else int(limit) @@ -537,25 +541,30 @@ class TelegramClient(TelegramBareClient): if limit > 3000: time.sleep(1) - # In a new list with the same length as the messages append - # their senders, so people can zip(messages, senders). - senders = [] + # Add a few extra attributes to the Message to make it friendlier. for m in messages: - if m.from_id: - who = entities[utils.get_peer_id(m.from_id, add_mark=True)] - elif getattr(m, 'fwd_from', None): - # .from_id is optional, so this is the sanest fallback. - who = entities[utils.get_peer_id( - m.fwd_from.from_id or PeerChannel(m.fwd_from.channel_id), - add_mark=True - )] - else: - # If there's not even a FwdHeader, fallback to the sender - # being where the message was sent. - who = entities[utils.get_peer_id(m.to_id, add_mark=True)] - senders.append(who) + # TODO Better way to return a total without tuples? + m.total = total_messages + m.sender = (None if not m.from_id else + entities[utils.get_peer_id(m.from_id, add_mark=True)]) - return total_messages, messages, senders + if getattr(m, 'fwd_from', None): + m.fwd_from.sender = ( + None if not m.fwd_from.from_id else + entities[utils.get_peer_id( + m.fwd_from.from_id, add_mark=True + )] + ) + m.fwd_from.channel = ( + None if not m.fwd_from.channel_id else + entities[utils.get_peer_id( + PeerChannel(m.fwd_from.channel_id), add_mark=True + )] + ) + + m.to = entities[utils.get_peer_id(m.to_id, add_mark=True)] + + return messages def send_read_acknowledge(self, entity, message=None, max_id=None): """