fix: return JSON from get_messages to preserve multi-line message text

The pipe-delimited text format (ID: N | ... | Message: <text>) breaks when
messages contain newlines, since \n serves as both record separator and can
appear inside message text. Downstream parsers that split on \n only capture
the first line

Switch to a JSON array output with fields: id, sender, date, text,
reply_to (optional), and engagement (optional, with views/forwards/reactions)

This is a breaking change for MCP clients that parse the old text format.
Since MCP tool results are typically consumed by LLMs which handle JSON
natively, this should be transparent for most consumers
This commit is contained in:
Ivan Kolodrivskyi 2026-04-09 08:16:44 +03:00
parent e96a6a8ca2
commit ff37abdb01

35
main.py
View file

@ -727,19 +727,34 @@ async def get_messages(chat_id: Union[int, str], page: int = 1, page_size: int =
messages = await client.get_messages(entity, limit=page_size, add_offset=offset)
if not messages:
return "No messages found for this page."
lines = []
result = []
for msg in messages:
sender_name = get_sender_name(msg)
reply_info = ""
entry = {
"id": msg.id,
"sender": sender_name,
"date": str(msg.date),
"text": msg.message,
}
if msg.reply_to and msg.reply_to.reply_to_msg_id:
reply_info = f" | reply to {msg.reply_to.reply_to_msg_id}"
engagement_info = get_engagement_info(msg)
lines.append(
f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info}{engagement_info} | Message: {msg.message}"
)
return "\n".join(lines)
entry["reply_to"] = msg.reply_to.reply_to_msg_id
# Engagement metrics (views, forwards, reactions)
engagement = {}
views = getattr(msg, "views", None)
if views is not None:
engagement["views"] = views
forwards = getattr(msg, "forwards", None)
if forwards is not None:
engagement["forwards"] = forwards
reactions = getattr(msg, "reactions", None)
if reactions is not None:
results = getattr(reactions, "results", None)
if results:
engagement["reactions"] = sum(getattr(r, "count", 0) or 0 for r in results)
if engagement:
entry["engagement"] = engagement
result.append(entry)
return json.dumps(result, ensure_ascii=False, default=json_serializer)
except Exception as e:
return log_and_format_error(
"get_messages", e, chat_id=chat_id, page=page, page_size=page_size