Skip to content

快速开始

创建 Bot

  1. 在 SafeW 应用中找到 BotFather
  2. 发送 /newbot 命令
  3. 按照提示设置 Bot 名称和用户名
  4. 获取 Bot Token

获取 Token

创建成功后,BotFather 会返回一个 Token。请妥善保管该 Token,它是调用 API 的唯一凭证。

安全提示

请勿将 Token 公开或提交到版本控制系统中。如果 Token 泄露,请立即通过 BotFather 重新生成。

发送第一条消息

使用 sendMessage 方法发送一条文本消息:

bash
curl -X POST "https://api.example.com/bot{YOUR_TOKEN}/sendmessage" \
  -H "Content-Type: application/json" \
  -d '{
    "chat_id": 123456789,
    "text": "Hello, World!"
  }'

成功响应:

json
{
  "ok": true,
  "result": {
    "message_id": 1,
    "chat": {
      "id": 123456789,
      "type": "private"
    },
    "text": "Hello, World!"
  }
}

接收消息

有两种方式接收消息更新:

长轮询(getUpdates)

bash
curl "https://api.example.com/bot{YOUR_TOKEN}/getupdates"

Webhook

设置 Webhook URL 后,服务器会主动将更新推送到你指定的 URL:

bash
curl -X POST "https://api.example.com/bot{YOUR_TOKEN}/setwebhook" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://your-server.com/webhook"
  }'

下一步