sendMessage
发送文本消息到指定聊天。
请求
POST /:token/sendMessage
参数
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| chat_id | Integer/String | 是 | 目标聊天的唯一标识符或用户名 |
| text | String | 是 | 消息文本内容 |
| business_connection_id | String | 否 | 商业连接的唯一标识符,用于代表商业帐户发送消息 |
| message_thread_id | Integer | 否 | 目标消息线程(话题)的唯一标识符,仅用于超级群和频道 |
| parse_mode | String | 否 | 消息文本解析模式,支持 HTML 或 Markdown |
| entities | MessageEntity[] | 否 | 消息文本中的特殊实体列表,可替代 parse_mode 使用 |
| link_preview_options | Object | 否 | 链接预览生成选项 |
| disable_notification | Boolean | 否 | 静默发送消息,用户将收到无声通知 |
| protect_content | Boolean | 否 | 保护消息内容不被转发和保存 |
| allow_paid_broadcast | Boolean | 否 | 允许向频道聊天发送付费广播消息 |
| message_effect_id | String | 否 | 消息特效的唯一标识符 |
| reply_parameters | Object | 否 | 回复参数,描述要回复的消息 |
| reply_markup | Object | 否 | 自定义键盘或内联键盘标记,JSON 序列化对象 |
| reply_to_message_id | Integer | 否 | 要回复的消息 ID,设置后该消息将作为引用回复发送 |
响应
返回发送成功的 Message 对象。
json
{
"ok": true,
"result": {
"message_id": 100,
"from": {
"id": 123456789,
"is_bot": true,
"first_name": "MyBot",
"username": "my_bot"
},
"chat": {
"id": 987654321,
"first_name": "User",
"username": "user123",
"type": "private"
},
"date": 1700000000,
"text": "Hello, World!"
}
}错误码
| 错误码 | 描述 |
|---|---|
| 400 | 请求参数错误,如缺少 text 或 chat_id |
| 401 | Token 无效或已过期 |
| 403 | Bot 被该用户封禁或无权向该聊天发送消息 |
| 404 | 聊天不存在 |
| 500 | 服务器内部错误 |
示例
cURL
bash
curl -X POST "https://api.example.com/<token>/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": 987654321,
"text": "Hello, World!",
"parse_mode": "HTML"
}'带内联键盘的消息
bash
curl -X POST "https://api.example.com/<token>/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": 987654321,
"text": "请选择一个选项:",
"reply_markup": {
"inline_keyboard": [
[
{"text": "选项 A", "callback_data": "option_a"},
{"text": "选项 B", "callback_data": "option_b"}
]
]
}
}'回复消息
bash
curl -X POST "https://api.example.com/<token>/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": 987654321,
"text": "这是一条回复",
"reply_to_message_id": 99
}'