From 103ce35594164d194955c05687382abf68ee3b5c Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 6 Aug 2025 19:37:30 +0300 Subject: [PATCH 1/2] feat: enhance message display with reply information Updated message formatting in various functions to include details about replies. When a message is a reply, it now shows the ID of the original message and the sender's name, improving context for users. --- main.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 4cbbc5b..dde76bc 100644 --- a/main.py +++ b/main.py @@ -229,7 +229,10 @@ async def get_messages(chat_id: int, page: int = 1, page_size: int = 20) -> str: return "No messages found for this page." lines = [] for msg in messages: - lines.append(f"ID: {msg.id} | Date: {msg.date} | Message: {msg.message}") + reply_info = "" + if msg.reply_to and msg.reply_to.reply_to_msg_id: + reply_info = f" | reply to {msg.reply_to.reply_to_msg_id}" + lines.append(f"ID: {msg.id} | Date: {msg.date}{reply_info} | Message: {msg.message}") return "\n".join(lines) except Exception as e: return log_and_format_error( @@ -407,8 +410,12 @@ async def list_messages( ) sender = f"{sender_name} | " + reply_info = "" + if msg.reply_to and msg.reply_to.reply_to_msg_id: + reply_info = f" | reply to {msg.reply_to.reply_to_msg_id}" + lines.append( - f"ID: {msg.id} | {sender}Date: {msg.date} | Message: {msg.message or '[Media/No text]'}" + f"ID: {msg.id} | {sender}Date: {msg.date}{reply_info} | Message: {msg.message or '[Media/No text]'}" ) return "\n".join(lines) @@ -736,8 +743,24 @@ async def get_message_context(chat_id: int, message_id: int, context_size: int = msg.sender, "title", "Unknown" ) highlight = " [THIS MESSAGE]" if msg.id == message_id else "" + + # Check if this message is a reply and get the replied message + reply_content = "" + if msg.reply_to and msg.reply_to.reply_to_msg_id: + try: + replied_msg = await client.get_messages(chat, ids=msg.reply_to.reply_to_msg_id) + if replied_msg: + replied_sender = "Unknown" + if replied_msg.sender: + replied_sender = getattr(replied_msg.sender, "first_name", "") or getattr( + replied_msg.sender, "title", "Unknown" + ) + reply_content = f" | reply to {msg.reply_to.reply_to_msg_id}\n → Replied message: [{replied_sender}] {replied_msg.message or '[Media/No text]'}" + except Exception: + reply_content = f" | reply to {msg.reply_to.reply_to_msg_id} (original message not found)" + results.append( - f"ID: {msg.id} | {sender_name} | {msg.date}{highlight}\n{msg.message or '[Media/No text]'}\n" + f"ID: {msg.id} | {sender_name} | {msg.date}{highlight}{reply_content}\n{msg.message or '[Media/No text]'}\n" ) return "\n".join(results) except Exception as e: @@ -2028,7 +2051,13 @@ async def search_messages(chat_id: int, query: str, limit: int = 20) -> str: try: entity = await client.get_entity(chat_id) messages = await client.get_messages(entity, limit=limit, search=query) - return "\n".join([f"ID: {m.id} | {m.date} | {m.message}" for m in messages]) + lines = [] + for m in messages: + reply_info = "" + if m.reply_to and m.reply_to.reply_to_msg_id: + reply_info = f" | reply to {m.reply_to.reply_to_msg_id}" + lines.append(f"ID: {m.id} | {m.date}{reply_info} | {m.message}") + return "\n".join(lines) except Exception as e: return log_and_format_error( "search_messages", e, chat_id=chat_id, query=query, limit=limit @@ -2354,7 +2383,13 @@ async def get_history(chat_id: int, limit: int = 100) -> str: try: entity = await client.get_entity(chat_id) messages = await client.get_messages(entity, limit=limit) - return "\n".join([f"ID: {m.id} | {m.date} | {m.message}" for m in messages]) + lines = [] + for m in messages: + reply_info = "" + if m.reply_to and m.reply_to.reply_to_msg_id: + reply_info = f" | reply to {m.reply_to.reply_to_msg_id}" + lines.append(f"ID: {m.id} | {m.date}{reply_info} | {m.message}") + return "\n".join(lines) except Exception as e: return log_and_format_error("get_history", e, chat_id=chat_id, limit=limit) @@ -2429,9 +2464,13 @@ async def get_pinned_messages(chat_id: int) -> str: if not messages: return "No pinned messages found in this chat." - return "\n".join( - [f"ID: {m.id} | {m.date} | {m.message or '[Media/No text]'}" for m in messages] - ) + lines = [] + for m in messages: + reply_info = "" + if m.reply_to and m.reply_to.reply_to_msg_id: + reply_info = f" | reply to {m.reply_to.reply_to_msg_id}" + lines.append(f"ID: {m.id} | {m.date}{reply_info} | {m.message or '[Media/No text]'}") + return "\n".join(lines) except Exception as e: logger.exception(f"get_pinned_messages failed (chat_id={chat_id})") return log_and_format_error("get_pinned_messages", e, chat_id=chat_id) From 78a99b06f294e8e21c20ed87e15637a8caeb555d Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 6 Aug 2025 21:07:59 +0300 Subject: [PATCH 2/2] refactor: clean up whitespace and improve code formatting using black . --- main.py | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index 815d6b3..6fb590e 100644 --- a/main.py +++ b/main.py @@ -190,14 +190,14 @@ def get_sender_name(message) -> str: """Helper function to get sender name from a message.""" if not message.sender: return "Unknown" - + # Check for group/channel title first - if hasattr(message.sender, 'title') and message.sender.title: + if hasattr(message.sender, "title") and message.sender.title: return message.sender.title - elif hasattr(message.sender, 'first_name'): + elif hasattr(message.sender, "first_name"): # User sender - first_name = getattr(message.sender, 'first_name', '') or '' - last_name = getattr(message.sender, 'last_name', '') or '' + first_name = getattr(message.sender, "first_name", "") or "" + last_name = getattr(message.sender, "last_name", "") or "" full_name = f"{first_name} {last_name}".strip() return full_name if full_name else "Unknown" else: @@ -251,7 +251,9 @@ async def get_messages(chat_id: int, page: int = 1, page_size: int = 20) -> str: reply_info = "" if msg.reply_to and msg.reply_to.reply_to_msg_id: reply_info = f" | reply to {msg.reply_to.reply_to_msg_id}" - lines.append(f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info} | Message: {msg.message}") + lines.append( + f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info} | Message: {msg.message}" + ) return "\n".join(lines) except Exception as e: return log_and_format_error( @@ -423,11 +425,11 @@ async def list_messages( lines = [] for msg in messages: sender_name = get_sender_name(msg) - message_text = msg.message or '[Media/No text]' + message_text = msg.message or "[Media/No text]" reply_info = "" if msg.reply_to and msg.reply_to.reply_to_msg_id: reply_info = f" | reply to {msg.reply_to.reply_to_msg_id}" - + lines.append( f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info} | Message: {message_text}" ) @@ -753,7 +755,7 @@ async def get_message_context(chat_id: int, message_id: int, context_size: int = for msg in all_messages: sender_name = get_sender_name(msg) highlight = " [THIS MESSAGE]" if msg.id == message_id else "" - + # Check if this message is a reply and get the replied message reply_content = "" if msg.reply_to and msg.reply_to.reply_to_msg_id: @@ -762,13 +764,15 @@ async def get_message_context(chat_id: int, message_id: int, context_size: int = if replied_msg: replied_sender = "Unknown" if replied_msg.sender: - replied_sender = getattr(replied_msg.sender, "first_name", "") or getattr( - replied_msg.sender, "title", "Unknown" - ) + replied_sender = getattr( + replied_msg.sender, "first_name", "" + ) or getattr(replied_msg.sender, "title", "Unknown") reply_content = f" | reply to {msg.reply_to.reply_to_msg_id}\n → Replied message: [{replied_sender}] {replied_msg.message or '[Media/No text]'}" except Exception: - reply_content = f" | reply to {msg.reply_to.reply_to_msg_id} (original message not found)" - + reply_content = ( + f" | reply to {msg.reply_to.reply_to_msg_id} (original message not found)" + ) + results.append( f"ID: {msg.id} | {sender_name} | {msg.date}{highlight}{reply_content}\n{msg.message or '[Media/No text]'}\n" ) @@ -2067,7 +2071,9 @@ async def search_messages(chat_id: int, query: str, limit: int = 20) -> str: reply_info = "" if msg.reply_to and msg.reply_to.reply_to_msg_id: reply_info = f" | reply to {msg.reply_to.reply_to_msg_id}" - lines.append(f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info} | Message: {msg.message}") + lines.append( + f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info} | Message: {msg.message}" + ) return "\n".join(lines) except Exception as e: return log_and_format_error( @@ -2400,7 +2406,9 @@ async def get_history(chat_id: int, limit: int = 100) -> str: reply_info = "" if msg.reply_to and msg.reply_to.reply_to_msg_id: reply_info = f" | reply to {msg.reply_to.reply_to_msg_id}" - lines.append(f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info} | Message: {msg.message}") + lines.append( + f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info} | Message: {msg.message}" + ) return "\n".join(lines) except Exception as e: return log_and_format_error("get_history", e, chat_id=chat_id, limit=limit) @@ -2482,8 +2490,10 @@ async def get_pinned_messages(chat_id: int) -> str: reply_info = "" if msg.reply_to and msg.reply_to.reply_to_msg_id: reply_info = f" | reply to {msg.reply_to.reply_to_msg_id}" - lines.append(f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info} | Message: {msg.message or '[Media/No text]'}") - + lines.append( + f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info} | Message: {msg.message or '[Media/No text]'}" + ) + return "\n".join(lines) except Exception as e: logger.exception(f"get_pinned_messages failed (chat_id={chat_id})")