Skip to content

sendVoice

发送语音消息到指定聊天。支持直接上传文件或传入已有文件的 file_id

请求

POST /:token/sendVoice

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

参数

参数类型必填描述
chat_idInteger/String目标聊天的唯一标识符或用户名
voiceString/File语音文件或已上传文件的 file_id
captionString语音说明文字
parse_modeString说明文字的解析模式,支持 HTMLMarkdown
durationInteger语音时长(秒)
reply_markupObject自定义键盘或内联键盘标记,JSON 序列化对象

响应

返回发送成功的 Message 对象。

json
{
  "ok": true,
  "result": {
    "message_id": 103,
    "from": {
      "id": 123456789,
      "is_bot": true,
      "first_name": "MyBot",
      "username": "my_bot"
    },
    "chat": {
      "id": 987654321,
      "first_name": "User",
      "username": "user123",
      "type": "private"
    },
    "date": 1700000000,
    "voice": {
      "file_id": "AwACAgIAAxkBAAI...",
      "file_unique_id": "AQADAgAT...",
      "duration": 5,
      "mime_type": "audio/ogg",
      "file_size": 23456
    },
    "caption": "这是一条语音消息"
  }
}

错误码

错误码描述
400请求参数错误,如文件格式不支持
401Token 无效或已过期
403Bot 无权向该聊天发送消息
404聊天不存在
413文件体积过大
500服务器内部错误

示例

通过文件上传发送语音

bash
curl -X POST "https://api.example.com/<token>/sendVoice" \
  -F "chat_id=987654321" \
  -F "voice=@/path/to/voice.ogg" \
  -F "caption=这是一条语音消息" \
  -F "duration=5"

通过 file_id 发送语音

bash
curl -X POST "https://api.example.com/<token>/sendVoice" \
  -H "Content-Type: application/json" \
  -d '{
    "chat_id": 987654321,
    "voice": "AwACAgIAAxkBAAI...",
    "duration": 5
  }'