@zhin.js/adapter-lark 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/README.md +58 -2
- package/adapters/lark/index.js +3 -3
- package/adapters/lark/index.ts +3 -3
- package/lib/cards.d.ts +23 -0
- package/lib/cards.js +77 -0
- package/lib/endpoint.d.ts +10 -5
- package/lib/endpoint.js +187 -53
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/long-connection.d.ts +16 -0
- package/lib/long-connection.js +88 -0
- package/lib/protocol.d.ts +7 -4
- package/lib/protocol.js +63 -2
- package/lib/stream-proxy.d.ts +7 -0
- package/lib/stream-proxy.js +17 -0
- package/lib/web-api-proxy.d.ts +11 -0
- package/lib/web-api-proxy.js +48 -0
- package/lib/webhook.d.ts +2 -0
- package/lib/webhook.js +13 -10
- package/package.json +17 -15
- package/schema.json +67 -0
- package/src/cards.ts +69 -0
- package/src/endpoint.ts +168 -53
- package/src/index.ts +7 -0
- package/src/long-connection.ts +87 -0
- package/src/protocol.ts +56 -2
- package/src/stream-proxy.ts +20 -0
- package/src/web-api-proxy.ts +51 -0
- package/src/webhook.ts +15 -9
package/src/webhook.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Lark/Feishu webhook HTTP: verification → signature → challenge/event → admit.
|
|
3
3
|
*/
|
|
4
|
+
import type { LarkCardAction } from './cards.js';
|
|
4
5
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
5
6
|
import type { HttpHost, HttpRouteRegistration } from '@zhin.js/host-http';
|
|
6
7
|
import { formatCompact, getLogger } from '@zhin.js/logger';
|
|
@@ -21,6 +22,7 @@ export interface LarkWebhookHandler {
|
|
|
21
22
|
readonly isOpen: boolean;
|
|
22
23
|
admitPlatform(name: string, event: unknown): void;
|
|
23
24
|
admit(msg: LarkMessage): void;
|
|
25
|
+
admitCardAction?(event: LarkCardAction): void;
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
export function registerLarkWebhookRoutes(
|
|
@@ -43,8 +45,18 @@ export async function handleLarkWebhookRequest(
|
|
|
43
45
|
try {
|
|
44
46
|
const rawBody = await readTextBody(request);
|
|
45
47
|
|
|
48
|
+
let event: LarkEventBody;
|
|
49
|
+
try {
|
|
50
|
+
event = JSON.parse(rawBody) as LarkEventBody;
|
|
51
|
+
} catch {
|
|
52
|
+
response.writeHead(200, { 'Content-Type': 'application/json' });
|
|
53
|
+
response.end(JSON.stringify({ code: 0, msg: 'success' }));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
46
57
|
if (handler.config.verificationToken) {
|
|
47
|
-
const
|
|
58
|
+
const bodyToken = (event as LarkEventBody & { header?: { token?: unknown } }).header?.token ?? event.token;
|
|
59
|
+
const token = typeof bodyToken === 'string' ? bodyToken : headerValue(request.headers, 'x-lark-request-token');
|
|
48
60
|
if (!verifyToken(token, handler.config.verificationToken)) {
|
|
49
61
|
logger.warn(formatCompact({ op: 'webhook', ok: false, error: 'invalid verification token' }));
|
|
50
62
|
response.writeHead(403, { 'Content-Type': 'text/plain' });
|
|
@@ -70,14 +82,6 @@ export async function handleLarkWebhookRequest(
|
|
|
70
82
|
}
|
|
71
83
|
}
|
|
72
84
|
|
|
73
|
-
let event: LarkEventBody;
|
|
74
|
-
try {
|
|
75
|
-
event = JSON.parse(rawBody) as LarkEventBody;
|
|
76
|
-
} catch {
|
|
77
|
-
response.writeHead(200, { 'Content-Type': 'application/json' });
|
|
78
|
-
response.end(JSON.stringify({ code: 0, msg: 'success' }));
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
85
|
|
|
82
86
|
if (handler.isOpen) {
|
|
83
87
|
const eventName = event.type === 'event_callback'
|
|
@@ -92,6 +96,8 @@ export async function handleLarkWebhookRequest(
|
|
|
92
96
|
return;
|
|
93
97
|
}
|
|
94
98
|
|
|
99
|
+
if ((event as LarkEventBody & { header?: { event_type?: string } }).header?.event_type === 'card.action.trigger' && handler.isOpen) handler.admitCardAction?.(event.event as LarkCardAction);
|
|
100
|
+
|
|
95
101
|
if (event.type === 'event_callback' && event.event?.message && handler.isOpen) {
|
|
96
102
|
const message: LarkMessage = {
|
|
97
103
|
...event.event.message,
|