Appearance
发送文本消息
发送纯文本消息。
说明与注意
conversationId必须是 JSON number,传字符串会报cannot unmarshal string into uint64;自聊要用本账号 uin 作目标,不能用伪房间 roomId,否则报-3020。响应为完整消息对象。
请求参数
| 参数 | 必填 | 类型 | 说明 |
|---|---|---|---|
appid | 是 | string | 实例 appid,标识操作哪个已登录账号。示例:we_xxxxxxxxxxxxxxx |
conversationId | 是 | int64 | 会话 id:单聊传对方 userId,群聊传 roomId,给自己发传当前账号 uin;必须是 JSON number。示例:1688855874759204 |
content | 是 | string | 文本内容。示例:示例文本消息 |
请求示例
http
POST {BASE_URL}/wx-api/api/message/sendText
Authorization: Bearer <你的 App Token>
Content-Type: application/json
{
"appid": "we_xxxxxxxxxxxxxxx",
"conversationId": 1688855874759204,
"content": "示例文本消息"
}1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
bash
curl -X POST '{BASE_URL}/wx-api/api/message/sendText' \
-H 'Authorization: Bearer <你的 App Token>' \
-H 'Content-Type: application/json' \
--data-raw '{
"appid": "we_xxxxxxxxxxxxxxx",
"conversationId": 1688855874759204,
"content": "示例文本消息"
}'1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
python
import requests
url = "{BASE_URL}/wx-api/api/message/sendText"
headers = {"Authorization": "Bearer <你的 App Token>", "Content-Type": "application/json"}
body = """{
"appid": "we_xxxxxxxxxxxxxxx",
"conversationId": 1688855874759204,
"content": "示例文本消息"
}"""
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
2
3
4
5
6
7
8
9
10
11
12
java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
String url = "{BASE_URL}/wx-api/api/message/sendText";
String body = "{\n" +
" \"appid\": \"we_xxxxxxxxxxxxxxx\",\n" +
" \"conversationId\": 1688855874759204,\n" +
" \"content\": \"示例文本消息\"\n" +
"}";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.header("Authorization", "Bearer <你的 App Token>")
.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
php
<?php
$url = '{BASE_URL}/wx-api/api/message/sendText';
$body = <<<'JSON'
{
"appid": "we_xxxxxxxxxxxxxxx",
"conversationId": 1688855874759204,
"content": "示例文本消息"
}
JSON;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer <你的 App Token>',
'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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
响应示例
json
{
"code": 0,
"data": {
"id": 1145139,
"messageType": 0,
"contentType": 0,
"fromUserId": 1688850000000001,
"toUserId": 7881300000000002,
"sendTime": 1788664632,
"content": [
{
"type": 0,
"text": "示例文本消息"
}
]
},
"detail": "",
"message": "ok",
"time": "2026-09-06 11:17:13"
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
响应字段
响应字段(22 项)
| 字段 | 类型 | 说明 |
|---|---|---|
code | int64 | 统一封套状态码:0=成功,负数=失败 |
data | object | 业务数据 |
data.id | int64 | 消息服务端 id,撤回时作为 serverMsgId |
data.syncKey | int64 | 同步游标;增量拉取的断点 |
data.messageType | int64 | 会话类型:0 好友(单聊)、1 群聊、3 系统通知,与 Webhook 报文外层的 messageType 同一取值,见会话类型 |
data.fromUserId | int64 | 发送方 id(uin);群消息里是真实发送成员 |
data.toUserId | int64 | 接收方 id(uin),通常是本账号 |
data.roomId | int64 | 群会话 id;非群消息为 0 |
data.contentType | int64 | 消息类型码(0 文本/14 图片/23 视频…) |
data.sendTime | int64 | 发送时间(Unix 秒) |
data.appInfo | string | 企微内部透传串(base64);群发/待发送场景需回填,业务方不必解析 |
data.senderName | string | 发送者昵称(可能为空) |
data.content | array<object> | 消息内容;文本为数组、媒体为对象;未解析的消息为 {msgType, hex}(hex=protobuf 原始十六进制) |
data.content[].type | int64 | 类型编码(具体含义随所在对象:成员/部门、图片/视频、节点类型等,详见对应接口) |
data.content[].text | string | type 0/3 的文本内容 |
data.extraData | string | 企微内部透传(新协议为 base64 字符串;老协议曾为字符串数组);业务方不解析 |
data.flag / data.devInfo / data.summary | int64 / string | 消息标志位 / 设备信息 / 摘要;观测到 flag 取 0 或 16777216,summary 常为空。精确含义待确认 |
detail | string | 附加明细,通常为空 |
message | string | 文案:成功为 ok;失败形如 -码|描述 |
time | string | 服务端处理时间 YYYY-MM-DD HH:MM:SS |
常见错误
下表为企微错误码。业务失败时顶层 code=-1,具体错误码在 message 的 | 前,例如 -3004|参数错误;鉴权与网关错误见错误码。
| 错误码 | 含义 | 怎么办 |
|---|---|---|
-4014 | 过期或错误的 uin 作 conversationId | 先 personal/getInfo 取当前 uin |
-3020 | 用自聊伪房间 roomId 作发送目标 | 给自己发用当前账号 uin |
-3004 | 参数错误 | 检查字段名与类型,数字 id 不能传字符串 |