回调文档
小微云会在消息、实例状态和联系人同步发生变化时,向你的服务发送 HTTPS POST 请求。每个租户只能配置一个回调地址;接收方应以事件 ID 去重并异步处理。
配置回调
在控制台的「Webhook」页面创建或更新配置。回调地址必须是公网可访问的 HTTPS 地址,使用 443 端口;不支持 URL 查询参数、片段或重定向。保存后的凭据不会在控制台或管理接口响应中再次返回。
{
"url": "https://receiver.example/webhook",
"access_token": "receiver-issued-token",
"enabled": true,
"subscriptions": {
"events": ["message.received", "instance.status_changed"],
"instance_ids": ["35", "36"],
"message_types": ["text", "image"],
"chat_types": ["direct", "group"]
}
}| 字段 | 必填 | 说明 |
|---|---|---|
url | 是 | 接收地址。仅支持公网 HTTPS;URL 最长 2048 字符。 |
access_token | 创建时是 | 接收方校验用的 Bearer 凭据,1–1024 个可打印 ASCII 字符。更新时省略即保留已有凭据。 |
enabled | 是 | 是否投递新业务事件。关闭后不发送业务事件;测试事件仍可发送。 |
subscriptions.events | 是 | 订阅的事件类型;空数组表示不订阅任何业务事件。 |
subscriptions.instance_ids | 否 | 实例 ID 字符串列表,最多 1000 个。省略或 null 表示全部实例;空数组表示不订阅任何实例。 |
subscriptions.message_types | 消息事件需要 | text、image、voice、video、emoji、system、unknown。 |
subscriptions.chat_types | 消息事件需要 | direct、group、system、unknown。 |
筛选维度之间为 AND,同一列表内为 OR。消息类型和聊天类型仅对 message.* 事件生效;其值为空、null 或省略时不会匹配消息事件。订阅变更仅影响后续新事件,不会补投历史事件。
投递协议
POST /webhook HTTP/1.1
Authorization: Bearer <access_token>
Content-Type: application/json
Webhook-Id: 78d22bb4-e88d-4b09-a591-2c989d67872c
Webhook-Delivery-Id: 1024
Webhook-Attempt: 1
{
"event_id": "78d22bb4-e88d-4b09-a591-2c989d67872c",
"type": "message.received",
"schema_version": 1,
"occurred_at": "2026-09-30T00:00:00Z",
"tenant_id": "12",
"instance_id": "35",
"account_id": "wxid_example",
"data": {}
}| 位置 | 字段 | 说明 |
|---|---|---|
| Header | Authorization | Bearer <access_token>,值来自回调配置。 |
| Header | Webhook-Id | 事件 UUID,与正文 event_id 相同;重试时不变,应用它做幂等去重。 |
| Header | Webhook-Delivery-Id | 平台投递任务 ID,以字符串表示。 |
| Header | Webhook-Attempt | 本任务的尝试序号,从 1 开始;人工重试会继续递增。 |
| Body | schema_version | 当前固定为 1。 |
| Body | occurred_at | 事件入队时间,RFC 3339 UTC 时间。 |
| Body | tenant_id | 租户 ID 字符串。 |
| Body | instance_id | 实例 ID 字符串;无关联实例时为 null。 |
| Body | account_id | 微信账号 ID;尚未关联账号时为 null。 |
接收方完成鉴权并可靠保存 event_id 后,应尽快返回任意 2xx 状态码;响应正文不会被平台解析。非 2xx、网络错误或超时会触发重试。请不要依赖事件的全局顺序。
event_id 写入带唯一约束的存储,再投递给异步任务。重复收到同一 ID 时直接返回 2xx,可避免超时重试造成重复处理。事件目录
| 类型 | 触发时机 |
|---|---|
message.received | 入站消息首次写入系统。 |
message.observed | 同步首次观察到本账号从其他设备发出的消息;不是发送回执。 |
message.send_result | 系统持久化的业务发送进入 accepted、failed 或 unknown。 |
instance.status_changed | 实例创建,或 status、desired_state、account_id 实际发生持久化变化。 |
contact.sync_completed | 联系人同步进入 succeeded、partial、failed 或 interrupted。 |
webhook.test | 在控制台手动测试时发送,不需要订阅,也不包含真实业务数据。 |
原始开放协议发送接口不保证产生 message.send_result;若后续微信同步观察到该消息,仍可能收到 message.observed。
消息回调
三种消息事件使用相同的 data 结构;字段值随消息方向、来源和处理状态变化。下方按消息类型提供的示例均为入站接收场景,出站或发送结果事件仍使用相同字段集合。
接收消息回调
事件类型为 message.received。一条入站消息首次写入系统时触发,通常 direction 为 inbound、status 为 received。消息类别由 message_type 标识,完整字段见“公共字段”和对应消息类型章节。
已观察消息回调
事件类型为 message.observed。同步首次观察到本账号在其他设备发出的消息时触发,通常 direction 为 outbound、status 为 observed。它不是平台发送请求的回执,不应据此判断某次 API 发送已送达。
发送结果回调
事件类型为 message.send_result。系统持久化的业务发送进入 accepted、failed 或 unknown 时触发,通常 direction 为 outbound;accepted 仅表示上游协议接受,不代表送达或已读。原始开放协议发送接口不保证产生此事件。
公共字段
每一条消息回调的 data 都包含下列字段。可空字段会返回 null,不会省略。
| 字段 | 类型 | 说明 |
|---|---|---|
message_id | string | 平台消息 ID。 |
server_message_id | string / null | 微信服务端消息 ID。 |
legacy_message_id | string / null | 微信协议 MsgId 的无符号十进制表示。下载图片、语音或视频时作为请求的 msg_id;不要用 server_message_id 替代。 |
request_id | string / null | 关联的发送请求 ID。 |
direction | string | 消息方向,例如 inbound 或 outbound。 |
source | string | 记录来源。 |
status | string | 消息记录状态;accepted 仅表示协议接受,并不代表送达或已读。 |
message_type | string | 可订阅值为 text、image、voice、video、emoji、system、unknown。底层的 file、link 尚无可靠结构化解析,不能订阅,也不会产生回调数据。 |
protocol_type | number / null | 上游协议消息类型编号。 |
chat_type | string | 会话类别,与配置中的 chat_types 枚举一致。 |
peer_username | string / null | 会话对端标识。 |
sender_username | string / null | 发送方标识。 |
recipient_username | string / null | 接收方标识。 |
content | string | 文本消息为正文;图片、语音、视频、表情为微信原始消息内容(通常为 XML),保留完整内容以提供下载参数。文本最多 64 KiB。 |
content_truncated | boolean | 仅文本正文可能因超过 64 KiB 被截断;媒体内容始终为完整原文且为 false。 |
message_at | RFC 3339 / null | 消息时间。 |
received_at | RFC 3339 / null | 系统接收时间。 |
error_code | string / null | 发送或处理错误码。 |
send_finished_at | RFC 3339 / null | 发送处理完成时间。 |
文本消息
message_type 固定为 text,content 是 UTF-8 文本正文。正文超过 64 KiB 时会在字符边界截断,并将 content_truncated 设为 true;当前文本的 protocol_type 为 1。
{
"message_id": "8901",
"server_message_id": "18446744073709550000",
"legacy_message_id": null,
"request_id": null,
"direction": "inbound",
"source": "wechat_sync",
"status": "received",
"message_type": "text",
"protocol_type": 1,
"chat_type": "direct",
"peer_username": "wxid_sender",
"sender_username": "wxid_sender",
"recipient_username": "wxid_example",
"content": "你好",
"content_truncated": false,
"message_at": "2026-09-30T00:00:00Z",
"received_at": "2026-09-30T00:00:01Z",
"error_code": null,
"send_finished_at": null
}媒体 content XML
图片、语音、视频和表情的 content 是微信消息正文的完整原文,通常为一个 msg 根节点及一个媒体子节点。平台不重新编码、补值或删除 XML 属性;下表是当前会用于识别和下载资源的常见结构。微信可能增加额外属性,接收方应忽略未知属性,不能把 XML 当作永久稳定的业务对象。
| 消息类型 | 节点 | content 中的常见属性 | 下载用途 |
|---|---|---|---|
image | msg.img | aeskey、cdnmidimgurl、cdnmidimgsize、cdnthumburl、cdnthumblength、length、md5 | 这些是原始媒体元数据。图片下载接口以公共字段 peer_username 和 legacy_message_id 定位消息;长度/分段参数按接口请求结构传入。 |
voice | msg.voicemsg | bufid、length、voicelength、voiceformat、endflag、clientmsgid | bufid → bufid,length → length;公共字段 peer_username → from_user_name,legacy_message_id → msg_id。 |
video | msg.videomsg | aeskey、cdnvideourl、cdnvideoaeskey、cdnthumburl、length、playlength | 这些是原始媒体元数据。视频下载接口以公共字段 peer_username 和 legacy_message_id 定位消息;长度/分段参数按接口请求结构传入。 |
emoji | msg.emoji | md5、len、productid、cdnurl、aeskey、externurl、externmd5 | cdnurl → file_no,aeskey → file_aes_key,用于 CDN 图片下载。 |
content 是 XML 字符串,不是 JSON 对象;请使用安全的 XML 解析器读取属性,不要以字符串替换方式修改它。下载接口中的数值字段应解析为无符号整数;若 legacy_message_id 为 null,本条消息不能使用依赖 msg_id 的下载接口。
图片消息
message_type 固定为 image。content 返回完整原始内容,其中的图片长度等字段按微信实际报文保留。下载时使用 peer_username 作为 to_wxid、legacy_message_id 转为无符号整数作为 msg_id,并从 content 读取 data_len、分段长度和压缩类型(缺省值按接口文档处理)。调用实例对应的 图片下载接口;wxid 可省略。
{
"message_id": "8901",
"server_message_id": "18446744073709550000",
"legacy_message_id": "4294967295",
"request_id": null,
"direction": "inbound",
"source": "wechat_sync",
"status": "received",
"message_type": "image",
"protocol_type": 3,
"chat_type": "direct",
"peer_username": "wxid_sender",
"sender_username": "wxid_sender",
"recipient_username": "wxid_example",
"content": "<msg><img aeskey=\"...\" cdnmidimgurl=\"...\" cdnmidimgsize=\"102400\" cdnthumburl=\"...\" cdnthumblength=\"8192\" length=\"102400\" md5=\"...\" /></msg>",
"content_truncated": false,
"message_at": "2026-09-30T00:00:00Z",
"received_at": "2026-09-30T00:00:01Z",
"error_code": null,
"send_finished_at": null
}下载请求体如下。section.start_pos 从 0 开始;按响应的 total_len、data_len 继续请求后续分段,并将每段 data.buffer Base64 解码后按偏移拼接。
{
"to_wxid": "wxid_sender",
"msg_id": 4294967295,
"data_len": 0,
"section": { "start_pos": 0, "data_len": 0 },
"compress_type": 0
}图片和视频分段下载的成功响应结构:
{
"code": "OK",
"message": "",
"data": {
"success": true,
"code": 0,
"message": "成功",
"data": {
"total_len": 102400,
"start_pos": 0,
"data_len": 65536,
"data": { "i_len": 65536, "buffer": "<base64>" }
},
"data62": "",
"debug": ""
}
}语音消息
message_type 固定为 voice。content 返回完整原始内容;从其中的 voicemsg 读取 bufid 和 length,以 peer_username 填充 from_user_name,并将 legacy_message_id 转为无符号整数填充 msg_id。随后调用实例对应的 语音下载接口;wxid 可省略。
{
"message_id": "8901",
"server_message_id": "18446744073709550000",
"legacy_message_id": "4294967295",
"request_id": null,
"direction": "inbound",
"source": "wechat_sync",
"status": "received",
"message_type": "voice",
"protocol_type": 34,
"chat_type": "direct",
"peer_username": "wxid_sender",
"sender_username": "wxid_sender",
"recipient_username": "wxid_example",
"content": "<msg><voicemsg bufid=\"123456\" length=\"3200\" voicelength=\"6000\" voiceformat=\"4\" endflag=\"1\" /></msg>",
"content_truncated": false,
"message_at": "2026-09-30T00:00:00Z",
"received_at": "2026-09-30T00:00:01Z",
"error_code": null,
"send_finished_at": null
}语音下载请求与响应结构如下。成功时将 data.data.buffer 按 Base64 解码;end_flag 为 1 表示最后一段。
{
"from_user_name": "wxid_sender",
"msg_id": 4294967295,
"bufid": "123456",
"length": 3200
}{
"code": "OK",
"message": "",
"data": {
"success": true,
"code": 0,
"message": "成功",
"data": {
"msg_id": 4294967295,
"offset": 0,
"length": 3200,
"voice_length": 3200,
"data": { "i_len": 3200, "buffer": "<base64>" },
"end_flag": 1
},
"data62": "",
"debug": ""
}
}视频消息
message_type 固定为 video。content 返回完整原始内容,其中的视频长度等字段按微信实际报文保留。下载时使用 peer_username 作为 to_wxid、legacy_message_id 转为无符号整数作为 msg_id,并从 content 读取 data_len、分段长度和压缩类型(缺省值按接口文档处理)。调用实例对应的 视频下载接口;wxid 可省略。
{
"message_id": "8901",
"server_message_id": "18446744073709550000",
"legacy_message_id": "4294967295",
"request_id": null,
"direction": "inbound",
"source": "wechat_sync",
"status": "received",
"message_type": "video",
"protocol_type": 43,
"chat_type": "direct",
"peer_username": "wxid_sender",
"sender_username": "wxid_sender",
"recipient_username": "wxid_example",
"content": "<msg><videomsg aeskey=\"...\" cdnvideourl=\"...\" cdnvideoaeskey=\"...\" cdnthumburl=\"...\" length=\"1048576\" playlength=\"12000\" /></msg>",
"content_truncated": false,
"message_at": "2026-09-30T00:00:00Z",
"received_at": "2026-09-30T00:00:01Z",
"error_code": null,
"send_finished_at": null
}视频下载请求体与图片一致;响应也采用上方的分段响应结构。用 start_pos 递增请求,直至已获取长度达到 total_len。
{
"to_wxid": "wxid_sender",
"msg_id": 4294967295,
"data_len": 0,
"section": { "start_pos": 0, "data_len": 0 },
"compress_type": 0
}表情消息
message_type 固定为 emoji。content 返回完整原始内容;从其中的 emoji 节点读取 CDN 文件标识(cdnurl)和 aeskey,分别填入 file_no 和 file_aes_key,调用实例对应的 CDN 图片下载接口。wxid 可省略。
{
"message_id": "8901",
"server_message_id": "18446744073709550000",
"legacy_message_id": "4294967295",
"request_id": null,
"direction": "inbound",
"source": "wechat_sync",
"status": "received",
"message_type": "emoji",
"protocol_type": 47,
"chat_type": "direct",
"peer_username": "wxid_sender",
"sender_username": "wxid_sender",
"recipient_username": "wxid_example",
"content": "<msg><emoji md5=\"...\" len=\"20480\" productid=\"...\" cdnurl=\"...\" aeskey=\"...\" /></msg>",
"content_truncated": false,
"message_at": "2026-09-30T00:00:00Z",
"received_at": "2026-09-30T00:00:01Z",
"error_code": null,
"send_finished_at": null
}表情 CDN 下载一次返回完整的已解密图片,data.data.image 是 Base64 字符串,解码后即为图片二进制。
{
"file_no": "<content 中 emoji.cdnurl>",
"file_aes_key": "<content 中 emoji.aeskey>"
}{
"code": "OK",
"message": "",
"data": {
"success": true,
"code": 0,
"message": "成功",
"data": { "image": "<完整图片 base64>" },
"data62": "",
"debug": ""
}
}系统消息
message_type 固定为 system,表示上游识别为系统类的消息。除公共字段外没有系统动作专用对象;content 始终为空字符串,content_truncated 为 false。不转发原始 XML 或内部字段,接收方应以 message_id 去重,而不是由内容推断系统动作。
{
"message_id": "8901",
"server_message_id": "18446744073709550000",
"legacy_message_id": "4294967295",
"request_id": null,
"direction": "inbound",
"source": "wechat_sync",
"status": "received",
"message_type": "system",
"protocol_type": null,
"chat_type": "direct",
"peer_username": "wxid_sender",
"sender_username": "wxid_sender",
"recipient_username": "wxid_example",
"content": "",
"content_truncated": false,
"message_at": "2026-09-30T00:00:00Z",
"received_at": "2026-09-30T00:00:01Z",
"error_code": null,
"send_finished_at": null
}未识别消息
message_type 固定为 unknown,表示平台尚未能可靠归类该上游消息。它仍包含完整公共字段,content 始终为空字符串,content_truncated 为 false;不会透传原始 XML、protobuf、会话信息或任何媒体凭据。接收方应安全记录并容忍这类事件,而非假设其业务语义。
{
"message_id": "8901",
"server_message_id": "18446744073709550000",
"legacy_message_id": "4294967295",
"request_id": null,
"direction": "inbound",
"source": "wechat_sync",
"status": "received",
"message_type": "unknown",
"protocol_type": null,
"chat_type": "direct",
"peer_username": "wxid_sender",
"sender_username": "wxid_sender",
"recipient_username": "wxid_example",
"content": "",
"content_truncated": false,
"message_at": "2026-09-30T00:00:00Z",
"received_at": "2026-09-30T00:00:01Z",
"error_code": null,
"send_finished_at": null
}实例状态变更回调
事件类型为 instance.status_changed。实例创建,或 status、desired_state、account_id 的持久化值实际改变时触发;data 记录变化前后的快照,而不是完整实例对象。
{
"previous": {
"status": "stopped",
"desired_state": "running",
"account_id": "wxid_example"
},
"current": {
"status": "running",
"desired_state": "running",
"account_id": "wxid_example"
},
"state_version": 8,
"reason_code": null
}| 字段 | 类型 | 说明 |
|---|---|---|
previous | object | 变更前的状态快照。创建实例时 status 和 desired_state 为空字符串。 |
previous.status | string | 变更前实例状态。 |
previous.desired_state | string | 变更前的目标运行状态。 |
previous.account_id | string / null | 变更前关联的微信账号 ID。 |
current | object | 变更后的状态快照。 |
current.status | string | 变更后实例状态。 |
current.desired_state | string | 变更后的目标运行状态。 |
current.account_id | string / null | 变更后关联的微信账号 ID;尚未登录或账号已清除时为 null。 |
state_version | number | 此次状态快照对应的实例版本。对于同一 instance_id,可据此忽略版本更小的旧事件。 |
reason_code | string / null | 本次变化关联的内部错误或原因码;无原因码时为 null。 |
同一实例的事件不保证按到达顺序排列。接收方应以 event_id 去重,并按 state_version 防止旧状态覆盖新状态。
联系人同步完成回调
事件类型为 contact.sync_completed。联系人同步任务结束为 succeeded、partial、failed 或 interrupted 时触发;data 是任务级汇总,不包含具体联系人列表或失败明细。
{
"task_id": "467",
"status": "partial",
"discovered_count": 120,
"success_count": 118,
"failed_count": 2,
"error_code": "CONTACT_DETAIL_FAILED",
"finished_at": "2026-09-30T00:03:12Z"
}| 字段 | 类型 | 说明 |
|---|---|---|
task_id | string | 联系人同步任务 ID。它是字符串,以避免 JavaScript 精度丢失。 |
status | string | 最终任务状态:succeeded、partial、failed 或 interrupted。 |
discovered_count | number | 本轮去重后发现的联系人总数。 |
success_count | number | 成功取得并保存详情的联系人数量。 |
failed_count | number | 详情处理失败的联系人数量。 |
error_code | string / null | 任务级归一化错误码。部分失败时可有值;无任务级错误时为 null。 |
finished_at | RFC 3339 / null | 任务结束或中断的时间。无法写入结束时间时为 null。 |
success_count + failed_count 不会超过 discovered_count;尚未完成处理的已发现联系人不会被计入前两者。需要联系人或失败项详情时,请使用对应的联系人与同步任务查询接口。
测试回调
事件类型为 webhook.test,由控制台手动触发,无需订阅且在回调配置关闭时仍可投递。它仅用于验证地址、凭据与接收方逻辑,不包含真实业务数据。
{
"test": true
}测试事件的 instance_id 与 account_id 均为 null;完整信封仍包含唯一的 event_id,并遵循相同的认证、2xx 成功判定和重试规则。
重试与投递记录
平台把 2xx 视为成功;默认请求超时为 10 秒,3xx 不会跟随。失败后最多自动尝试 10 次,采用带抖动的递增退避;429 或 503 的 Retry-After 会被采纳(最长 24 小时)。投递至少一次,不保证顺序。
正文默认保留 7 天,投递与尝试记录默认保留 30 天。控制台可以查看投递状态、HTTP 状态与尝试日志;有效期内的 failed 任务可以人工重试,且保留原有 event_id。
有关控制台 API 的统一响应外壳,请参阅响应规范。
