fix: use correct Telegram API for archive/unarchive chat

archive_chat and unarchive_chat were using ToggleDialogPinRequest which
pins/unpins dialogs instead of archiving them. Replace with
folders.EditPeerFoldersRequest using folder_id=1 (Archive) and
folder_id=0 (Main) which is the correct API for moving chats to/from
the Archive folder.
This commit is contained in:
gvidonind-commits 2026-02-09 06:36:27 +03:00
parent 36522e5ebc
commit 382246d89e

14
main.py
View file

@ -16,7 +16,7 @@ from dotenv import load_dotenv
from mcp.server.fastmcp import FastMCP from mcp.server.fastmcp import FastMCP
from mcp.types import ToolAnnotations from mcp.types import ToolAnnotations
from pythonjsonlogger import jsonlogger from pythonjsonlogger import jsonlogger
from telethon import TelegramClient, functions, utils from telethon import TelegramClient, functions, types, utils
from telethon.sessions import StringSession from telethon.sessions import StringSession
from telethon.tl.types import ( from telethon.tl.types import (
User, User,
@ -2870,9 +2870,11 @@ async def archive_chat(chat_id: Union[int, str]) -> str:
Archive a chat. Archive a chat.
""" """
try: try:
entity = await client.get_entity(chat_id)
peer = utils.get_input_peer(entity)
await client( await client(
functions.messages.ToggleDialogPinRequest( functions.folders.EditPeerFoldersRequest(
peer=await client.get_entity(chat_id), pinned=True folder_peers=[types.InputFolderPeer(peer=peer, folder_id=1)]
) )
) )
return f"Chat {chat_id} archived." return f"Chat {chat_id} archived."
@ -2891,9 +2893,11 @@ async def unarchive_chat(chat_id: Union[int, str]) -> str:
Unarchive a chat. Unarchive a chat.
""" """
try: try:
entity = await client.get_entity(chat_id)
peer = utils.get_input_peer(entity)
await client( await client(
functions.messages.ToggleDialogPinRequest( functions.folders.EditPeerFoldersRequest(
peer=await client.get_entity(chat_id), pinned=False folder_peers=[types.InputFolderPeer(peer=peer, folder_id=0)]
) )
) )
return f"Chat {chat_id} unarchived." return f"Chat {chat_id} unarchived."