fix: feishu event

Signed-off-by: d0zingcat <iamtangli42@gmail.com>
This commit is contained in:
2026-01-14 13:35:57 +08:00
parent 9afe1f093c
commit 75306a67b1
3 changed files with 47 additions and 21 deletions

View File

@@ -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);