refactor: clean up whitespace and improve code formatting using black .
This commit is contained in:
parent
2dc8454a4f
commit
78a99b06f2
1 changed files with 28 additions and 18 deletions
46
main.py
46
main.py
|
|
@ -190,14 +190,14 @@ def get_sender_name(message) -> str:
|
||||||
"""Helper function to get sender name from a message."""
|
"""Helper function to get sender name from a message."""
|
||||||
if not message.sender:
|
if not message.sender:
|
||||||
return "Unknown"
|
return "Unknown"
|
||||||
|
|
||||||
# Check for group/channel title first
|
# 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
|
return message.sender.title
|
||||||
elif hasattr(message.sender, 'first_name'):
|
elif hasattr(message.sender, "first_name"):
|
||||||
# User sender
|
# User sender
|
||||||
first_name = getattr(message.sender, 'first_name', '') or ''
|
first_name = getattr(message.sender, "first_name", "") or ""
|
||||||
last_name = getattr(message.sender, 'last_name', '') or ''
|
last_name = getattr(message.sender, "last_name", "") or ""
|
||||||
full_name = f"{first_name} {last_name}".strip()
|
full_name = f"{first_name} {last_name}".strip()
|
||||||
return full_name if full_name else "Unknown"
|
return full_name if full_name else "Unknown"
|
||||||
else:
|
else:
|
||||||
|
|
@ -251,7 +251,9 @@ async def get_messages(chat_id: int, page: int = 1, page_size: int = 20) -> str:
|
||||||
reply_info = ""
|
reply_info = ""
|
||||||
if msg.reply_to and msg.reply_to.reply_to_msg_id:
|
if msg.reply_to and msg.reply_to.reply_to_msg_id:
|
||||||
reply_info = f" | reply to {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)
|
return "\n".join(lines)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return log_and_format_error(
|
return log_and_format_error(
|
||||||
|
|
@ -423,11 +425,11 @@ async def list_messages(
|
||||||
lines = []
|
lines = []
|
||||||
for msg in messages:
|
for msg in messages:
|
||||||
sender_name = get_sender_name(msg)
|
sender_name = get_sender_name(msg)
|
||||||
message_text = msg.message or '[Media/No text]'
|
message_text = msg.message or "[Media/No text]"
|
||||||
reply_info = ""
|
reply_info = ""
|
||||||
if msg.reply_to and msg.reply_to.reply_to_msg_id:
|
if msg.reply_to and msg.reply_to.reply_to_msg_id:
|
||||||
reply_info = f" | reply to {msg.reply_to.reply_to_msg_id}"
|
reply_info = f" | reply to {msg.reply_to.reply_to_msg_id}"
|
||||||
|
|
||||||
lines.append(
|
lines.append(
|
||||||
f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info} | Message: {message_text}"
|
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:
|
for msg in all_messages:
|
||||||
sender_name = get_sender_name(msg)
|
sender_name = get_sender_name(msg)
|
||||||
highlight = " [THIS MESSAGE]" if msg.id == message_id else ""
|
highlight = " [THIS MESSAGE]" if msg.id == message_id else ""
|
||||||
|
|
||||||
# Check if this message is a reply and get the replied message
|
# Check if this message is a reply and get the replied message
|
||||||
reply_content = ""
|
reply_content = ""
|
||||||
if msg.reply_to and msg.reply_to.reply_to_msg_id:
|
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:
|
if replied_msg:
|
||||||
replied_sender = "Unknown"
|
replied_sender = "Unknown"
|
||||||
if replied_msg.sender:
|
if replied_msg.sender:
|
||||||
replied_sender = getattr(replied_msg.sender, "first_name", "") or getattr(
|
replied_sender = getattr(
|
||||||
replied_msg.sender, "title", "Unknown"
|
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]'}"
|
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:
|
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(
|
results.append(
|
||||||
f"ID: {msg.id} | {sender_name} | {msg.date}{highlight}{reply_content}\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"
|
||||||
)
|
)
|
||||||
|
|
@ -2067,7 +2071,9 @@ async def search_messages(chat_id: int, query: str, limit: int = 20) -> str:
|
||||||
reply_info = ""
|
reply_info = ""
|
||||||
if msg.reply_to and msg.reply_to.reply_to_msg_id:
|
if msg.reply_to and msg.reply_to.reply_to_msg_id:
|
||||||
reply_info = f" | reply to {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)
|
return "\n".join(lines)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return log_and_format_error(
|
return log_and_format_error(
|
||||||
|
|
@ -2400,7 +2406,9 @@ async def get_history(chat_id: int, limit: int = 100) -> str:
|
||||||
reply_info = ""
|
reply_info = ""
|
||||||
if msg.reply_to and msg.reply_to.reply_to_msg_id:
|
if msg.reply_to and msg.reply_to.reply_to_msg_id:
|
||||||
reply_info = f" | reply to {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)
|
return "\n".join(lines)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return log_and_format_error("get_history", e, chat_id=chat_id, limit=limit)
|
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 = ""
|
reply_info = ""
|
||||||
if msg.reply_to and msg.reply_to.reply_to_msg_id:
|
if msg.reply_to and msg.reply_to.reply_to_msg_id:
|
||||||
reply_info = f" | reply to {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)
|
return "\n".join(lines)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(f"get_pinned_messages failed (chat_id={chat_id})")
|
logger.exception(f"get_pinned_messages failed (chat_id={chat_id})")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue