Skip to content

Commit aaa4f28

Browse files
committed
feat(messages): forward list of ids and expose grouped_id
`forward_message` now accepts `Union[int, List[int]]` for `message_id`. When given a list, Telethon's `forward_messages` preserves Telegram album grouping (messages sharing a `grouped_id`) in the destination chat, which was impossible with the previous single-int signature. `list_messages` and `get_message_context` now include `grouped_id` in each record when present, so callers can detect album membership and forward all parts atomically. Motivated by an agent use case: forwarding multi-photo posts (3+ photos with a caption) produced one detached photo at a time. Without `grouped_id` exposure there was no way to discover sibling messages, and without list-id forward there was no way to preserve the album.
1 parent 479230d commit aaa4f28

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

telegram_mcp/tools/messages.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,9 @@ async def list_messages(
576576
"date": msg.date,
577577
"text": sanitize_user_content(msg.message),
578578
}
579+
grouped_id = getattr(msg, "grouped_id", None)
580+
if grouped_id is not None:
581+
record["grouped_id"] = grouped_id
579582
reply_to_id = getattr(msg.reply_to, "reply_to_msg_id", None) if msg.reply_to else None
580583
if reply_to_id:
581584
record["reply_to"] = reply_to_id
@@ -639,6 +642,9 @@ async def get_message_context(
639642
"is_target": msg.id == message_id,
640643
"text": sanitize_user_content(msg.message),
641644
}
645+
grouped_id = getattr(msg, "grouped_id", None)
646+
if grouped_id is not None:
647+
record["grouped_id"] = grouped_id
642648

643649
# Check if this message is a reply and get the replied message
644650
if msg.reply_to and msg.reply_to.reply_to_msg_id:
@@ -683,19 +689,26 @@ async def get_message_context(
683689
@validate_id("from_chat_id", "to_chat_id")
684690
async def forward_message(
685691
from_chat_id: Union[int, str],
686-
message_id: int,
692+
message_id: Union[int, List[int]],
687693
to_chat_id: Union[int, str],
688694
account: str = None,
689695
) -> str:
690696
"""
691-
Forward a message from one chat to another.
697+
Forward one or more messages from one chat to another.
698+
699+
Pass a list of message IDs to forward multiple messages in a single call.
700+
Messages that share a Telegram album (same grouped_id) are preserved as a
701+
grouped album in the destination when forwarded together in one list.
692702
"""
693703
try:
694704
cl = get_client(account)
695705
from_entity = await resolve_entity(from_chat_id, cl)
696706
to_entity = await resolve_entity(to_chat_id, cl)
697707
await cl.forward_messages(to_entity, message_id, from_entity)
698-
return f"Message {message_id} forwarded from {from_chat_id} to {to_chat_id}."
708+
count = len(message_id) if isinstance(message_id, list) else 1
709+
if count == 1:
710+
return f"Message {message_id} forwarded from {from_chat_id} to {to_chat_id}."
711+
return f"{count} messages forwarded from {from_chat_id} to {to_chat_id}."
699712
except Exception as e:
700713
return log_and_format_error(
701714
"forward_message",

0 commit comments

Comments
 (0)