Telegram机器人创建与Chat ID获取指南
一、创建Telegram机器人获取Token
- 打开Telegram应用搜索
@BotFather
- 点击"Start"开始对话
- 选择菜单→
/newbot
或直接发送该指令 - 按指引完成创建后,将收到包含以下关键信息的消息:
Done! Congratulations on your new bot. You will find it at t.me/new_bot.
You can now add a description.....
Use this token to access the HTTP API:
63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c
Keep your token secure and store it safely, it can be used by anyone to control your bot.
For a description of the Bot API, see this page: https://core.telegram.org/bots/api
- 妥善保存你的机器人Token:
63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c
二、获取私聊Chat ID
- 搜索并打开你创建的机器人
- 发送任意消息激活对话
- 浏览器访问(注意Token前需加
bot
前缀):
https://api.telegram.org/bot63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c/getUpdates
- 在返回的JSON数据中定位:
{
"ok": true,
"result": [
{
"update_id": 83xxxxx35,
"message": {
"message_id": 2643,
"from": {...},
"chat": {
"id": 21xxxxx38,
"first_name": "...",
"last_name": "...",
"username": "@username",
"type": "private"
},
"date": 1703062972,
"text": "/start"
}
}
]
}
- 记录result.0.message.chat.id, Chat ID:
21xxxxx38
- 测试消息发送:
三、获取频道Chat ID
- 将机器人添加为频道管理员
- 在频道发布任意消息
- 访问相同API接口获取更新 https://api.telegram.org/bot{our_bot_token}/getUpdates
- 在返回数据中查找:
{
"ok": true,
"result": [
{
"update_id": 838xxxx36,
"channel_post": {...},
"chat": {
"id": -1001xxxxxx062,
"title": "....",
"type": "channel"
},
"date": 1703065989,
"text": "test"
}
}
]
}
- 频道result.0.channel_post.chat.id, Chat ID示例:
-1001xxxxxx062
(注意包含-100
前缀) - 测试频道消息发送 https://api.telegram.org/bot63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c/sendMessage?chat_id=-1001xxxxxx062&text=test123
- 当我们正确设置了机器人令牌(bot token)和聊天 ID(chat ID)后,消息 “test123” 应该会发送到我们的 Telegram 频道中。****
四、获取群组Chat ID(推荐桌面端操作)
- 通过Telegram桌面端添加机器人到群组
- 在群组发送任意消息
- 右键消息选择"Copy Message Link",获得格式:
https://t.me/c/194xxxx987/11/13
- 群组原始ID:
194xxxx987
- API使用需添加
-100
前缀:-100194xxxx987
- 测试群组消息发送 https://api.telegram.org/bot63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c/sendMessage?chat_id=-100194xxxx987&text=test123
- 当我们正确设置了机器人令牌(bot token)和聊天 ID(chat ID)后,消息 “test123” 应该会发送到我们的 Telegram 频道中。****
五、获取群组话题ID
- 同上复制消息链接后,解析格式:
https://t.me/c/194xxxx987/11/13
- 示例中话题ID:
11
- 发送到指定话题需添加参数
&message_thread_id=11
: https://api.telegram.org/bot783114779:AAEuRWDTFD2UQ7agBtFSuhJf2-NmvHN3OPc/sendMessage?chat_id=-100194xxxx987&message_thread_id=11&text=test123 - 当我们正确设置了机器人令牌(bot token)和聊天 ID(chat ID)后,消息 “test123” 应该会发送到我们的 Telegram 频道中。
更新于: 23/06/2025
谢谢!