mirror of
https://github.com/d0zingcat/alert-message-center.git
synced 2026-05-13 15:09:19 +00:00
@@ -5,32 +5,39 @@ import { eventDispatcher } from '../event-handler';
|
||||
const feishuEvent = new Hono();
|
||||
|
||||
// Helper to adapt Hono request to Lark SDK request
|
||||
const adaptRequest = async (c: any) => {
|
||||
const headers = c.req.raw.headers;
|
||||
// Convert Headers object to Record<string, string>
|
||||
const headerRecord: Record<string, string> = {};
|
||||
headers.forEach((value: string, key: string) => {
|
||||
headerRecord[key] = value;
|
||||
});
|
||||
|
||||
return {
|
||||
headers: headerRecord,
|
||||
body: await c.req.json(),
|
||||
};
|
||||
};
|
||||
|
||||
feishuEvent.post('/', async (c) => {
|
||||
try {
|
||||
const req = await adaptRequest(c);
|
||||
const headers = c.req.raw.headers;
|
||||
const headerRecord: Record<string, string> = {};
|
||||
headers.forEach((value, key) => {
|
||||
headerRecord[key] = value;
|
||||
});
|
||||
|
||||
// Use the official SDK to handle the request
|
||||
// It handles URL verification, encryption, and dispatching
|
||||
const res = await lark.adaptDefault('/api/feishu/event', req, eventDispatcher, {
|
||||
autoChallenge: true,
|
||||
encryptKey: process.env.FEISHU_ENCRYPT_KEY
|
||||
}) as any;
|
||||
const body = await c.req.json();
|
||||
const req = {
|
||||
headers: headerRecord,
|
||||
body,
|
||||
};
|
||||
|
||||
return c.json(res?.body || {});
|
||||
// Use the official SDK functions directly for Hono compatibility
|
||||
// 1. Handle URL verification (Challenge)
|
||||
const { isChallenge, challenge } = lark.generateChallenge(body, {
|
||||
encryptKey: process.env.FEISHU_ENCRYPT_KEY || ''
|
||||
});
|
||||
|
||||
if (isChallenge) {
|
||||
return c.json(challenge);
|
||||
}
|
||||
|
||||
// 2. Dispatch event
|
||||
// The dispatcher expects an object containing headers and body
|
||||
const result = await eventDispatcher.invoke({
|
||||
...req.body,
|
||||
headers: req.headers
|
||||
});
|
||||
|
||||
return c.json(result || {});
|
||||
} catch (e) {
|
||||
console.error('[Feishu Event] Error:', e);
|
||||
return c.json({ error: 'Internal Server Error' }, 500);
|
||||
|
||||
Reference in New Issue
Block a user