@tencent-connect/qqbot-nodejs 1.0.3
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/README.md +205 -0
- package/USAGE.md +804 -0
- package/dist/QQBot.d.ts +536 -0
- package/dist/QQBot.d.ts.map +1 -0
- package/dist/QQBot.js +752 -0
- package/dist/QQBot.js.map +1 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/access-policy.d.ts +54 -0
- package/dist/middleware/access-policy.d.ts.map +1 -0
- package/dist/middleware/access-policy.js +93 -0
- package/dist/middleware/access-policy.js.map +1 -0
- package/dist/middleware/concurrency-guard.d.ts +105 -0
- package/dist/middleware/concurrency-guard.d.ts.map +1 -0
- package/dist/middleware/concurrency-guard.js +325 -0
- package/dist/middleware/concurrency-guard.js.map +1 -0
- package/dist/middleware/content-sanitizer.d.ts +47 -0
- package/dist/middleware/content-sanitizer.d.ts.map +1 -0
- package/dist/middleware/content-sanitizer.js +85 -0
- package/dist/middleware/content-sanitizer.js.map +1 -0
- package/dist/middleware/envelope-formatter.d.ts +51 -0
- package/dist/middleware/envelope-formatter.d.ts.map +1 -0
- package/dist/middleware/envelope-formatter.js +97 -0
- package/dist/middleware/envelope-formatter.js.map +1 -0
- package/dist/middleware/error-handler.d.ts +36 -0
- package/dist/middleware/error-handler.d.ts.map +1 -0
- package/dist/middleware/error-handler.js +59 -0
- package/dist/middleware/error-handler.js.map +1 -0
- package/dist/middleware/history-buffer.d.ts +84 -0
- package/dist/middleware/history-buffer.d.ts.map +1 -0
- package/dist/middleware/history-buffer.js +113 -0
- package/dist/middleware/history-buffer.js.map +1 -0
- package/dist/middleware/index.d.ts +71 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/middleware/index.js +48 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/mention-gate.d.ts +99 -0
- package/dist/middleware/mention-gate.d.ts.map +1 -0
- package/dist/middleware/mention-gate.js +117 -0
- package/dist/middleware/mention-gate.js.map +1 -0
- package/dist/middleware/message-filter.d.ts +38 -0
- package/dist/middleware/message-filter.d.ts.map +1 -0
- package/dist/middleware/message-filter.js +60 -0
- package/dist/middleware/message-filter.js.map +1 -0
- package/dist/middleware/quote-ref.d.ts +106 -0
- package/dist/middleware/quote-ref.d.ts.map +1 -0
- package/dist/middleware/quote-ref.js +168 -0
- package/dist/middleware/quote-ref.js.map +1 -0
- package/dist/middleware/rate-limiter.d.ts +43 -0
- package/dist/middleware/rate-limiter.d.ts.map +1 -0
- package/dist/middleware/rate-limiter.js +77 -0
- package/dist/middleware/rate-limiter.js.map +1 -0
- package/dist/middleware/slash-command.d.ts +112 -0
- package/dist/middleware/slash-command.d.ts.map +1 -0
- package/dist/middleware/slash-command.js +203 -0
- package/dist/middleware/slash-command.js.map +1 -0
- package/dist/middleware/types.d.ts +175 -0
- package/dist/middleware/types.d.ts.map +1 -0
- package/dist/middleware/types.js +138 -0
- package/dist/middleware/types.js.map +1 -0
- package/dist/middleware/typing-indicator.d.ts +54 -0
- package/dist/middleware/typing-indicator.d.ts.map +1 -0
- package/dist/middleware/typing-indicator.js +68 -0
- package/dist/middleware/typing-indicator.js.map +1 -0
- package/dist/protocol/api/api-client.d.ts +27 -0
- package/dist/protocol/api/api-client.d.ts.map +1 -0
- package/dist/protocol/api/api-client.js +119 -0
- package/dist/protocol/api/api-client.js.map +1 -0
- package/dist/protocol/api/media-chunked.d.ts +68 -0
- package/dist/protocol/api/media-chunked.d.ts.map +1 -0
- package/dist/protocol/api/media-chunked.js +298 -0
- package/dist/protocol/api/media-chunked.js.map +1 -0
- package/dist/protocol/api/media.d.ts +46 -0
- package/dist/protocol/api/media.d.ts.map +1 -0
- package/dist/protocol/api/media.js +101 -0
- package/dist/protocol/api/media.js.map +1 -0
- package/dist/protocol/api/messages.d.ts +86 -0
- package/dist/protocol/api/messages.d.ts.map +1 -0
- package/dist/protocol/api/messages.js +200 -0
- package/dist/protocol/api/messages.js.map +1 -0
- package/dist/protocol/api/retry.d.ts +23 -0
- package/dist/protocol/api/retry.d.ts.map +1 -0
- package/dist/protocol/api/retry.js +102 -0
- package/dist/protocol/api/retry.js.map +1 -0
- package/dist/protocol/api/routes.d.ts +23 -0
- package/dist/protocol/api/routes.d.ts.map +1 -0
- package/dist/protocol/api/routes.js +50 -0
- package/dist/protocol/api/routes.js.map +1 -0
- package/dist/protocol/api/token.d.ts +38 -0
- package/dist/protocol/api/token.d.ts.map +1 -0
- package/dist/protocol/api/token.js +191 -0
- package/dist/protocol/api/token.js.map +1 -0
- package/dist/protocol/gateway/codec.d.ts +9 -0
- package/dist/protocol/gateway/codec.d.ts.map +1 -0
- package/dist/protocol/gateway/codec.js +32 -0
- package/dist/protocol/gateway/codec.js.map +1 -0
- package/dist/protocol/gateway/constants.d.ts +71 -0
- package/dist/protocol/gateway/constants.d.ts.map +1 -0
- package/dist/protocol/gateway/constants.js +91 -0
- package/dist/protocol/gateway/constants.js.map +1 -0
- package/dist/protocol/gateway/event-dispatcher.d.ts +97 -0
- package/dist/protocol/gateway/event-dispatcher.d.ts.map +1 -0
- package/dist/protocol/gateway/event-dispatcher.js +155 -0
- package/dist/protocol/gateway/event-dispatcher.js.map +1 -0
- package/dist/protocol/gateway/gateway-connection.d.ts +73 -0
- package/dist/protocol/gateway/gateway-connection.d.ts.map +1 -0
- package/dist/protocol/gateway/gateway-connection.js +293 -0
- package/dist/protocol/gateway/gateway-connection.js.map +1 -0
- package/dist/protocol/gateway/reconnect.d.ts +25 -0
- package/dist/protocol/gateway/reconnect.d.ts.map +1 -0
- package/dist/protocol/gateway/reconnect.js +118 -0
- package/dist/protocol/gateway/reconnect.js.map +1 -0
- package/dist/protocol/index.d.ts +41 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +46 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/protocol/transport/index.d.ts +8 -0
- package/dist/protocol/transport/index.d.ts.map +1 -0
- package/dist/protocol/transport/index.js +4 -0
- package/dist/protocol/transport/index.js.map +1 -0
- package/dist/protocol/transport/types.d.ts +109 -0
- package/dist/protocol/transport/types.d.ts.map +1 -0
- package/dist/protocol/transport/types.js +15 -0
- package/dist/protocol/transport/types.js.map +1 -0
- package/dist/protocol/transport/webhook-server-node.d.ts +14 -0
- package/dist/protocol/transport/webhook-server-node.d.ts.map +1 -0
- package/dist/protocol/transport/webhook-server-node.js +57 -0
- package/dist/protocol/transport/webhook-server-node.js.map +1 -0
- package/dist/protocol/transport/webhook-verify.d.ts +49 -0
- package/dist/protocol/transport/webhook-verify.d.ts.map +1 -0
- package/dist/protocol/transport/webhook-verify.js +98 -0
- package/dist/protocol/transport/webhook-verify.js.map +1 -0
- package/dist/protocol/transport/webhook.d.ts +37 -0
- package/dist/protocol/transport/webhook.d.ts.map +1 -0
- package/dist/protocol/transport/webhook.js +183 -0
- package/dist/protocol/transport/webhook.js.map +1 -0
- package/dist/protocol/types.d.ts +270 -0
- package/dist/protocol/types.d.ts.map +1 -0
- package/dist/protocol/types.js +59 -0
- package/dist/protocol/types.js.map +1 -0
- package/dist/protocol/utils/audio.d.ts +79 -0
- package/dist/protocol/utils/audio.d.ts.map +1 -0
- package/dist/protocol/utils/audio.js +501 -0
- package/dist/protocol/utils/audio.js.map +1 -0
- package/dist/protocol/utils/decode-media-path.d.ts +26 -0
- package/dist/protocol/utils/decode-media-path.d.ts.map +1 -0
- package/dist/protocol/utils/decode-media-path.js +79 -0
- package/dist/protocol/utils/decode-media-path.js.map +1 -0
- package/dist/protocol/utils/ffmpeg.d.ts +38 -0
- package/dist/protocol/utils/ffmpeg.d.ts.map +1 -0
- package/dist/protocol/utils/ffmpeg.js +119 -0
- package/dist/protocol/utils/ffmpeg.js.map +1 -0
- package/dist/protocol/utils/file-utils.d.ts +42 -0
- package/dist/protocol/utils/file-utils.d.ts.map +1 -0
- package/dist/protocol/utils/file-utils.js +108 -0
- package/dist/protocol/utils/file-utils.js.map +1 -0
- package/dist/protocol/utils/format.d.ts +10 -0
- package/dist/protocol/utils/format.d.ts.map +1 -0
- package/dist/protocol/utils/format.js +68 -0
- package/dist/protocol/utils/format.js.map +1 -0
- package/dist/protocol/utils/image-size.d.ts +56 -0
- package/dist/protocol/utils/image-size.d.ts.map +1 -0
- package/dist/protocol/utils/image-size.js +185 -0
- package/dist/protocol/utils/image-size.js.map +1 -0
- package/dist/protocol/utils/media-source.d.ts +145 -0
- package/dist/protocol/utils/media-source.d.ts.map +1 -0
- package/dist/protocol/utils/media-source.js +175 -0
- package/dist/protocol/utils/media-source.js.map +1 -0
- package/dist/protocol/utils/media-tags.d.ts +15 -0
- package/dist/protocol/utils/media-tags.d.ts.map +1 -0
- package/dist/protocol/utils/media-tags.js +150 -0
- package/dist/protocol/utils/media-tags.js.map +1 -0
- package/dist/protocol/utils/media-type.d.ts +41 -0
- package/dist/protocol/utils/media-type.d.ts.map +1 -0
- package/dist/protocol/utils/media-type.js +89 -0
- package/dist/protocol/utils/media-type.js.map +1 -0
- package/dist/protocol/utils/payload.d.ts +52 -0
- package/dist/protocol/utils/payload.d.ts.map +1 -0
- package/dist/protocol/utils/payload.js +100 -0
- package/dist/protocol/utils/payload.js.map +1 -0
- package/dist/protocol/utils/ref-attachments.d.ts +80 -0
- package/dist/protocol/utils/ref-attachments.d.ts.map +1 -0
- package/dist/protocol/utils/ref-attachments.js +121 -0
- package/dist/protocol/utils/ref-attachments.js.map +1 -0
- package/dist/protocol/utils/ref-index-store.d.ts +69 -0
- package/dist/protocol/utils/ref-index-store.d.ts.map +1 -0
- package/dist/protocol/utils/ref-index-store.js +227 -0
- package/dist/protocol/utils/ref-index-store.js.map +1 -0
- package/dist/protocol/utils/reply-limiter.d.ts +75 -0
- package/dist/protocol/utils/reply-limiter.d.ts.map +1 -0
- package/dist/protocol/utils/reply-limiter.js +123 -0
- package/dist/protocol/utils/reply-limiter.js.map +1 -0
- package/dist/protocol/utils/session-store.d.ts +73 -0
- package/dist/protocol/utils/session-store.d.ts.map +1 -0
- package/dist/protocol/utils/session-store.js +274 -0
- package/dist/protocol/utils/session-store.js.map +1 -0
- package/dist/protocol/utils/target.d.ts +53 -0
- package/dist/protocol/utils/target.d.ts.map +1 -0
- package/dist/protocol/utils/target.js +104 -0
- package/dist/protocol/utils/target.js.map +1 -0
- package/dist/protocol/utils/text-chunk.d.ts +21 -0
- package/dist/protocol/utils/text-chunk.d.ts.map +1 -0
- package/dist/protocol/utils/text-chunk.js +25 -0
- package/dist/protocol/utils/text-chunk.js.map +1 -0
- package/dist/protocol/utils/text-parsing.d.ts +40 -0
- package/dist/protocol/utils/text-parsing.d.ts.map +1 -0
- package/dist/protocol/utils/text-parsing.js +126 -0
- package/dist/protocol/utils/text-parsing.js.map +1 -0
- package/dist/protocol/utils/upload-cache.d.ts +27 -0
- package/dist/protocol/utils/upload-cache.d.ts.map +1 -0
- package/dist/protocol/utils/upload-cache.js +73 -0
- package/dist/protocol/utils/upload-cache.js.map +1 -0
- package/dist/protocol/utils/voice-text.d.ts +9 -0
- package/dist/protocol/utils/voice-text.d.ts.map +1 -0
- package/dist/protocol/utils/voice-text.js +16 -0
- package/dist/protocol/utils/voice-text.js.map +1 -0
- package/dist/storage/index.d.ts +23 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +21 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/kv-store.d.ts +85 -0
- package/dist/storage/kv-store.d.ts.map +1 -0
- package/dist/storage/kv-store.js +184 -0
- package/dist/storage/kv-store.js.map +1 -0
- package/dist/storage/session-adapter.d.ts +35 -0
- package/dist/storage/session-adapter.d.ts.map +1 -0
- package/dist/storage/session-adapter.js +48 -0
- package/dist/storage/session-adapter.js.map +1 -0
- package/dist/streaming.d.ts +67 -0
- package/dist/streaming.d.ts.map +1 -0
- package/dist/streaming.js +207 -0
- package/dist/streaming.js.map +1 -0
- package/package.json +90 -0
- package/src/QQBot.ts +1123 -0
- package/src/index.ts +175 -0
- package/src/middleware/access-policy.ts +139 -0
- package/src/middleware/concurrency-guard.ts +450 -0
- package/src/middleware/content-sanitizer.ts +125 -0
- package/src/middleware/envelope-formatter.ts +140 -0
- package/src/middleware/error-handler.ts +77 -0
- package/src/middleware/history-buffer.ts +169 -0
- package/src/middleware/index.ts +129 -0
- package/src/middleware/mention-gate.ts +227 -0
- package/src/middleware/message-filter.ts +82 -0
- package/src/middleware/quote-ref.ts +237 -0
- package/src/middleware/rate-limiter.ts +112 -0
- package/src/middleware/slash-command.ts +307 -0
- package/src/middleware/types.ts +273 -0
- package/src/middleware/typing-indicator.ts +107 -0
- package/src/protocol/api/api-client.ts +170 -0
- package/src/protocol/api/media-chunked.ts +517 -0
- package/src/protocol/api/media.ts +188 -0
- package/src/protocol/api/messages.ts +345 -0
- package/src/protocol/api/retry.ts +157 -0
- package/src/protocol/api/routes.ts +62 -0
- package/src/protocol/api/token.ts +250 -0
- package/src/protocol/gateway/codec.ts +33 -0
- package/src/protocol/gateway/constants.ts +98 -0
- package/src/protocol/gateway/event-dispatcher.ts +275 -0
- package/src/protocol/gateway/gateway-connection.ts +375 -0
- package/src/protocol/gateway/reconnect.ts +155 -0
- package/src/protocol/index.ts +238 -0
- package/src/protocol/transport/index.ts +17 -0
- package/src/protocol/transport/types.ts +121 -0
- package/src/protocol/transport/webhook-server-node.ts +64 -0
- package/src/protocol/transport/webhook-verify.ts +118 -0
- package/src/protocol/transport/webhook.ts +237 -0
- package/src/protocol/types.ts +305 -0
- package/src/protocol/utils/audio.ts +663 -0
- package/src/protocol/utils/decode-media-path.ts +88 -0
- package/src/protocol/utils/ffmpeg.ts +131 -0
- package/src/protocol/utils/file-utils.ts +121 -0
- package/src/protocol/utils/format.ts +69 -0
- package/src/protocol/utils/image-size.ts +236 -0
- package/src/protocol/utils/media-source.ts +243 -0
- package/src/protocol/utils/media-tags.ts +177 -0
- package/src/protocol/utils/media-type.ts +102 -0
- package/src/protocol/utils/payload.ts +157 -0
- package/src/protocol/utils/ref-attachments.ts +190 -0
- package/src/protocol/utils/ref-index-store.ts +279 -0
- package/src/protocol/utils/reply-limiter.ts +164 -0
- package/src/protocol/utils/session-store.ts +346 -0
- package/src/protocol/utils/target.ts +121 -0
- package/src/protocol/utils/text-chunk.ts +29 -0
- package/src/protocol/utils/text-parsing.ts +153 -0
- package/src/protocol/utils/upload-cache.ts +107 -0
- package/src/protocol/utils/voice-text.ts +16 -0
- package/src/storage/index.ts +24 -0
- package/src/storage/kv-store.ts +243 -0
- package/src/storage/session-adapter.ts +66 -0
- package/src/streaming.ts +255 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebhookTransport — receive QQ Bot events via HTTP POST callbacks.
|
|
3
|
+
*
|
|
4
|
+
* This transport implements the QQ Open Platform Webhook mode:
|
|
5
|
+
* 1. Starts an HTTP server (built-in or user-provided adapter)
|
|
6
|
+
* 2. Verifies Ed25519 signatures on every request
|
|
7
|
+
* 3. Handles callback URL validation (op:13)
|
|
8
|
+
* 4. Dispatches op:0 events through the same `dispatchEvent` as WebSocket
|
|
9
|
+
* 5. Returns op:12 ACK to QQ platform
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* ```ts
|
|
13
|
+
* const bot = new QQBot({
|
|
14
|
+
* appId, appSecret,
|
|
15
|
+
* transport: "webhook",
|
|
16
|
+
* webhook: { port: 8443, path: "/callback" },
|
|
17
|
+
* });
|
|
18
|
+
* await bot.start();
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { dispatchEvent } from "../gateway/event-dispatcher.js";
|
|
23
|
+
import type { Logger } from "../types.js";
|
|
24
|
+
import type {
|
|
25
|
+
EventTransport,
|
|
26
|
+
EventTransportCallbacks,
|
|
27
|
+
WebhookRequest,
|
|
28
|
+
WebhookResponse,
|
|
29
|
+
WebhookServerAdapter,
|
|
30
|
+
WebhookTransportOptions,
|
|
31
|
+
} from "./types.js";
|
|
32
|
+
import { signValidationResponse, verifyWebhookSignature } from "./webhook-verify.js";
|
|
33
|
+
import { NodeHttpWebhookServer } from "./webhook-server-node.js";
|
|
34
|
+
|
|
35
|
+
// OpCodes relevant to webhook
|
|
36
|
+
const OP_DISPATCH = 0;
|
|
37
|
+
const OP_HTTP_CALLBACK_ACK = 12;
|
|
38
|
+
const OP_VALIDATION = 13;
|
|
39
|
+
|
|
40
|
+
interface WebhookPayload {
|
|
41
|
+
id?: string;
|
|
42
|
+
op: number;
|
|
43
|
+
d?: unknown;
|
|
44
|
+
s?: number;
|
|
45
|
+
t?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class WebhookTransport implements EventTransport {
|
|
49
|
+
private readonly opts: WebhookTransportOptions;
|
|
50
|
+
private readonly callbacks: EventTransportCallbacks;
|
|
51
|
+
private readonly log: Logger | undefined;
|
|
52
|
+
private readonly server: WebhookServerAdapter;
|
|
53
|
+
private readonly accountId: string;
|
|
54
|
+
private stopped = false;
|
|
55
|
+
private stopResolve: (() => void) | null = null;
|
|
56
|
+
|
|
57
|
+
constructor(opts: WebhookTransportOptions, callbacks: EventTransportCallbacks) {
|
|
58
|
+
this.opts = opts;
|
|
59
|
+
this.callbacks = callbacks;
|
|
60
|
+
this.log = opts.log;
|
|
61
|
+
this.server = opts.server ?? new NodeHttpWebhookServer();
|
|
62
|
+
this.accountId = opts.accountId ?? opts.appId;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async start(): Promise<void> {
|
|
66
|
+
const port = this.opts.port ?? 8080;
|
|
67
|
+
const path = this.opts.path ?? "/";
|
|
68
|
+
|
|
69
|
+
this.log?.info?.(`[webhook] starting on port ${port}, path ${path}`);
|
|
70
|
+
|
|
71
|
+
await this.server.listen(port, path, (req) => this.handleRequest(req));
|
|
72
|
+
|
|
73
|
+
this.log?.info?.(`[webhook] listening on :${port}${path}`);
|
|
74
|
+
|
|
75
|
+
// Notify ready
|
|
76
|
+
this.callbacks.onReady?.({ transport: "webhook", port, path });
|
|
77
|
+
|
|
78
|
+
// Wait until stopped
|
|
79
|
+
if (this.opts.abortSignal) {
|
|
80
|
+
await new Promise<void>((resolve) => {
|
|
81
|
+
if (this.opts.abortSignal!.aborted) {
|
|
82
|
+
resolve();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
this.stopResolve = resolve;
|
|
86
|
+
this.opts.abortSignal!.addEventListener("abort", () => this.stop(), { once: true });
|
|
87
|
+
});
|
|
88
|
+
} else {
|
|
89
|
+
// Wait until stop() is called externally
|
|
90
|
+
await new Promise<void>((resolve) => {
|
|
91
|
+
this.stopResolve = resolve;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
stop(): void {
|
|
97
|
+
if (this.stopped) return;
|
|
98
|
+
this.stopped = true;
|
|
99
|
+
this.server.close();
|
|
100
|
+
this.log?.info?.(`[webhook] stopped`);
|
|
101
|
+
this.stopResolve?.();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ============ Request handler ============
|
|
105
|
+
|
|
106
|
+
private async handleRequest(req: WebhookRequest): Promise<WebhookResponse> {
|
|
107
|
+
// Parse body
|
|
108
|
+
let payload: WebhookPayload;
|
|
109
|
+
try {
|
|
110
|
+
payload = JSON.parse(req.body.toString("utf-8")) as WebhookPayload;
|
|
111
|
+
} catch {
|
|
112
|
+
this.log?.warn?.(`[webhook] invalid JSON body`);
|
|
113
|
+
return { status: 400, body: JSON.stringify({ error: "invalid json" }) };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ── op:13 — Callback URL validation (no signature check needed) ──
|
|
117
|
+
if (payload.op === OP_VALIDATION) {
|
|
118
|
+
return this.handleValidation(payload);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ── Signature verification ──
|
|
122
|
+
const timestamp = getHeader(req.headers, "x-signature-timestamp") ?? "";
|
|
123
|
+
const signature = getHeader(req.headers, "x-signature-ed25519") ?? "";
|
|
124
|
+
|
|
125
|
+
if (!timestamp || !signature) {
|
|
126
|
+
this.log?.warn?.(`[webhook] missing signature headers`);
|
|
127
|
+
return { status: 401, body: JSON.stringify({ error: "missing signature" }) };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const valid = verifyWebhookSignature({
|
|
131
|
+
body: req.body,
|
|
132
|
+
timestamp,
|
|
133
|
+
signature,
|
|
134
|
+
botSecret: this.opts.appSecret,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
if (!valid) {
|
|
138
|
+
this.log?.warn?.(`[webhook] signature verification failed`);
|
|
139
|
+
return { status: 401, body: JSON.stringify({ error: "invalid signature" }) };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ── op:0 — Dispatch event (async, fire-and-forget) ──
|
|
143
|
+
// Return ACK immediately, process message in background.
|
|
144
|
+
// Otherwise long-running handlers (AI inference) block the HTTP response,
|
|
145
|
+
// causing the QQ platform to timeout and re-deliver the event.
|
|
146
|
+
if (payload.op === OP_DISPATCH) {
|
|
147
|
+
this.handleDispatch(payload).catch((err) => {
|
|
148
|
+
this.log?.error?.(
|
|
149
|
+
`[webhook] dispatch error: ${err instanceof Error ? err.message : String(err)}`,
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Return ACK (op:12) for all valid requests
|
|
155
|
+
return {
|
|
156
|
+
status: 200,
|
|
157
|
+
body: JSON.stringify({ op: OP_HTTP_CALLBACK_ACK, d: 0 }),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ============ Validation handler (op:13) ============
|
|
162
|
+
|
|
163
|
+
private handleValidation(payload: WebhookPayload): WebhookResponse {
|
|
164
|
+
const d = payload.d as { plain_token?: string; event_ts?: string } | undefined;
|
|
165
|
+
|
|
166
|
+
if (!d?.plain_token || !d?.event_ts) {
|
|
167
|
+
this.log?.warn?.(`[webhook] validation missing plain_token or event_ts`);
|
|
168
|
+
return { status: 400, body: JSON.stringify({ error: "invalid validation" }) };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.log?.info?.(`[webhook] handling callback URL validation`);
|
|
172
|
+
|
|
173
|
+
const response = signValidationResponse({
|
|
174
|
+
plainToken: d.plain_token,
|
|
175
|
+
eventTs: d.event_ts,
|
|
176
|
+
botSecret: this.opts.appSecret,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
status: 200,
|
|
181
|
+
body: JSON.stringify(response),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ============ Dispatch handler (op:0) ============
|
|
186
|
+
|
|
187
|
+
private async handleDispatch(payload: WebhookPayload): Promise<void> {
|
|
188
|
+
const eventType = payload.t ?? "";
|
|
189
|
+
const data = payload.d;
|
|
190
|
+
|
|
191
|
+
this.log?.debug?.(
|
|
192
|
+
`[webhook] dispatch event: t=${eventType} payload=${JSON.stringify(data)}`,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const result = dispatchEvent(eventType, data, this.accountId, this.log);
|
|
196
|
+
|
|
197
|
+
switch (result.action) {
|
|
198
|
+
case "ready":
|
|
199
|
+
this.callbacks.onReady?.(result.data);
|
|
200
|
+
break;
|
|
201
|
+
case "resumed":
|
|
202
|
+
this.callbacks.onResumed?.(result.data);
|
|
203
|
+
break;
|
|
204
|
+
case "message":
|
|
205
|
+
try {
|
|
206
|
+
await this.callbacks.onMessage(result.msg);
|
|
207
|
+
} catch (err) {
|
|
208
|
+
this.callbacks.onError?.(
|
|
209
|
+
err instanceof Error ? err : new Error(String(err)),
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
break;
|
|
213
|
+
case "interaction":
|
|
214
|
+
try {
|
|
215
|
+
await this.callbacks.onInteraction?.(result.event);
|
|
216
|
+
} catch (err) {
|
|
217
|
+
this.callbacks.onError?.(
|
|
218
|
+
err instanceof Error ? err : new Error(String(err)),
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
break;
|
|
222
|
+
case "ignore":
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ============ Helpers ============
|
|
229
|
+
|
|
230
|
+
function getHeader(
|
|
231
|
+
headers: Record<string, string | string[] | undefined>,
|
|
232
|
+
key: string,
|
|
233
|
+
): string | undefined {
|
|
234
|
+
const val = headers[key];
|
|
235
|
+
if (Array.isArray(val)) return val[0];
|
|
236
|
+
return val;
|
|
237
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protocol-level public types for the QQ Open Platform.
|
|
3
|
+
*
|
|
4
|
+
* 这一层只关心与 QQ 官方 API 协议相关的数据结构(HTTP 请求/响应、
|
|
5
|
+
* WebSocket Gateway 事件、消息体、媒体类型等),不包含任何上层框架
|
|
6
|
+
* 业务概念(路由、ACP、access policy 等)。
|
|
7
|
+
*
|
|
8
|
+
* 上层应用通过 `QQBot` facade 与这些类型交互;高级用户也可以从
|
|
9
|
+
* `@tencent-connect/qqbot-nodejs/protocol` 直接消费这些低层类型。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// ============ Structured API Error ============
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* QQ 开放平台 HTTP 调用失败时抛出的结构化错误。
|
|
16
|
+
*
|
|
17
|
+
* 携带 HTTP 状态码、API 路径以及业务错误码,供上层重试/降级判断使用。
|
|
18
|
+
*/
|
|
19
|
+
export class ApiError extends Error {
|
|
20
|
+
override readonly name = "ApiError";
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
message: string,
|
|
24
|
+
/** HTTP status code returned by the QQ Open Platform. */
|
|
25
|
+
public readonly httpStatus: number,
|
|
26
|
+
/** API path that produced the error (e.g. `/v2/users/{id}/messages`). */
|
|
27
|
+
public readonly path: string,
|
|
28
|
+
/** Business error code from the response body (`code` or `err_code`). */
|
|
29
|
+
public readonly bizCode?: number,
|
|
30
|
+
/** Original error message from the response body. */
|
|
31
|
+
public readonly bizMessage?: string,
|
|
32
|
+
) {
|
|
33
|
+
super(message);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ============ Logger ============
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Logger interface used across all protocol modules.
|
|
41
|
+
*
|
|
42
|
+
* `info` and `error` are required; `warn` and `debug` are optional.
|
|
43
|
+
*/
|
|
44
|
+
export interface Logger {
|
|
45
|
+
info: (msg: string, meta?: Record<string, unknown>) => void;
|
|
46
|
+
error: (msg: string, meta?: Record<string, unknown>) => void;
|
|
47
|
+
warn?: (msg: string, meta?: Record<string, unknown>) => void;
|
|
48
|
+
debug?: (msg: string, meta?: Record<string, unknown>) => void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @deprecated Use {@link Logger}. */
|
|
52
|
+
export type EngineLogger = Logger;
|
|
53
|
+
|
|
54
|
+
// ============ Chat Scope ============
|
|
55
|
+
|
|
56
|
+
/** Chat scope used to unify C2C/Group path construction. */
|
|
57
|
+
export type ChatScope = "c2c" | "group";
|
|
58
|
+
|
|
59
|
+
// ============ Credentials ============
|
|
60
|
+
|
|
61
|
+
/** Credentials needed to authenticate API requests. */
|
|
62
|
+
export interface Credentials {
|
|
63
|
+
appId: string;
|
|
64
|
+
clientSecret: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ============ Message Response ============
|
|
68
|
+
|
|
69
|
+
/** Standard message send response from the QQ Open Platform. */
|
|
70
|
+
export interface MessageResponse {
|
|
71
|
+
id: string;
|
|
72
|
+
timestamp: number | string;
|
|
73
|
+
/** Reference index for future quoting. */
|
|
74
|
+
ext_info?: {
|
|
75
|
+
ref_idx?: string;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ============ Media Types ============
|
|
80
|
+
|
|
81
|
+
/** QQ Open Platform media file type codes. */
|
|
82
|
+
export enum MediaFileType {
|
|
83
|
+
IMAGE = 1,
|
|
84
|
+
VIDEO = 2,
|
|
85
|
+
VOICE = 3,
|
|
86
|
+
FILE = 4,
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Media upload response from the QQ Open Platform. */
|
|
90
|
+
export interface UploadMediaResponse {
|
|
91
|
+
file_uuid: string;
|
|
92
|
+
file_info: string;
|
|
93
|
+
ttl: number;
|
|
94
|
+
id?: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Structured metadata recorded for outbound messages. */
|
|
98
|
+
export interface OutboundMeta {
|
|
99
|
+
text?: string;
|
|
100
|
+
mediaType?: "image" | "voice" | "video" | "file";
|
|
101
|
+
mediaUrl?: string;
|
|
102
|
+
mediaLocalPath?: string;
|
|
103
|
+
ttsText?: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ============ API Client Config ============
|
|
107
|
+
|
|
108
|
+
/** Configuration for the core HTTP client. */
|
|
109
|
+
export interface ApiClientConfig {
|
|
110
|
+
baseUrl?: string;
|
|
111
|
+
defaultTimeoutMs?: number;
|
|
112
|
+
fileUploadTimeoutMs?: number;
|
|
113
|
+
logger?: Logger;
|
|
114
|
+
userAgent?: string | (() => string);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ============ Chunked Upload Types ============
|
|
118
|
+
|
|
119
|
+
export interface UploadPart {
|
|
120
|
+
index: number;
|
|
121
|
+
presigned_url: string;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface UploadPrepareResponse {
|
|
125
|
+
upload_id: string;
|
|
126
|
+
block_size: number;
|
|
127
|
+
parts: UploadPart[];
|
|
128
|
+
concurrency?: number;
|
|
129
|
+
retry_timeout?: number;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface UploadPrepareHashes {
|
|
133
|
+
md5: string;
|
|
134
|
+
sha1: string;
|
|
135
|
+
md5_10m: string;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ============ Stream Message Types ============
|
|
139
|
+
|
|
140
|
+
export const StreamInputMode = {
|
|
141
|
+
REPLACE: "replace",
|
|
142
|
+
} as const;
|
|
143
|
+
export type StreamInputMode = (typeof StreamInputMode)[keyof typeof StreamInputMode];
|
|
144
|
+
|
|
145
|
+
export const StreamInputState = {
|
|
146
|
+
GENERATING: 1,
|
|
147
|
+
DONE: 10,
|
|
148
|
+
} as const;
|
|
149
|
+
export type StreamInputState = (typeof StreamInputState)[keyof typeof StreamInputState];
|
|
150
|
+
|
|
151
|
+
export const StreamContentType = {
|
|
152
|
+
MARKDOWN: "markdown",
|
|
153
|
+
} as const;
|
|
154
|
+
export type StreamContentType = (typeof StreamContentType)[keyof typeof StreamContentType];
|
|
155
|
+
|
|
156
|
+
export interface StreamMessageRequest {
|
|
157
|
+
input_mode: StreamInputMode;
|
|
158
|
+
input_state: StreamInputState;
|
|
159
|
+
content_type: StreamContentType;
|
|
160
|
+
content_raw: string;
|
|
161
|
+
event_id: string;
|
|
162
|
+
msg_id: string;
|
|
163
|
+
stream_msg_id?: string;
|
|
164
|
+
msg_seq: number;
|
|
165
|
+
index: number;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ============ Inline Keyboard ============
|
|
169
|
+
|
|
170
|
+
export interface KeyboardButton {
|
|
171
|
+
id: string;
|
|
172
|
+
render_data: {
|
|
173
|
+
label: string;
|
|
174
|
+
visited_label: string;
|
|
175
|
+
style: number;
|
|
176
|
+
};
|
|
177
|
+
action: {
|
|
178
|
+
type: number;
|
|
179
|
+
permission: { type: number };
|
|
180
|
+
data: string;
|
|
181
|
+
click_limit?: number;
|
|
182
|
+
};
|
|
183
|
+
group_id?: string;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export interface InlineKeyboard {
|
|
187
|
+
content: {
|
|
188
|
+
rows: Array<{ buttons: KeyboardButton[] }>;
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ============ Interaction Event ============
|
|
193
|
+
|
|
194
|
+
export interface InteractionEvent {
|
|
195
|
+
id: string;
|
|
196
|
+
type: number;
|
|
197
|
+
scene?: string;
|
|
198
|
+
chat_type?: number;
|
|
199
|
+
timestamp?: string;
|
|
200
|
+
guild_id?: string;
|
|
201
|
+
channel_id?: string;
|
|
202
|
+
user_openid?: string;
|
|
203
|
+
group_openid?: string;
|
|
204
|
+
group_member_openid?: string;
|
|
205
|
+
version: number;
|
|
206
|
+
data: {
|
|
207
|
+
type: number;
|
|
208
|
+
resolved: {
|
|
209
|
+
button_data?: string;
|
|
210
|
+
button_id?: string;
|
|
211
|
+
user_id?: string;
|
|
212
|
+
feature_id?: string;
|
|
213
|
+
message_id?: string;
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ============ Gateway WebSocket Types ============
|
|
219
|
+
|
|
220
|
+
export interface WSPayload {
|
|
221
|
+
op: number;
|
|
222
|
+
d: unknown;
|
|
223
|
+
s?: number;
|
|
224
|
+
t?: string;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export interface RawMessageAttachment {
|
|
228
|
+
content_type: string;
|
|
229
|
+
url: string;
|
|
230
|
+
filename?: string;
|
|
231
|
+
voice_wav_url?: string;
|
|
232
|
+
asr_refer_text?: string;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export interface RawMsgElement {
|
|
236
|
+
msg_idx?: string;
|
|
237
|
+
content?: string;
|
|
238
|
+
attachments?: Array<
|
|
239
|
+
RawMessageAttachment & {
|
|
240
|
+
height?: number;
|
|
241
|
+
width?: number;
|
|
242
|
+
size?: number;
|
|
243
|
+
}
|
|
244
|
+
>;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export interface C2CMessageEvent {
|
|
248
|
+
id: string;
|
|
249
|
+
content: string;
|
|
250
|
+
timestamp: string;
|
|
251
|
+
author: { user_openid: string };
|
|
252
|
+
attachments?: RawMessageAttachment[];
|
|
253
|
+
message_scene?: { ext?: string[] };
|
|
254
|
+
message_type?: number;
|
|
255
|
+
msg_elements?: RawMsgElement[];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export interface GuildMessageEvent {
|
|
259
|
+
id: string;
|
|
260
|
+
content: string;
|
|
261
|
+
timestamp: string;
|
|
262
|
+
author: { id: string; username?: string };
|
|
263
|
+
channel_id: string;
|
|
264
|
+
guild_id: string;
|
|
265
|
+
attachments?: RawMessageAttachment[];
|
|
266
|
+
message_scene?: { ext?: string[] };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export interface GroupMessageEvent {
|
|
270
|
+
id: string;
|
|
271
|
+
content: string;
|
|
272
|
+
timestamp: string;
|
|
273
|
+
author: {
|
|
274
|
+
member_openid: string;
|
|
275
|
+
username?: string;
|
|
276
|
+
bot?: boolean;
|
|
277
|
+
};
|
|
278
|
+
group_openid: string;
|
|
279
|
+
attachments?: RawMessageAttachment[];
|
|
280
|
+
mentions?: Array<{
|
|
281
|
+
scope?: "all" | "single";
|
|
282
|
+
id?: string;
|
|
283
|
+
user_openid?: string;
|
|
284
|
+
member_openid?: string;
|
|
285
|
+
nickname?: string;
|
|
286
|
+
username?: string;
|
|
287
|
+
bot?: boolean;
|
|
288
|
+
is_you?: boolean;
|
|
289
|
+
}>;
|
|
290
|
+
message_scene?: { source?: string; ext?: string[] };
|
|
291
|
+
message_type?: number;
|
|
292
|
+
msg_elements?: RawMsgElement[];
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// ============ Account ============
|
|
296
|
+
|
|
297
|
+
/** Lightweight account record consumed by GatewayConnection. */
|
|
298
|
+
export interface GatewayAccount {
|
|
299
|
+
/** Stable account identifier (used for logging / session persistence). */
|
|
300
|
+
accountId: string;
|
|
301
|
+
appId: string;
|
|
302
|
+
clientSecret: string;
|
|
303
|
+
/** Whether this bot has markdown permission. */
|
|
304
|
+
markdownSupport?: boolean;
|
|
305
|
+
}
|