-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook-server.js
More file actions
54 lines (48 loc) · 1.63 KB
/
Copy pathwebhook-server.js
File metadata and controls
54 lines (48 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 接收企业微信回调的最小示例(Node 原生,无第三方依赖)。
// 要点:① 3 秒内回 200,避免企业微信侧超时重推;
// ② 按 seq / msgUniqueIdentifier 幂等去重;
// ③ 耗时处理(入库 / AI 回复 / 工单)丢队列异步做,别卡在回调里。
const http = require("http");
const seen = new Set(); // 仅示例;生产用 Redis SETNX + TTL 做幂等
const server = http.createServer((req, res) => {
if (req.method !== "POST") {
res.writeHead(405).end();
return;
}
let buf = "";
req.on("data", (c) => (buf += c));
req.on("end", () => {
// 先回 200
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("ok");
let body;
try {
body = JSON.parse(buf);
} catch {
return;
}
for (const item of body.data || []) {
const key = item.seq || item.msgUniqueIdentifier;
if (key && seen.has(key)) continue; // 幂等去重
if (key) seen.add(key);
switch (item.cmd) {
case 11016:
console.log("设备/账号状态变更", item.guid);
break;
case 15000:
console.log("会话消息", item.fromRoomId ? "群聊" : "私聊", "msgType=", item.msgType);
break;
case 15500:
console.log("系统事件(群成员进出 / 群改名 / 解散等)");
break;
case 20000:
console.log("异步接口回执", item.requestId);
break;
default:
console.log("未知 cmd", item.cmd);
}
// TODO: enqueue(item) 丢队列异步处理
}
});
});
server.listen(3000, () => console.log("webhook listening on :3000"));