sendDocument
发送文件。
返回值: 调用成功时返回 Message。
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
business_connection_id | String | 可选 | 商业连接 ID |
chat_id | Integer or String | 是 | 聊天 ID 或 @username |
message_thread_id | Integer | 可选 | 论坛话题 ID |
direct_messages_topic_id | Integer | 可选 | 私信话题 ID (Bot API 10.0) |
ephemeral_message_parameters | EphemeralMessageParameters | 可选 | EphemeralMessageParameters JSON 对象——仅对单个用户可见的临时消息 (Bot API 10.3) |
document | InputFile or String | 是 | 文件 URL 或 file_id |
thumbnail | InputFile or String | 可选 | 缩略图 URL 或 file_id |
caption | String | 可选 | 说明文字(0-1024) |
parse_mode | String | 可选 | HTML、Markdown 或 MarkdownV2 |
caption_entities | Array of MessageEntity | 可选 | MessageEntity JSON 数组 |
disable_content_type_detection | Boolean | 可选 | 禁用内容类型检测 |
disable_notification | Boolean | 可选 | 静默发送 |
protect_content | Boolean | 可选 | 保护内容 |
allow_paid_broadcast | Boolean | 可选 | 允许付费广播 |
message_effect_id | String | 可选 | 消息特效 ID |
suggested_post_parameters | SuggestedPostParameters | 可选 | SuggestedPostParameters JSON 对象 |
reply_parameters | ReplyParameters | 可选 | ReplyParameters JSON 对象 |
reply_markup | InlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardRemove or ForceReply | 可选 | JSON 对象:InlineKeyboardMarkup / ReplyKeyboardMarkup / ReplyKeyboardRemove / ForceReply |
请求示例
curl
curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendDocument" \
-H "Content-Type: application/json" \
-d '{"chat_id":123456789,"document":"https://example.com/file.pdf"}'
Python
import requests
TOKEN = "123456789:AAEexampleTokenReplaceWithYours"
resp = requests.post(
f"https://api.telegram.org/bot{TOKEN}/sendDocument",
json={
"chat_id": 123456789,
"document": "https://example.com/file.pdf"
},
timeout=30,
)
print(resp.json())
JavaScript
const TOKEN = "123456789:AAEexampleTokenReplaceWithYours";
const res = await fetch(`https://api.telegram.org/bot${TOKEN}/sendDocument`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
"chat_id": 123456789,
"document": "https://example.com/file.pdf"
}),
});
console.log(await res.json());
分步教程: Telegram 机器人教程:从零创建 Telegram 机器人并发送第一条消息
相关方法
sendPhoto发送照片。sendVideo发送视频。sendAudio发送音频。sendAnimation发送动画(GIF)。sendSticker发送贴纸。sendVoice发送语音消息。sendVideoNote发送视频消息(圆形视频)。sendMediaGroup发送一组媒体(相册)。sendPaidMedia发送付费媒体 (Bot API 8.0+)。sendDice发送骰子。sendLivePhoto发送实况照片(照片 + 视频,Bot API 10.0)。