返回 CodeWhale
lark.mjs
1 const base = process.env.FAKE_CHAT_URL;
2
3 export const LoggerLevel = { info: 3 };
4 export const Domain = { Feishu: "https://feishu.invalid", Lark: "https://lark.invalid" };
5
6 async function record(chatId, data) {
7 await fetch(`${base}/fake/reply`, {
8 method: "POST",
9 headers: { "content-type": "application/json" },
10 body: JSON.stringify({ platform: "feishu", to: chatId, text: JSON.parse(data.content).text })
11 });
12 }
13
14 const chatOfMessage = new Map();
15
16 export class Client {
17 constructor() {
18 this.im = {
19 v1: {
20 message: {
21 create: async ({ params, data }) => record(data.receive_id || params?.receive_id, data),
22 reply: async ({ path, data }) => record(chatOfMessage.get(path.message_id) || "unknown", data)
23 }
24 }
25 };
26 }
27 }
28
29 export class EventDispatcher {
30 register(handlers) {
31 this.handlers = handlers;
32 return this;
33 }
34 }
35
36 export class WSClient {
37 start({ eventDispatcher }) {
38 const poll = async () => {
39 try {
40 const events = await (await fetch(`${base}/fake/next?platform=feishu`)).json();
41 for (const event of events) {
42 chatOfMessage.set(event.message.message_id, event.message.chat_id);
43 await eventDispatcher.handlers["im.message.receive_v1"](event);
44 }
45 } catch {}
46 setTimeout(poll, 50);
47 };
48 poll();
49 }
50 }
51
51 lines Plain Text