
本文详解如何通过 telethon 库创建支持话题(topics)的 telegram 论坛群组,包括新建论坛、创建具体话题、获取话题 id 以及发送消息到指定话题的完整流程。
本文详解如何通过 telethon 库创建支持话题(topics)的 telegram 论坛群组,包括新建论坛、创建具体话题、获取话题 id 以及发送消息到指定话题的完整流程。
Telegram 自 2022 年起引入「论坛模式」(Forum Mode),允许将群组或频道升级为支持多话题讨论的结构化社区。Telethon 作为功能完备的 Python Telegram SDK,提供了底层 TL 方法支持该特性,但需注意:官方 high-level API 尚未封装话题相关操作,必须调用原始 RPC 方法(如 CreateChannelRequest 和 CreateForumTopicRequest)。
✅ 创建论坛群组
首先,使用 CreateChannelRequest 创建一个启用论坛模式的群组(注意 forum=True 参数):
from telethon import TelegramClient
from telethon.tl.functions.channels import CreateChannelRequest
import asyncio
async def create_forum():
async with TelegramClient('session_name', api_id, api_hash) as client:
# 创建论坛群组(类型为 'supergroup',forum=True 启用话题)
result = await client(CreateChannelRequest(
title='Python 开发者论坛',
about='专注 Python、Telethon 与 Telegram Bot 开发的交流社区',
broadcast=False, # False → 群组;True → 频道
megagroup=True, # 必须为 True 才能启用话题(仅 supergroup 支持)
forum=True # 关键:启用论坛模式
))
# 提取群组 ID(注意:返回值结构较复杂,推荐用 result.chats[0].id)
forum_id = result.chats[0].id
print(f"✅ 论坛已创建,ID: {forum_id}")
return forum_id
⚠️ 注意:forum=True 仅对 megagroup=True(即超级群组)有效;普通群组或频道无法直接启用话题。
✅ 创建新话题并获取 topic_id
创建话题需调用 CreateForumTopicRequest,传入论坛群组 ID 和标题。返回结果中包含话题对象及唯一 topic_id(即 PeerChannel 的 channel_id + topic_id 组合,实际用于消息发送):
from telethon.tl.functions.channels import CreateForumTopicRequest
from telethon.tl.types import PeerChannel
async def create_topic(client, forum_id, topic_title):
result = await client(CreateForumTopicRequest(
channel=PeerChannel(forum_id), # 必须包装为 PeerChannel
title=topic_title,
icon_emoji="?" # 可选:设置话题图标(Telegram 5.10+ 支持)
))
# topic_id 是消息发送的目标实体(本质是 int 类型的 topic ID)
topic_id = result.updates[0].topic.id if hasattr(result.updates[0], 'topic') else result.updates[0].id
print(f"✅ 话题 '{topic_title}' 已创建,Topic ID: {topic_id}")
return topic_id
? 获取 topic_id 的说明:CreateForumTopicRequest 返回的 updates 列表中,首个更新通常为 UpdateForumTopicCreated,其 .topic.id 即为话题 ID。部分版本中可直接取 result.updates[0].id —— 建议打印 result 调试确认结构。
✅ 向指定话题发送消息
发送消息时,不能直接用 forum_id 或 topic_id 字符串,而应构造 InputPeerChannel 并指定 top_msg_id(即话题首条消息 ID,通常为 1):
from telethon.tl.types import InputPeerChannel
async def send_to_topic(client, forum_id, topic_id, message):
# 构造目标 peer:forum_id + topic_id 组合
peer = InputPeerChannel(
channel_id=forum_id,
access_hash=0 # 注意:access_hash 需提前获取(见下方说明)
)
# 更可靠的方式:使用 client.get_input_entity() 获取完整 InputPeer
try:
channel = await client.get_input_entity(forum_id)
await client.send_message(
entity=channel,
message=message,
reply_to=topic_id # 关键:reply_to=topic_id 表示发到该话题
)
print(f"? 消息已发送至话题 ID {topic_id}")
except Exception as e:
print(f"❌ 发送失败: {e}")
? 实用技巧:reply_to=topic_id 是当前最稳定的方式——Telethon 会自动识别该 ID 为话题并路由消息。无需手动构造复杂 peer。
? 补充:将已有群组升级为论坛
若已有活跃群组,可通过 ToggleForumRequest 启用话题功能(需管理员权限):
from telethon.tl.functions.channels import ToggleForumRequest
async def enable_forum_for_existing_group(client, group_id):
await client(ToggleForumRequest(
channel=await client.get_input_entity(group_id),
enabled=True
))
print(f"? 群组 {group_id} 已启用论坛模式")
✅ 完整运行示例(整合版)
import asyncio
from telethon import TelegramClient
from telethon.tl.functions.channels import CreateChannelRequest, CreateForumTopicRequest
from telethon.tl.types import PeerChannel
api_id = 12345678
api_hash = 'your_api_hash'
phone = '+1234567890'
async def main():
async with TelegramClient('forum_session', api_id, api_hash) as client:
# 步骤 1:创建论坛群组
result = await client(CreateChannelRequest(
title='Demo Forum',
about='Test forum for topics',
broadcast=False,
megagroup=True,
forum=True
))
forum_id = result.chats[0].id
# 步骤 2:创建话题
topic_result = await client(CreateForumTopicRequest(
channel=PeerChannel(forum_id),
title='欢迎与提问区',
icon_emoji='?'
))
topic_id = topic_result.updates[0].topic.id
# 步骤 3:向话题发送首条消息
await client.send_message(
entity=forum_id,
message='Hello! 这是话题内的第一条消息 ?',
reply_to=topic_id
)
print(f"✅ 全流程完成!论坛 ID: {forum_id}, 话题 ID: {topic_id}")
if __name__ == '__main__':
asyncio.run(main())
? 注意事项总结
- ✅ 权限要求:创建论坛/话题需账号具备群组管理员权限(创建者自动获得);邀请用户、设管理员等操作需额外调用对应方法(如 InviteToChannelRequest)。
- ✅ 依赖版本:确保使用 Telethon ≥ 1.30.0(旧版本可能缺少 CreateForumTopicRequest 支持)。
- ✅ access_hash 问题:直接构造 InputPeerChannel 时 access_hash 不可随意设为 0;推荐统一使用 client.get_input_entity() 获取合法 peer。
- ❗ 话题不可删除:Telegram API 当前不支持删除话题(仅可关闭/隐藏),设计时请考虑此限制。
- ? 客户端兼容性:话题功能需客户端支持(Telegram Android/iOS ≥ 8.9,Desktop ≥ 4.4)。
掌握以上方法,即可在自动化脚本、Bot 或用户端应用中灵活构建结构化 Telegram 社区。











