Skip to content

sendPhoto

发送图片到指定聊天。支持直接上传文件或传入已有文件的 file_id

请求

POST /:token/sendPhoto

文件上传时使用 multipart/form-data 编码。

参数

参数类型必填描述
chat_idInteger/String目标聊天的唯一标识符或用户名
photoString/File图片文件或已上传文件的 file_id,最大 10MB
captionString图片说明文字
parse_modeString说明文字的解析模式,支持 HTMLMarkdown
reply_markupObject自定义键盘或内联键盘标记,JSON 序列化对象

响应

返回发送成功的 Message 对象。

json
{
  "ok": true,
  "result": {
    "message_id": 101,
    "from": {
      "id": 123456789,
      "is_bot": true,
      "first_name": "MyBot",
      "username": "my_bot"
    },
    "chat": {
      "id": 987654321,
      "first_name": "User",
      "username": "user123",
      "type": "private"
    },
    "date": 1700000000,
    "photo": [
      {
        "file_id": "AgACAgIAAxkBAAI...",
        "file_unique_id": "AQADAgAT...",
        "file_size": 12345,
        "width": 320,
        "height": 240
      },
      {
        "file_id": "AgACAgIAAxkBAAI...",
        "file_unique_id": "AQADAgAT...",
        "file_size": 56789,
        "width": 800,
        "height": 600
      }
    ],
    "caption": "这是一张示例图片"
  }
}

错误码

错误码描述
400请求参数错误,如文件格式不支持或超出大小限制(最大 10MB)
401Token 无效或已过期
403Bot 无权向该聊天发送消息
404聊天不存在
413文件体积过大
500服务器内部错误

示例

通过文件上传发送图片

bash
curl -X POST "https://api.example.com/<token>/sendPhoto" \
  -F "chat_id=987654321" \
  -F "photo=@/path/to/image.jpg" \
  -F "caption=这是一张示例图片"

通过 file_id 发送图片

bash
curl -X POST "https://api.example.com/<token>/sendPhoto" \
  -H "Content-Type: application/json" \
  -d '{
    "chat_id": 987654321,
    "photo": "AgACAgIAAxkBAAI...",
    "caption": "转发一张图片"
  }'