Skip to content

sendMessage

发送文本消息到指定聊天。

请求

POST /:token/sendMessage

参数

参数类型必填描述
chat_idInteger/String目标聊天的唯一标识符或用户名
textString消息文本内容
business_connection_idString商业连接的唯一标识符,用于代表商业帐户发送消息
message_thread_idInteger目标消息线程(话题)的唯一标识符,仅用于超级群和频道
parse_modeString消息文本解析模式,支持 HTMLMarkdown
entitiesMessageEntity[]消息文本中的特殊实体列表,可替代 parse_mode 使用
link_preview_optionsObject链接预览生成选项
disable_notificationBoolean静默发送消息,用户将收到无声通知
protect_contentBoolean保护消息内容不被转发和保存
allow_paid_broadcastBoolean允许向频道聊天发送付费广播消息
message_effect_idString消息特效的唯一标识符
reply_parametersObject回复参数,描述要回复的消息
reply_markupObject自定义键盘或内联键盘标记,JSON 序列化对象
reply_to_message_idInteger要回复的消息 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
401Token 无效或已过期
403Bot 被该用户封禁或无权向该聊天发送消息
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
  }'