|
1 | 1 | """Chats MCP tools.""" |
2 | 2 |
|
| 3 | +import struct |
| 4 | + |
| 5 | +from telethon.tl.tlobject import TLObject, TLRequest |
| 6 | + |
3 | 7 | from telegram_mcp.runtime import * |
4 | 8 |
|
5 | 9 |
|
| 10 | +class GetForumTopicsRequest(TLRequest): |
| 11 | + """Raw request for channels.getForumTopics missing in Telethon 1.42-1.43.""" |
| 12 | + |
| 13 | + CONSTRUCTOR_ID = 0x0DE560D1 |
| 14 | + SUBCLASS_OF_ID = 0x0 |
| 15 | + |
| 16 | + def __init__(self, channel, offset_date, offset_id, offset_topic, limit, q=None): |
| 17 | + self.channel = channel |
| 18 | + self.q = q |
| 19 | + self.offset_date = offset_date |
| 20 | + self.offset_id = offset_id |
| 21 | + self.offset_topic = offset_topic |
| 22 | + self.limit = limit |
| 23 | + |
| 24 | + async def resolve(self, client, utils): |
| 25 | + self.channel = utils.get_input_channel(await client.get_input_entity(self.channel)) |
| 26 | + |
| 27 | + def to_dict(self): |
| 28 | + return { |
| 29 | + "_": "GetForumTopicsRequest", |
| 30 | + "channel": ( |
| 31 | + self.channel.to_dict() if isinstance(self.channel, TLObject) else self.channel |
| 32 | + ), |
| 33 | + "q": self.q, |
| 34 | + "offset_date": self.offset_date, |
| 35 | + "offset_id": self.offset_id, |
| 36 | + "offset_topic": self.offset_topic, |
| 37 | + "limit": self.limit, |
| 38 | + } |
| 39 | + |
| 40 | + def _bytes(self): |
| 41 | + flags = 0 if self.q is None or self.q is False else 1 |
| 42 | + return b"".join( |
| 43 | + ( |
| 44 | + struct.pack("<I", self.CONSTRUCTOR_ID), |
| 45 | + struct.pack("<I", flags), |
| 46 | + self.channel._bytes(), |
| 47 | + b"" if self.q is None or self.q is False else self.serialize_bytes(self.q), |
| 48 | + struct.pack("<i", self.offset_date), |
| 49 | + struct.pack("<i", self.offset_id), |
| 50 | + struct.pack("<i", self.offset_topic), |
| 51 | + struct.pack("<i", self.limit), |
| 52 | + ) |
| 53 | + ) |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def from_reader(cls, reader): |
| 57 | + flags = reader.read_int() |
| 58 | + channel = reader.tgread_object() |
| 59 | + q = reader.tgread_string() if flags & 1 else None |
| 60 | + offset_date = reader.read_int() |
| 61 | + offset_id = reader.read_int() |
| 62 | + offset_topic = reader.read_int() |
| 63 | + limit = reader.read_int() |
| 64 | + return cls( |
| 65 | + channel=channel, |
| 66 | + offset_date=offset_date, |
| 67 | + offset_id=offset_id, |
| 68 | + offset_topic=offset_topic, |
| 69 | + limit=limit, |
| 70 | + q=q, |
| 71 | + ) |
| 72 | + |
| 73 | + |
6 | 74 | @mcp.tool(annotations=ToolAnnotations(title="Get Chats", openWorldHint=True, readOnlyHint=True)) |
7 | 75 | @with_account(readonly=True) |
8 | 76 | async def get_chats(account: str = None, page: int = 1, page_size: int = 20) -> str: |
@@ -107,7 +175,7 @@ async def list_topics( |
107 | 175 | return "The specified supergroup does not have forum topics enabled." |
108 | 176 |
|
109 | 177 | result = await cl( |
110 | | - functions.channels.GetForumTopicsRequest( |
| 178 | + GetForumTopicsRequest( |
111 | 179 | channel=entity, |
112 | 180 | offset_date=0, |
113 | 181 | offset_id=0, |
|
0 commit comments