diff --git a/README.md b/README.md index 3aa2393..d64d18c 100644 --- a/README.md +++ b/README.md @@ -712,3 +712,9 @@ This project is licensed under the [Apache 2.0 License](LICENSE). ## Star History [![Star History Chart](https://api.star-history.com/svg?repos=chigwell/telegram-mcp&type=Date)](https://www.star-history.com/#chigwell/telegram-mcp&Date) + +## Contributors + + + + diff --git a/main.py b/main.py index 5046e86..bc36cae 100644 --- a/main.py +++ b/main.py @@ -1271,13 +1271,17 @@ async def list_topics( @mcp.tool(annotations=ToolAnnotations(title="List Chats", openWorldHint=True, readOnlyHint=True)) -async def list_chats(chat_type: str = None, limit: int = 20) -> str: +async def list_chats( + chat_type: str = None, limit: int = 20, unread_only: bool = False, unmuted_only: bool = False +) -> str: """ List available chats with metadata. Args: chat_type: Filter by chat type ('user', 'group', 'channel', or None for all) - limit: Maximum number of chats to retrieve. + limit: Maximum number of chats to retrieve from Telegram API (applied before filtering, so fewer results may be returned when filters are active). + unread_only: If True, only return chats with unread messages. + unmuted_only: If True, only return unmuted chats. """ try: dialogs = await client.get_dialogs(limit=limit) @@ -1316,6 +1320,24 @@ async def list_chats(chat_type: str = None, limit: int = 20) -> str: bool(getattr(inner_dialog, "unread_mark", False)) if inner_dialog else False ) + # Extract mute status from notify_settings + notify_settings = getattr(inner_dialog, "notify_settings", None) + mute_until = getattr(notify_settings, "mute_until", None) + if mute_until is None: + is_muted = False + elif isinstance(mute_until, datetime): + is_muted = mute_until.timestamp() > time.time() + else: + is_muted = mute_until > time.time() + + # Filter by mute status if requested + if unmuted_only and is_muted: + continue + + # Filter by unread status if requested + if unread_only and unread_count == 0 and not unread_mark: + continue + if unread_count > 0: chat_info += f", Unread: {unread_count}" elif unread_mark: @@ -1323,6 +1345,8 @@ async def list_chats(chat_type: str = None, limit: int = 20) -> str: else: chat_info += ", No unread messages" + chat_info += f", Muted: {'yes' if is_muted else 'no'}" + # Add unread mentions count if available unread_mentions = getattr(dialog, "unread_mentions_count", 0) or 0 if unread_mentions > 0: @@ -1335,7 +1359,14 @@ async def list_chats(chat_type: str = None, limit: int = 20) -> str: return "\n".join(results) except Exception as e: - return log_and_format_error("list_chats", e, chat_type=chat_type, limit=limit) + return log_and_format_error( + "list_chats", + e, + chat_type=chat_type, + limit=limit, + unread_only=unread_only, + unmuted_only=unmuted_only, + ) @mcp.tool(annotations=ToolAnnotations(title="Get Chat", openWorldHint=True, readOnlyHint=True))