sendMessage
发送文本消息。
返回值: 调用成功时返回 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) |
text | String | 是 | 消息文本(1-4096 个字符) |
parse_mode | String | 可选 | HTML、Markdown 或 MarkdownV2 |
entities | Array of MessageEntity | 可选 | MessageEntity JSON 数组 |
link_preview_options | LinkPreviewOptions | 可选 | LinkPreviewOptions JSON 对象 |
disable_notification | Boolean | 可选 | 静默发送 |
protect_content | Boolean | 可选 | 保护内容 |
allow_paid_broadcast | Boolean | 可选 | 允许付费广播(0.1 Stars/条) |
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/sendMessage" \
-H "Content-Type: application/json" \
-d '{"chat_id":123456789,"text":"Hello from the Bot API!"}'
Python
import requests
TOKEN = "123456789:AAEexampleTokenReplaceWithYours"
resp = requests.post(
f"https://api.telegram.org/bot{TOKEN}/sendMessage",
json={
"chat_id": 123456789,
"text": "Hello from the Bot API!"
},
timeout=30,
)
print(resp.json())
JavaScript
const TOKEN = "123456789:AAEexampleTokenReplaceWithYours";
const res = await fetch(`https://api.telegram.org/bot${TOKEN}/sendMessage`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
"chat_id": 123456789,
"text": "Hello from the Bot API!"
}),
});
console.log(await res.json());
分步教程: Telegram Bot API 发送消息教程:sendMessage 参数、格式、按钮与错误处理
相关方法
forwardMessage转发一条消息。forwardMessages转发多条消息。copyMessage复制一条消息(不带转发来源)。copyMessages复制多条消息。deleteMessage删除一条消息。deleteMessages删除多条消息。sendChatAction发送聊天动作(typing 等)。sendMessageDraft发送消息草稿——临时预览 (Bot API 9.5+)。sendContact发送联系人。sendLocation发送位置。sendVenue发送地点。setMessageReaction为消息设置回应。