Skip to content

sendDocument

发送文档文件到指定聊天。支持直接上传文件或传入已有文件的 file_id。适用于发送各类文件,最大支持 100MB。

请求

POST /:token/sendDocument

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

参数

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

响应

返回发送成功的 Message 对象。

json
{
  "ok": true,
  "result": {
    "message_id": 105,
    "from": {
      "id": 123456789,
      "is_bot": true,
      "first_name": "MyBot",
      "username": "my_bot"
    },
    "chat": {
      "id": 987654321,
      "first_name": "User",
      "username": "user123",
      "type": "private"
    },
    "date": 1700000000,
    "document": {
      "file_id": "BQACAgIAAxkBAAI...",
      "file_unique_id": "AQADAgAT...",
      "file_name": "report.pdf",
      "mime_type": "application/pdf",
      "file_size": 524288
    },
    "caption": "这是一份报告文档"
  }
}

错误码

错误码描述
400请求参数错误,如缺少 document 或 chat_id
401Token 无效或已过期
403Bot 无权向该聊天发送消息
404聊天不存在
413文件体积过大(超过 100MB)
500服务器内部错误

示例

通过文件上传发送文档

bash
curl -X POST "https://api.example.com/<token>/sendDocument" \
  -F "chat_id=987654321" \
  -F "document=@/path/to/report.pdf" \
  -F "caption=这是一份报告文档"

通过 file_id 发送文档

bash
curl -X POST "https://api.example.com/<token>/sendDocument" \
  -H "Content-Type: application/json" \
  -d '{
    "chat_id": 987654321,
    "document": "BQACAgIAAxkBAAI...",
    "caption": "转发一份文档"
  }'