diff --git a/README.md b/README.md index 3109466..3aa2393 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ This MCP server exposes a huge suite of Telegram tools. **Every major Telegram/T ### Search & Discovery - **search_public_chats(query, limit)**: Search public chats/channels/bots with a configurable result limit - **search_messages(chat_id, query, limit)**: Search messages in a chat +- **search_global(query, page, page_size)**: Search messages globally with pagination - **resolve_username(username)**: Resolve a username to ID ### Stickers, GIFs, Bots diff --git a/main.py b/main.py index ed93b46..2785951 100644 --- a/main.py +++ b/main.py @@ -3236,6 +3236,45 @@ async def search_messages(chat_id: Union[int, str], query: str, limit: int = 20) ) +@mcp.tool( + annotations=ToolAnnotations( + title="Search Global Messages", + openWorldHint=True, + readOnlyHint=True, + ) +) +async def search_global(query: str, page: int = 1, page_size: int = 20) -> str: + """ + Search for messages across all public chats and channels by text content. + """ + try: + offset = (page - 1) * page_size + messages = await client.get_messages( + None, limit=page_size, search=query, add_offset=offset + ) + + if not messages: + return "No messages found for this page." + + lines = [] + for msg in messages: + chat = msg.chat + chat_name = ( + getattr(chat, "title", None) or getattr(chat, "first_name", "") or str(msg.chat_id) + ) + sender_name = get_sender_name(msg) + lines.append( + f"Chat: {chat_name} | ID: {msg.id} | {sender_name} | " + f"Date: {msg.date} | Message: {msg.message}" + ) + + return "\n".join(lines) + except Exception as e: + return log_and_format_error( + "search_global", e, query=query, page=page, page_size=page_size + ) + + @mcp.tool( annotations=ToolAnnotations(title="Resolve Username", openWorldHint=True, readOnlyHint=True) )