Skip to content

下载消息中的语音 ​

根据语音消息回调中的字段下载语音文件,返回通常为 silk 格式的语音地址。

接口地址: POST /getMsgVoice

格式说明

下载成功后,如需将 silk 格式转换成 MP3 等格式,可参考 silk-v3-decoder。

前置条件 ​

  • 当前 wId 对应的微信实例在线。
  • msgId、length、bufId、fromUser 来自语音消息回调。
  • fromUser 应传发送者 ID。

请求参数 ​

参数名必选类型说明
wId是string登录实例标识
msgId是number消息 ID
length是int语音长度,对应 XML 中的 length 字段
bufId是stringXML 中返回的 bufId 字段
fromUser是string发送者

请求示例 ​

http
POST {BASE_URL}/getMsgVoice
Authorization: Bearer <你的调用凭证>
Content-Type: application/json

{
  "wId": "{{wId}}",
  "msgId": 1114311129,
  "fromUser": "wxid_6tn88z16x6ou12",
  "bufId": "289139440622895483",
  "length": 3227
}
bash
curl -X POST '{BASE_URL}/getMsgVoice' \
  -H 'Authorization: Bearer <你的调用凭证>' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "wId": "{{wId}}",
  "msgId": 1114311129,
  "fromUser": "wxid_6tn88z16x6ou12",
  "bufId": "289139440622895483",
  "length": 3227
}'
python
import requests

url = "{BASE_URL}/getMsgVoice"
headers = {"Authorization": "Bearer <你的调用凭证>", "Content-Type": "application/json"}
body = """{
  "wId": "{{wId}}",
  "msgId": 1114311129,
  "fromUser": "wxid_6tn88z16x6ou12",
  "bufId": "289139440622895483",
  "length": 3227
}"""

resp = requests.post(url, headers=headers, data=body.encode("utf-8"))
print(resp.text)
java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

String url = "{BASE_URL}/getMsgVoice";
String body = "{\n" +
    "  \"wId\": \"{{wId}}\",\n" +
    "  \"msgId\": 1114311129,\n" +
    "  \"fromUser\": \"wxid_6tn88z16x6ou12\",\n" +
    "  \"bufId\": \"289139440622895483\",\n" +
    "  \"length\": 3227\n" +
    "}";

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
    .header("Authorization", "Bearer <你的调用凭证>")
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString(body))
    .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
php
<?php

$url = '{BASE_URL}/getMsgVoice';
$body = <<<'JSON'
{
  "wId": "{{wId}}",
  "msgId": 1114311129,
  "fromUser": "wxid_6tn88z16x6ou12",
  "bufId": "289139440622895483",
  "length": 3227
}
JSON;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer <你的调用凭证>',
    'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

响应参数 ​

参数名类型说明
codestring1000 成功;其他值都是失败,通常是 1001
messagestring反馈信息
dataobject下载结果
data.urlstring语音文件地址,通常为 silk 文件

成功响应 ​

json
{
  "code": "1000",
  "message": "成功",
  "data": {
    "url": "https://cdn.e-yun.example/msgVoice/e2045467-b1ac-4b3c-89f8-62ab2b3a2284.silk"
  }
}

错误响应 ​

json
{
  "code": "1001",
  "message": "失败",
  "data": null
}

使用建议 ​

  • 下载语音后如需播放,通常需要先做 silk 转码。
  • 语音下载参数建议从回调原始字段提取,不要手动猜测 bufId。
  • 返回地址可能有有效期,业务侧需要及时转存或消费。