feat: add engagement metrics (views, forwards, reactions) to message output

Adds views, forwards, and reaction counts to message output for channel posts.
These metrics are available in Telegram's API for channel content.

Changes:
- get_messages: Include engagement info in output
- list_messages: Include engagement info in output

Example output: 'views:1234, forwards:56, reactions:78'

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Bayram Annakov 2025-12-26 13:58:56 -08:00
parent 3d8027fe77
commit 6a05d7a661

26
main.py
View file

@ -317,6 +317,23 @@ def get_sender_name(message) -> str:
return "Unknown"
def get_engagement_info(message) -> str:
"""Helper function to get engagement metrics (views, forwards, reactions) from a message."""
engagement_parts = []
views = getattr(message, "views", None)
if views is not None:
engagement_parts.append(f"views:{views}")
forwards = getattr(message, "forwards", None)
if forwards is not None:
engagement_parts.append(f"forwards:{forwards}")
reactions = getattr(message, "reactions", None)
if reactions is not None:
results = getattr(reactions, "results", None)
total_reactions = sum(getattr(r, "count", 0) or 0 for r in results) if results else 0
engagement_parts.append(f"reactions:{total_reactions}")
return f" | {', '.join(engagement_parts)}" if engagement_parts else ""
@mcp.tool(annotations=ToolAnnotations(title="Get Chats", openWorldHint=True, readOnlyHint=True))
async def get_chats(page: int = 1, page_size: int = 20) -> str:
"""
@ -365,8 +382,11 @@ async def get_messages(chat_id: Union[int, str], page: int = 1, page_size: int =
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}"
engagement_info = get_engagement_info(msg)
lines.append(
f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info} | Message: {msg.message}"
f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info}{engagement_info} | Message: {msg.message}"
)
return "\n".join(lines)
except Exception as e:
@ -796,8 +816,10 @@ async def list_messages(
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} | Message: {message_text}"
f"ID: {msg.id} | {sender_name} | Date: {msg.date}{reply_info}{engagement_info} | Message: {message_text}"
)
return "\n".join(lines)