切换主题
消息与账号事件
长轮询
GET /v2/events 适用于接收消息、响应好友请求通知、更新登录状态,以及获取后续引用回复需要的消息 ID。
| 查询参数 | 类型 | 说明 |
|---|---|---|
after | 十进制字符串 | 上一页最后处理完成的游标。首次省略或使用 0。 |
limit | integer | 默认 100,范围 1–100。 |
wait_ms | integer | 默认 20000,范围 0–20000。0 表示立即读取。 |
http
GET /v2/events?after=0&limit=50&wait_ms=20000data 字段 | 类型 | 说明 |
|---|---|---|
items | array | 按事件 ID 递增的事件。长轮询超时可为空。 |
next_cursor | string | 下一次查询的 after。 |
has_more | boolean | 缓冲区还有条目时可立即读下一页。 |
gap | boolean | 旧事件已淘汰或存在缺口时,重新同步相关业务状态。 |
session | string | 本次服务会话。变化或返回 409 session_changed 时,从新会话开始。 |
缓存最多 1000 条及 8 MiB,单次约 1 MiB 条目预算。保存游标时附上实例和会话,并在业务逻辑处理成功后推进。每个订阅者有自己的进度。
单条事件结构
{13 项}
"schema_version": "1.0",
"event_id": "17",
"type": "message.received",
"occurred_at_ms": 1796176800000,
"owner": {7 项},
"pid": 12345,
"session_id": "12345-example",
"wxid": "wxid_self",
"alias": null,
"nickname": "示例",
"login_state": "logged_in",
"identity_observed_at_ms": 1796176799000
},
"channel": "private",
"direction": "incoming",
"message_type": "text",
"from": {3 项},
"id": "wxid_sender",
"kind": "user",
"name": null
},
"to": {3 项},
"id": "wxid_self",
"kind": "user",
"name": null
},
"receiver": {3 项},
"id": "wxid_self",
"kind": "user",
"name": null
},
"conversation": {3 项},
"id": "wxid_sender",
"kind": "user",
"name": null
},
"message": {14 项}
"id": "9007199254740993",
"type": "text",
"message_type": 1,
"timestamp": 1796176800,
"sender_id": "wxid_sender",
"receiver_id": "wxid_self",
"conversation_id": "wxid_sender",
"native_from_id": "wxid_sender",
"native_to_id": "wxid_self",
"text": "你好",
"content": "你好",
"mentioned_ids": null,
"is_mentioned": null,
"truncated": false
}
}
| 字段 | 类型和用法 |
|---|---|
schema_version、event_id | 字符串,结构版本与本地事件 ID。 |
type | 见下方事件类型。 |
occurred_at_ms | Unix 毫秒,记录被观察到的时间。 |
owner.pid、session_id | 整数和字符串,归属实例。 |
owner.wxid、alias、nickname | 字符串或 null,事件发生时的本机账号资料。 |
owner.login_state、identity_observed_at_ms | 登录状态与资料采样时间。 |
channel | private、group、official_account、system、unknown。 |
direction | incoming、outgoing 或 unknown。 |
from、to、receiver、conversation | 实际发送者、逻辑目标、原生接收者、会话标识。各对象有 id、kind、name。名称无数据时为 null。群聊的逻辑目标为群 ID。 |
message_type | 文本类别,如 text、image、voice、video、app、transfer、red_packet、friend_request 或其他事件分类。 |
message.id、type、message_type | 微信消息 ID 字符串、消息类别、原生数字类型。引用使用 message.id。 |
message.timestamp | Unix 秒。 |
message.sender_id、receiver_id、conversation_id | 发言人、接收者、会话。 |
message.native_from_id、native_to_id | 原生传输标识。 |
message.text、content | 纯文本和原始内容,按类别可能为 null。 |
message.mentioned_ids、is_mentioned | 预留,当前为 null。 |
message.truncated | 布尔值,内容是否有截断。 |
message.payment | 支付时可选,含 kind、amount、amount_minor、currency、status 和对应 transfer_id 或 message_id。 |
account | account.changed 事件携带的本人资料快照,结构与本人资料接口一致但可能缺少 session,以 owner.session_id 为准。此事件省略 message。 |
type 目前包括 message.received、account.changed、friend.requested、system.notification、subscription.notification、payment.transfer_received、payment.red_packet_received。同名 message.received 也可能带 direction=outgoing,用方向字段判断。使用资料变化事件触发重新查询,避免把事件当作持久账号状态。
回调
Hook 启动时读取实例的 yunke_hook_settings.json。port 是本实例监听端口的十进制字符串,callBackUrl 是回调 HTTP(S) 地址字符串。空地址关闭投递。
{2 项}
"port": "14123",
"callBackUrl": "http://127.0.0.1:9000/yunke/events"
}
接收方接受 HTTP POST UTF-8 JSON,成功返回 2xx。旧版事件字段和平行的 runtime_event 同时提供,优先读取后者。队列最多保留 256 条或 4 MiB,投递失败后该次回调结束。使用长轮询游标补读仍在缓存范围内的事件。