Appearance
下载消息中的语音
根据语音消息回调中的字段下载语音文件,返回通常为 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 | 是 | string | XML 中返回的 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
}1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
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
}'1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
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)1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
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());1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
响应参数
| 参数名 | 类型 | 说明 |
|---|---|---|
| code | string | 1000 成功;其他值都是失败,通常是 1001 |
| message | string | 反馈信息 |
| data | object | 下载结果 |
| data.url | string | 语音文件地址,通常为 silk 文件 |
成功响应
json
{
"code": "1000",
"message": "成功",
"data": {
"url": "https://cdn.e-yun.example/msgVoice/e2045467-b1ac-4b3c-89f8-62ab2b3a2284.silk"
}
}1
2
3
4
5
6
7
2
3
4
5
6
7
错误响应
json
{
"code": "1001",
"message": "失败",
"data": null
}1
2
3
4
5
2
3
4
5
使用建议
- 下载语音后如需播放,通常需要先做 silk 转码。
- 语音下载参数建议从回调原始字段提取,不要手动猜测
bufId。 - 返回地址可能有有效期,业务侧需要及时转存或消费。