You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Telethon在Telegram群组中创建话题并获取话题ID

问题:使用Telethon创建Telegram群组话题并获取话题ID

我正在尝试用Telethon开发用户机器人,目标是创建Telegram群组并在其中创建话题。目前已经能成功创建群组、邀请所需用户,但找不到操作话题的正确方法——只知道如何向指定话题发送消息,不清楚如何创建话题以及获取话题ID。

以下是已实现的预启动代码:

@loader.tds
class testr(loader.Module):
    '''name'''
    strings = {
        "name": "test",   
    }
    async def client_ready(self, client, db):
        self.test_channel, _ = await utils.asset_channel(
            self._client,
            "Test - chat",
            "check descr",
            silent=True,
            archive=True,
            _folder="m",
            
        )   
        await self.client(functions.channels.InviteToChannelRequest(self.test_channel, ['@my_bot']))
           
        await self.client(functions.channels.EditAdminRequest(
                channel=self.test_channel,
                user_id="@my_bot",
                admin_rights=ChatAdminRights(ban_users=True, post_messages=True, edit_messages=True),
                rank="xTest",
            )
        )

    def __init__(self):
        pass  # 补充原代码缺失的初始化逻辑

解决方案

1. 创建话题

Telegram的话题(Forum Topic)仅支持超级群组(Supergroup),utils.asset_channel默认创建的就是超级群组,可直接使用。创建话题需调用channels.CreateForumTopicRequest函数,核心参数如下:

  • channel: 目标超级群组的实体或ID
  • title: 话题名称
  • icon_color(可选): 话题图标颜色,取值为预定义色值(如0x6FB9F0、0xFFD67E等)
  • icon_emoji_id(可选): 话题图标对应的emoji ID

2. 获取话题ID

调用CreateForumTopicRequest成功后,会返回Updates对象,可从updates[-1].message.action.topic.id中提取话题ID,后续向话题发消息时需用到该ID。

整合到现有代码的示例

修改client_ready方法,添加创建话题和提取ID的逻辑:

from telethon.tl.functions.channels import CreateForumTopicRequest
from telethon.tl.types import ChatAdminRights

@loader.tds
class testr(loader.Module):
    '''name'''
    strings = {
        "name": "test",   
    }
    async def client_ready(self, client, db):
        self.test_channel, _ = await utils.asset_channel(
            self._client,
            "Test - chat",
            "check descr",
            silent=True,
            archive=True,
            _folder="m",
            
        )   
        await self.client(functions.channels.InviteToChannelRequest(self.test_channel, ['@my_bot']))
           
        await self.client(functions.channels.EditAdminRequest(
                channel=self.test_channel,
                user_id="@my_bot",
                admin_rights=ChatAdminRights(ban_users=True, post_messages=True, edit_messages=True),
                rank="xTest",
            )
        )

        # 创建话题
        topic_updates = await self.client(CreateForumTopicRequest(
            channel=self.test_channel,
            title="测试话题",
            icon_color=0x6FB9F0  # 设置蓝色图标
        ))
        
        # 提取并保存话题ID
        self.topic_id = topic_updates[-1].message.action.topic.id
        print(f"已创建话题ID: {self.topic_id}")

    def __init__(self):
        pass

向指定话题发送消息

如需向该话题发消息,发送时指定reply_to参数为话题ID即可:

await self.client.send_message(
    self.test_channel,
    "这是话题内的测试消息",
    reply_to=self.topic_id
)

内容的提问来源于stack exchange,提问作者xaecep

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 01:51:22