Appearance
发送语音
给好友或群聊发一条语音。
接口地址: POST /sendVoice
格式说明
- 仅支持 silk 格式,不支持 amr 格式。如需将 mp3 转换为 silk 格式,可参考:silk-v3-decoder
- 也可通过 下载消息中的语音 获取 silk 格式语音文件
说明与注意
length要填真实时长,填错了微信里显示的语音时长可能不准。
请求参数
| 参数名 | 必选 | 类型 | 说明 |
|---|---|---|---|
| wId | 是 | string | 登录实例标识 |
| wcId | 是 | string | 接收方微信 ID 或群 ID |
| content | 是 | string | 语音文件 URL(仅支持 silk 格式) |
| length | 是 | int | 语音时长(毫秒),可从回调消息的 voicelength 字段获取 |
请求示例
http
POST {BASE_URL}/sendVoice
Authorization: Bearer <你的调用凭证>
Content-Type: application/json
{
"wId": "0000016f-a719-5b44-0003-a567f79011fc",
"wcId": "jack_623555049",
"content": "https://xc-1300726975.cos.ap-shanghai.myqcloud.com/msgVoice/e17dd0a9-5c59-4a54-a3cd-1a4817f5dd29-1579005558791.silk",
"length": 1
}1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
bash
curl -X POST '{BASE_URL}/sendVoice' \
-H 'Authorization: Bearer <你的调用凭证>' \
-H 'Content-Type: application/json' \
--data-raw '{
"wId": "0000016f-a719-5b44-0003-a567f79011fc",
"wcId": "jack_623555049",
"content": "https://xc-1300726975.cos.ap-shanghai.myqcloud.com/msgVoice/e17dd0a9-5c59-4a54-a3cd-1a4817f5dd29-1579005558791.silk",
"length": 1
}'1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
python
import requests
url = "{BASE_URL}/sendVoice"
headers = {"Authorization": "Bearer <你的调用凭证>", "Content-Type": "application/json"}
body = """{
"wId": "0000016f-a719-5b44-0003-a567f79011fc",
"wcId": "jack_623555049",
"content": "https://xc-1300726975.cos.ap-shanghai.myqcloud.com/msgVoice/e17dd0a9-5c59-4a54-a3cd-1a4817f5dd29-1579005558791.silk",
"length": 1
}"""
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
2
3
4
5
6
7
8
9
10
11
12
13
java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
String url = "{BASE_URL}/sendVoice";
String body = "{\n" +
" \"wId\": \"0000016f-a719-5b44-0003-a567f79011fc\",\n" +
" \"wcId\": \"jack_623555049\",\n" +
" \"content\": \"https://xc-1300726975.cos.ap-shanghai.myqcloud.com/msgVoice/e17dd0a9-5c59-4a54-a3cd-1a4817f5dd29-1579005558791.silk\",\n" +
" \"length\": 1\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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
php
<?php
$url = '{BASE_URL}/sendVoice';
$body = <<<'JSON'
{
"wId": "0000016f-a719-5b44-0003-a567f79011fc",
"wcId": "jack_623555049",
"content": "https://xc-1300726975.cos.ap-shanghai.myqcloud.com/msgVoice/e17dd0a9-5c59-4a54-a3cd-1a4817f5dd29-1579005558791.silk",
"length": 1
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
成功响应
json
{
"code": "1000",
"message": "发送语音消息成功",
"data": {
"type": null,
"msgId": 697760541,
"newMsgId": 1375821081513076275,
"createTime": 1641458029,
"wcId": "jack_623555049"
}
}1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
错误响应
json
{
"message": "失败",
"code": "1001",
"data": null
}1
2
3
4
5
2
3
4
5
响应参数
| 参数名 | 类型 | 说明 |
|---|---|---|
| code | string | 1000 成功;其他值都是失败,通常是 1001 |
| message | string | 反馈信息 |
| data.type | int | 类型 |
| data.msgId | long | 消息 ID |
| data.newMsgId | long | 新消息 ID |
| data.createTime | long | 发送时间戳(秒) |
| data.wcId | string | 接收方 ID |