如何获取Telegram频道内特定话题的Chat ID?
获取Telegram频道指定话题的Chat ID并发送消息
一、手动获取话题ID
- 打开Telegram客户端进入目标频道的指定话题
- 点击话题顶部标题,复制链接(格式如
https://t.me/your_channel/123),链接末尾的数字就是该话题的ID - 桌面端右键话题标题选「复制链接」,移动端长按标题选「复制链接」即可
二、程序化获取话题ID
如果需要通过代码批量获取频道内的话题ID,可以调用Telegram Bot API的getForumTopics接口:
curl -X POST https://api.telegram.org/bot<你的Bot令牌>/getForumTopics -d chat_id=<频道Chat ID>
返回的JSON数据中,每个topic对象里的message_thread_id字段就是对应话题的ID。
三、发送消息到指定话题
在调用sendMessage接口时,除了传入频道的chat_id,额外添加message_thread_id参数并填入目标话题ID即可。以Python为例:
import requests bot_token = "你的Bot令牌" channel_chat_id = "-100XXXXXXXXX" # 你的频道Chat ID target_topic_id = 123 # 获取到的指定话题ID api_url = f"https://api.telegram.org/bot{bot_token}/sendMessage" payload = { "chat_id": channel_chat_id, "message_thread_id": target_topic_id, "text": "发送到指定话题的测试消息" } response = requests.post(api_url, data=payload) print(response.json())
内容的提问来源于stack exchange,提问作者Stan Daily
相关产品推荐
相关产品推荐

