如何使用 Telethon 在 Telegram 中创建频道论坛和话题

作者:袖梨 2026-06-24

本文详解如何通过 telethon 库创建支持话题(topics)的 telegram 论坛频道,并动态新建话题、获取话题 id,适用于用户机器人开发场景。

本文详解如何通过 telethon 库创建支持话题(topics)的 telegram 论坛频道,并动态新建话题、获取话题 id,适用于用户机器人开发场景。

Telegram 自 2022 年起引入「论坛模式(Forum)」,允许在群组或频道中启用结构化话题功能。但需注意:只有频道(Channel)可原生启用论坛模式;普通群组(Group)需先升级为超级群组(Supergroup),再通过 ToggleForumRequest 启用论坛,或直接创建带 forum=True 的新频道。

✅ 创建支持话题的论坛频道

使用 CreateChannelRequest 并设置 forum=True 即可一步创建论坛频道:

from telethon import TelegramClientfrom telethon.tl.functions.channels import CreateChannelRequest, CreateForumTopicRequestfrom telethon.tl.types import ChatAdminRightsasync def create_forum_and_topic():    # 替换为你的 API 配置    client = TelegramClient('session_name', api_id=12345, api_hash='your_api_hash')    await client.start()    # 创建论坛频道    forum = await client(CreateChannelRequest(        title='我的技术论坛',        about='Python & Telethon 开发讨论区',        forum=True,  # 关键:启用论坛模式        megagroup=False  # 注意:forum=True 时必须为 False(即创建频道,非群组)    ))    # 提取频道 ID(返回结果中 channel_id 位于 updates[1].channel_id)    forum_id = forum.updates[1].channel_id    print(f"✅ 论坛频道已创建,ID: {forum_id}")    # 创建首个话题    topic = await client(CreateForumTopicRequest(        channel=forum_id,        title='欢迎与入门指南',        icon_emoji='?'  # 可选:指定话题图标(Emoji)    ))    # 获取话题 ID(注意:topic.updates[0].id 是 Topic ID,类型为 int)    topic_id = topic.updates[0].id    print(f"✅ 话题已创建,ID: {topic_id}")    # 向该话题发送首条消息(需指定 entity=topic_id,并 reply_to topic_id)    await client.send_message(        entity=topic_id,        message='欢迎加入!请阅读本话题中的使用说明 ?',        reply_to=topic_id  # 确保消息作为话题根帖发出    )    print("✅ 消息已发布至话题")

⚠️ 关键细节说明

  • CreateChannelRequest(forum=True) 仅适用于频道(Channel),不适用于群组(Group);
  • 若需将已有群组转为论坛,请先确保其为超级群组(Supergroup),再调用 ToggleForumRequest;
  • CreateForumTopicRequest 返回的 updates[0].id 是 Topic ID(整数),可直接用于 send_message(entity=topic_id);
  • 发送话题内消息时,entity 必须传入 topic_id(而非频道 ID),且 reply_to 建议设为同一 topic_id 以确保作为话题首帖。

? 将现有群组升级为论坛(可选)

若已有活跃群组,可通过以下方式启用论坛模式:

from telethon.tl.functions.channels import ToggleForumRequest# YOUR_GROUP 可为群组 ID、用户名或 InputChannel 实例await client(ToggleForumRequest(    channel=YOUR_GROUP,    enabled=True))

启用后,即可调用 CreateForumTopicRequest 创建话题(用法同上)。

? 补充:获取话题列表与 ID 映射

如需后续管理话题,可通过 GetForumTopicsRequest 查询:

from telethon.tl.functions.channels import GetForumTopicsRequesttopics = await client(GetForumTopicsRequest(    channel=forum_id,    offset_date=0,    offset_id=0,    offset_topic=0,    limit=100))for t in topics.topics:    print(f"Topic ID: {t.id}, Title: {t.title}, Creator: {t.creator}")

✅ 总结

  • ✅ 使用 CreateChannelRequest(forum=True) 创建原生论坛频道;
  • ✅ 使用 CreateForumTopicRequest 创建话题,返回 topic_id 直接用于消息发送;
  • ✅ 话题 ID ≠ 频道 ID,务必区分 entity 参数;
  • ✅ 已有超级群组可通过 ToggleForumRequest 启用论坛;
  • ✅ 所有操作需用户账号具有相应权限(如创建频道、管理话题等)。

掌握这些方法后,你即可在自动化脚本或用户机器人中完整实现 Telegram 论坛的初始化与话题生命周期管理。

相关文章

精彩推荐