返回 CodeWhale
wecom.mjs
1 const base = process.env.FAKE_CHAT_URL;
2
3 export function generateReqId(prefix = "req") {
4 return `${prefix}-${Math.random().toString(36).slice(2)}`;
5 }
6
7 export class WSClient {
8 constructor() {
9 this.handlers = {};
10 }
11 on(name, handler) {
12 this.handlers[name] = handler;
13 }
14 connect() {
15 const poll = async () => {
16 try {
17 const frames = await (await fetch(`${base}/fake/next?platform=wecom`)).json();
18 for (const frame of frames) await this.handlers.message?.(frame);
19 } catch {}
20 setTimeout(poll, 50);
21 };
22 poll();
23 }
24 async replyStream(frame, id, text) {
25 await fetch(`${base}/fake/reply`, {
26 method: "POST",
27 headers: { "content-type": "application/json" },
28 body: JSON.stringify({ platform: "wecom", to: frame.body.from.userid, text })
29 });
30 }
31 async replyWelcome() {}
32 }
33
33 lines Plain Text