一、创建Telegram机器人获取Token

  1. 打开Telegram应用搜索@BotFather
  2. 点击"Start"开始对话
  3. 选择菜单→/newbot或直接发送该指令
  4. 按指引完成创建后,将收到包含以下关键信息的消息:
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
  1. 妥善保存你的机器人Token:63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c

二、获取私聊Chat ID

  1. 搜索并打开你创建的机器人
  2. 发送任意消息激活对话
  3. 浏览器访问(注意Token前需加bot前缀):

https://api.telegram.org/bot63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c/getUpdates
4. 在返回的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"
      }
    }
  ]
}
  1. 记录result.0.message.chat.id, Chat ID:21xxxxx38
  2. 测试消息发送:

https://api.telegram.org/bot63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c/sendMessage?chat_id=21xxxxx38&text=test123``

三、获取频道Chat ID

  1. 将机器人添加为频道管理员
  2. 在频道发布任意消息
  3. 访问相同API接口获取更新 https://api.telegram.org/bot{our_bot_token}/getUpdates
  4. 在返回数据中查找:
{
  "ok": true,
  "result": [
    {
      "update_id": 838xxxx36,
      "channel_post": {...},
        "chat": {
          "id": -1001xxxxxx062,
          "title": "....",
          "type": "channel"
        },
        "date": 1703065989,
        "text": "test"
      }
    }
  ]
}
  1. 频道result.0.channel_post.chat.id, Chat ID示例:-1001xxxxxx062(注意包含 -100前缀)
  2. 测试频道消息发送 https://api.telegram.org/bot63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c/sendMessage?chat_id=-1001xxxxxx062&text=test123
  3. 当我们正确设置了机器人令牌(bot token)和聊天 ID(chat ID)后,消息 “test123” 应该会发送到我们的 Telegram 频道中。****

四、获取群组Chat ID(推荐桌面端操作)

  1. 通过Telegram桌面端添加机器人到群组
  2. 在群组发送任意消息
  3. 右键消息选择"Copy Message Link",获得格式:

https://t.me/c/194xxxx987/11/13
4. 群组原始ID:194xxxx987
5. API使用需添加-100前缀:-100194xxxx987
6. 测试群组消息发送 https://api.telegram.org/bot63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c/sendMessage?chat_id=-100194xxxx987&text=test123
7. 当我们正确设置了机器人令牌(bot token)和聊天 ID(chat ID)后,消息 “test123” 应该会发送到我们的 Telegram 频道中。****

五、获取群组话题ID

  1. 同上复制消息链接后,解析格式:

https://t.me/c/194xxxx987/11/13
2. 示例中话题ID:11
3. 发送到指定话题需添加参数&message_thread_id=11: https://api.telegram.org/bot783114779:AAEuRWDTFD2UQ7agBtFSuhJf2-NmvHN3OPc/sendMessage?chat_id=-100194xxxx987&message_thread_id=11&text=test123
4. 当我们正确设置了机器人令牌(bot token)和聊天 ID(chat ID)后,消息 “test123” 应该会发送到我们的 Telegram 频道中。