@openclaw/qqbot 2026.5.2-beta.2 → 2026.5.3-beta.1

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.
Files changed (203) hide show
  1. package/dist/api.js +646 -0
  2. package/dist/approval-cg0SVahb.js +94 -0
  3. package/dist/channel-N6Y_Rcjp.js +551 -0
  4. package/dist/channel-plugin-api.js +2 -0
  5. package/dist/channel.setup-ByCRAe5H.js +25 -0
  6. package/dist/config-D6545NkC.js +94 -0
  7. package/dist/config-schema-DFcjQw73.js +309 -0
  8. package/dist/exec-approvals-COUsM6wZ.js +138 -0
  9. package/dist/gateway-CmSUJKSt.js +6726 -0
  10. package/dist/handler-runtime-Bqm6N0WG.js +120 -0
  11. package/dist/index.js +29 -0
  12. package/dist/narrowing-BoieBTIU.js +25 -0
  13. package/dist/outbound-BJfhwrPg.js +1357 -0
  14. package/dist/request-context-DXtpwWui.js +921 -0
  15. package/dist/resolve-D_06fV6-.js +284 -0
  16. package/dist/runtime-BJAS3eXW.js +18 -0
  17. package/dist/runtime-api.js +2 -0
  18. package/dist/secret-contract-api.js +56 -0
  19. package/dist/sender-p-B14eLG.js +1987 -0
  20. package/dist/setup-entry.js +15 -0
  21. package/dist/setup-plugin-api.js +2 -0
  22. package/dist/string-normalize-Ci6NM5DE.js +98 -0
  23. package/dist/target-parser-Y0prnrXD.js +245 -0
  24. package/package.json +15 -6
  25. package/api.ts +0 -56
  26. package/channel-plugin-api.ts +0 -1
  27. package/index.ts +0 -29
  28. package/runtime-api.ts +0 -9
  29. package/setup-entry.ts +0 -9
  30. package/setup-plugin-api.ts +0 -3
  31. package/src/bridge/approval/capability.ts +0 -237
  32. package/src/bridge/approval/handler-runtime.ts +0 -204
  33. package/src/bridge/bootstrap.ts +0 -135
  34. package/src/bridge/channel-entry.ts +0 -18
  35. package/src/bridge/commands/framework-context-adapter.ts +0 -60
  36. package/src/bridge/commands/framework-registration.ts +0 -47
  37. package/src/bridge/commands/from-parser.test.ts +0 -86
  38. package/src/bridge/commands/from-parser.ts +0 -60
  39. package/src/bridge/commands/result-dispatcher.ts +0 -76
  40. package/src/bridge/config-shared.ts +0 -132
  41. package/src/bridge/config.ts +0 -111
  42. package/src/bridge/gateway.ts +0 -174
  43. package/src/bridge/logger.ts +0 -31
  44. package/src/bridge/narrowing.ts +0 -31
  45. package/src/bridge/plugin-version.test.ts +0 -146
  46. package/src/bridge/plugin-version.ts +0 -102
  47. package/src/bridge/runtime.ts +0 -25
  48. package/src/bridge/sdk-adapter.ts +0 -131
  49. package/src/bridge/setup/finalize.ts +0 -144
  50. package/src/bridge/setup/surface.ts +0 -34
  51. package/src/bridge/tools/channel.ts +0 -58
  52. package/src/bridge/tools/index.ts +0 -15
  53. package/src/bridge/tools/remind.test.ts +0 -124
  54. package/src/bridge/tools/remind.ts +0 -91
  55. package/src/channel.setup.ts +0 -33
  56. package/src/channel.ts +0 -288
  57. package/src/command-auth.test.ts +0 -62
  58. package/src/config-schema.ts +0 -84
  59. package/src/config.test.ts +0 -364
  60. package/src/engine/access/access-control.test.ts +0 -198
  61. package/src/engine/access/access-control.ts +0 -226
  62. package/src/engine/access/index.ts +0 -16
  63. package/src/engine/access/resolve-policy.test.ts +0 -59
  64. package/src/engine/access/resolve-policy.ts +0 -57
  65. package/src/engine/access/sender-match.test.ts +0 -60
  66. package/src/engine/access/sender-match.ts +0 -55
  67. package/src/engine/access/types.ts +0 -53
  68. package/src/engine/adapter/audio.port.ts +0 -27
  69. package/src/engine/adapter/commands.port.ts +0 -22
  70. package/src/engine/adapter/history.port.ts +0 -52
  71. package/src/engine/adapter/index.ts +0 -139
  72. package/src/engine/adapter/mention-gate.port.ts +0 -50
  73. package/src/engine/adapter/types.ts +0 -38
  74. package/src/engine/api/api-client.ts +0 -212
  75. package/src/engine/api/media-chunked.test.ts +0 -336
  76. package/src/engine/api/media-chunked.ts +0 -622
  77. package/src/engine/api/media.ts +0 -218
  78. package/src/engine/api/messages.ts +0 -293
  79. package/src/engine/api/retry.ts +0 -217
  80. package/src/engine/api/routes.ts +0 -95
  81. package/src/engine/api/token.ts +0 -271
  82. package/src/engine/approval/index.test.ts +0 -22
  83. package/src/engine/approval/index.ts +0 -224
  84. package/src/engine/commands/builtin/log-helpers.ts +0 -319
  85. package/src/engine/commands/builtin/register-all.ts +0 -17
  86. package/src/engine/commands/builtin/register-approve.ts +0 -201
  87. package/src/engine/commands/builtin/register-basic.ts +0 -95
  88. package/src/engine/commands/builtin/register-clear-storage.ts +0 -187
  89. package/src/engine/commands/builtin/register-logs.ts +0 -20
  90. package/src/engine/commands/builtin/register-streaming.ts +0 -137
  91. package/src/engine/commands/builtin/state.ts +0 -31
  92. package/src/engine/commands/slash-command-auth.ts +0 -48
  93. package/src/engine/commands/slash-command-handler.ts +0 -146
  94. package/src/engine/commands/slash-commands-impl.test.ts +0 -8
  95. package/src/engine/commands/slash-commands-impl.ts +0 -61
  96. package/src/engine/commands/slash-commands.ts +0 -199
  97. package/src/engine/config/credential-backup.test.ts +0 -88
  98. package/src/engine/config/credential-backup.ts +0 -107
  99. package/src/engine/config/credentials.ts +0 -76
  100. package/src/engine/config/group.test.ts +0 -234
  101. package/src/engine/config/group.ts +0 -299
  102. package/src/engine/config/resolve.test.ts +0 -152
  103. package/src/engine/config/resolve.ts +0 -283
  104. package/src/engine/config/setup-logic.ts +0 -84
  105. package/src/engine/engine-import-boundary.test.ts +0 -73
  106. package/src/engine/gateway/codec.ts +0 -47
  107. package/src/engine/gateway/constants.ts +0 -117
  108. package/src/engine/gateway/event-dispatcher.ts +0 -177
  109. package/src/engine/gateway/gateway-connection.ts +0 -371
  110. package/src/engine/gateway/gateway.ts +0 -291
  111. package/src/engine/gateway/inbound-attachments.test.ts +0 -126
  112. package/src/engine/gateway/inbound-attachments.ts +0 -360
  113. package/src/engine/gateway/inbound-context.ts +0 -195
  114. package/src/engine/gateway/inbound-pipeline.self-echo.test.ts +0 -218
  115. package/src/engine/gateway/inbound-pipeline.ts +0 -235
  116. package/src/engine/gateway/interaction-handler.ts +0 -220
  117. package/src/engine/gateway/message-queue.test.ts +0 -282
  118. package/src/engine/gateway/message-queue.ts +0 -499
  119. package/src/engine/gateway/outbound-dispatch.test.ts +0 -231
  120. package/src/engine/gateway/outbound-dispatch.ts +0 -575
  121. package/src/engine/gateway/reconnect.ts +0 -199
  122. package/src/engine/gateway/stages/access-stage.ts +0 -132
  123. package/src/engine/gateway/stages/assembly-stage.ts +0 -156
  124. package/src/engine/gateway/stages/content-stage.test.ts +0 -77
  125. package/src/engine/gateway/stages/content-stage.ts +0 -77
  126. package/src/engine/gateway/stages/envelope-stage.test.ts +0 -152
  127. package/src/engine/gateway/stages/envelope-stage.ts +0 -144
  128. package/src/engine/gateway/stages/group-gate-stage.ts +0 -292
  129. package/src/engine/gateway/stages/index.ts +0 -18
  130. package/src/engine/gateway/stages/quote-stage.ts +0 -113
  131. package/src/engine/gateway/stages/refidx-stage.ts +0 -62
  132. package/src/engine/gateway/stages/stub-contexts.ts +0 -116
  133. package/src/engine/gateway/types.ts +0 -264
  134. package/src/engine/gateway/typing-keepalive.ts +0 -79
  135. package/src/engine/group/activation.test.ts +0 -114
  136. package/src/engine/group/activation.ts +0 -147
  137. package/src/engine/group/history.test.ts +0 -314
  138. package/src/engine/group/history.ts +0 -321
  139. package/src/engine/group/mention.test.ts +0 -141
  140. package/src/engine/group/mention.ts +0 -197
  141. package/src/engine/group/message-gating.test.ts +0 -188
  142. package/src/engine/group/message-gating.ts +0 -216
  143. package/src/engine/messaging/decode-media-path.ts +0 -82
  144. package/src/engine/messaging/media-source.ts +0 -215
  145. package/src/engine/messaging/media-type-detect.ts +0 -37
  146. package/src/engine/messaging/outbound-audio-port.ts +0 -38
  147. package/src/engine/messaging/outbound-deliver.ts +0 -810
  148. package/src/engine/messaging/outbound-media-send.ts +0 -702
  149. package/src/engine/messaging/outbound-reply.ts +0 -27
  150. package/src/engine/messaging/outbound-result-helpers.ts +0 -54
  151. package/src/engine/messaging/outbound-types.ts +0 -45
  152. package/src/engine/messaging/outbound.ts +0 -485
  153. package/src/engine/messaging/reply-dispatcher.ts +0 -597
  154. package/src/engine/messaging/reply-limiter.ts +0 -164
  155. package/src/engine/messaging/sender.ts +0 -729
  156. package/src/engine/messaging/streaming-c2c.ts +0 -1192
  157. package/src/engine/messaging/streaming-media-send.ts +0 -544
  158. package/src/engine/messaging/target-parser.ts +0 -104
  159. package/src/engine/ref/format-message-ref.ts +0 -142
  160. package/src/engine/ref/format-ref-entry.test.ts +0 -60
  161. package/src/engine/ref/format-ref-entry.ts +0 -27
  162. package/src/engine/ref/store.ts +0 -208
  163. package/src/engine/ref/types.ts +0 -27
  164. package/src/engine/session/known-users.ts +0 -137
  165. package/src/engine/session/session-store.ts +0 -204
  166. package/src/engine/tools/channel-api.ts +0 -244
  167. package/src/engine/tools/remind-logic.test.ts +0 -280
  168. package/src/engine/tools/remind-logic.ts +0 -377
  169. package/src/engine/types.ts +0 -313
  170. package/src/engine/utils/attachment-tags.test.ts +0 -186
  171. package/src/engine/utils/attachment-tags.ts +0 -174
  172. package/src/engine/utils/audio.test.ts +0 -250
  173. package/src/engine/utils/audio.ts +0 -585
  174. package/src/engine/utils/data-paths.ts +0 -38
  175. package/src/engine/utils/diagnostics.ts +0 -109
  176. package/src/engine/utils/file-utils.test.ts +0 -72
  177. package/src/engine/utils/file-utils.ts +0 -225
  178. package/src/engine/utils/format.test.ts +0 -68
  179. package/src/engine/utils/format.ts +0 -70
  180. package/src/engine/utils/image-size.test.ts +0 -158
  181. package/src/engine/utils/image-size.ts +0 -249
  182. package/src/engine/utils/log.test.ts +0 -28
  183. package/src/engine/utils/log.ts +0 -61
  184. package/src/engine/utils/media-tags.test.ts +0 -32
  185. package/src/engine/utils/media-tags.ts +0 -177
  186. package/src/engine/utils/payload.test.ts +0 -68
  187. package/src/engine/utils/payload.ts +0 -145
  188. package/src/engine/utils/platform-storage-laziness.test.ts +0 -63
  189. package/src/engine/utils/platform.test.ts +0 -148
  190. package/src/engine/utils/platform.ts +0 -343
  191. package/src/engine/utils/request-context.ts +0 -60
  192. package/src/engine/utils/string-normalize.ts +0 -91
  193. package/src/engine/utils/stt.test.ts +0 -104
  194. package/src/engine/utils/stt.ts +0 -100
  195. package/src/engine/utils/text-parsing.test.ts +0 -29
  196. package/src/engine/utils/text-parsing.ts +0 -155
  197. package/src/engine/utils/upload-cache.ts +0 -96
  198. package/src/engine/utils/voice-text.ts +0 -15
  199. package/src/exec-approvals.ts +0 -218
  200. package/src/manifest-schema.test.ts +0 -56
  201. package/src/qqbot-test-support.ts +0 -29
  202. package/src/types.ts +0 -210
  203. package/tsconfig.json +0 -16
@@ -1,218 +0,0 @@
1
- /**
2
- * Media upload API for the QQ Open Platform (small-file direct upload).
3
- *
4
- * Key improvements:
5
- * - Unified `uploadMedia(scope, ...)` replaces `uploadC2CMedia` + `uploadGroupMedia`.
6
- * - Upload cache integration via composition (passed in constructor).
7
- * - Uses `withRetry` from the shared retry engine.
8
- *
9
- * Chunked upload for files above `LARGE_FILE_THRESHOLD` is tracked by
10
- * {@link ./media-chunked.ts}; this module currently handles only the
11
- * one-shot path.
12
- */
13
-
14
- import * as fs from "node:fs";
15
- import {
16
- MediaFileType,
17
- type ChatScope,
18
- type UploadMediaResponse,
19
- type MessageResponse,
20
- type EngineLogger,
21
- } from "../types.js";
22
- import { ApiClient } from "./api-client.js";
23
- import { withRetry, UPLOAD_RETRY_POLICY } from "./retry.js";
24
- import { mediaUploadPath, messagePath, getNextMsgSeq } from "./routes.js";
25
- import { TokenManager } from "./token.js";
26
-
27
- /** Upload cache interface — the caller provides the implementation. */
28
- export interface UploadCacheAdapter {
29
- computeHash: (data: string) => string;
30
- get: (hash: string, scope: string, targetId: string, fileType: number) => string | null;
31
- set: (
32
- hash: string,
33
- scope: string,
34
- targetId: string,
35
- fileType: number,
36
- fileInfo: string,
37
- fileUuid: string,
38
- ttl: number,
39
- ) => void;
40
- }
41
-
42
- /** File name sanitizer — injected to avoid importing platform-specific utils. */
43
- export type SanitizeFileNameFn = (name: string) => string;
44
-
45
- interface MediaApiConfig {
46
- logger?: EngineLogger;
47
- /** Upload cache adapter (optional, omit to disable caching). */
48
- uploadCache?: UploadCacheAdapter;
49
- /** File name sanitizer. */
50
- sanitizeFileName?: SanitizeFileNameFn;
51
- }
52
-
53
- /**
54
- * Small-file media upload module.
55
- *
56
- * Handles base64 and URL-based uploads with optional caching and retry.
57
- */
58
- export class MediaApi {
59
- private readonly client: ApiClient;
60
- private readonly tokenManager: TokenManager;
61
- private readonly logger?: EngineLogger;
62
- private readonly cache?: UploadCacheAdapter;
63
- private readonly sanitize: SanitizeFileNameFn;
64
-
65
- constructor(client: ApiClient, tokenManager: TokenManager, config: MediaApiConfig = {}) {
66
- this.client = client;
67
- this.tokenManager = tokenManager;
68
- this.logger = config.logger;
69
- this.cache = config.uploadCache;
70
- this.sanitize = config.sanitizeFileName ?? ((n) => n);
71
- }
72
-
73
- /**
74
- * Upload media via base64, URL, buffer, or local file path to a C2C or Group target.
75
- *
76
- * The `localPath` and `buffer` branches are equivalent to `fileData` for the
77
- * current one-shot implementation — the file is read and base64-encoded
78
- * synchronously. They exist as first-class inputs so that a future chunked
79
- * upload implementation can consume them without interface churn.
80
- *
81
- * @param scope - `'c2c'` or `'group'`.
82
- * @param targetId - User openid or group openid.
83
- * @param fileType - Media file type code.
84
- * @param creds - Authentication credentials.
85
- * @param opts - Upload options. Exactly one of `url`/`fileData`/`buffer`/`localPath`
86
- * must be supplied.
87
- * @returns Upload result containing `file_info` for subsequent message sends.
88
- */
89
- async uploadMedia(
90
- scope: ChatScope,
91
- targetId: string,
92
- fileType: MediaFileType,
93
- creds: { appId: string; clientSecret: string },
94
- opts: {
95
- url?: string;
96
- fileData?: string;
97
- /**
98
- * Raw bytes in memory. Currently re-encoded to base64 internally;
99
- * reserved as a dedicated input for the future chunked uploader.
100
- */
101
- buffer?: Buffer;
102
- /**
103
- * On-disk path. Currently read + base64-encoded internally; reserved
104
- * for streaming ingestion by the future chunked uploader.
105
- */
106
- localPath?: string;
107
- srvSendMsg?: boolean;
108
- fileName?: string;
109
- },
110
- ): Promise<UploadMediaResponse> {
111
- const sources = [opts.url, opts.fileData, opts.buffer, opts.localPath].filter(
112
- (v) => v !== undefined,
113
- );
114
- if (sources.length === 0) {
115
- throw new Error(`uploadMedia: one of url/fileData/buffer/localPath is required`);
116
- }
117
- if (sources.length > 1) {
118
- throw new Error(
119
- `uploadMedia: url/fileData/buffer/localPath are mutually exclusive (got ${sources.length})`,
120
- );
121
- }
122
-
123
- // One-shot path: materialize buffer/localPath into fileData.
124
- // Future chunked-upload work will branch here on size and route
125
- // buffer/localPath through streaming ingestion instead of base64 encoding.
126
- let fileData = opts.fileData;
127
- if (opts.buffer) {
128
- fileData = opts.buffer.toString("base64");
129
- } else if (opts.localPath) {
130
- const buf = await fs.promises.readFile(opts.localPath);
131
- fileData = buf.toString("base64");
132
- }
133
-
134
- // Check cache for base64 uploads.
135
- if (fileData && this.cache) {
136
- const hash = this.cache.computeHash(fileData);
137
- const cached = this.cache.get(hash, scope, targetId, fileType);
138
- if (cached) {
139
- return { file_uuid: "", file_info: cached, ttl: 0 };
140
- }
141
- }
142
-
143
- const body: Record<string, unknown> = {
144
- file_type: fileType,
145
- srv_send_msg: opts.srvSendMsg ?? false,
146
- };
147
- if (opts.url) {
148
- body.url = opts.url;
149
- } else if (fileData) {
150
- body.file_data = fileData;
151
- }
152
- if (fileType === MediaFileType.FILE && opts.fileName) {
153
- body.file_name = this.sanitize(opts.fileName);
154
- }
155
-
156
- const token = await this.tokenManager.getAccessToken(creds.appId, creds.clientSecret);
157
- const path = mediaUploadPath(scope, targetId);
158
-
159
- const result = await withRetry(
160
- () =>
161
- this.client.request<UploadMediaResponse>(token, "POST", path, body, {
162
- redactBodyKeys: ["file_data"],
163
- uploadRequest: true,
164
- }),
165
- UPLOAD_RETRY_POLICY,
166
- undefined,
167
- this.logger,
168
- );
169
-
170
- // Cache the result for future dedup.
171
- if (fileData && result.file_info && result.ttl > 0 && this.cache) {
172
- const hash = this.cache.computeHash(fileData);
173
- this.cache.set(
174
- hash,
175
- scope,
176
- targetId,
177
- fileType,
178
- result.file_info,
179
- result.file_uuid,
180
- result.ttl,
181
- );
182
- }
183
-
184
- return result;
185
- }
186
-
187
- /**
188
- * Send a media message (upload result → message) to a C2C or Group target.
189
- *
190
- * @param scope - `'c2c'` or `'group'`.
191
- * @param targetId - User openid or group openid.
192
- * @param fileInfo - `file_info` from a prior upload.
193
- * @param creds - Authentication credentials.
194
- * @param opts - Message options.
195
- */
196
- async sendMediaMessage(
197
- scope: ChatScope,
198
- targetId: string,
199
- fileInfo: string,
200
- creds: { appId: string; clientSecret: string },
201
- opts?: {
202
- msgId?: string;
203
- content?: string;
204
- },
205
- ): Promise<MessageResponse> {
206
- const token = await this.tokenManager.getAccessToken(creds.appId, creds.clientSecret);
207
- const msgSeq = opts?.msgId ? getNextMsgSeq(opts.msgId) : 1;
208
- const path = messagePath(scope, targetId);
209
-
210
- return this.client.request<MessageResponse>(token, "POST", path, {
211
- msg_type: 7,
212
- media: { file_info: fileInfo },
213
- msg_seq: msgSeq,
214
- ...(opts?.content ? { content: opts.content } : {}),
215
- ...(opts?.msgId ? { msg_id: opts.msgId } : {}),
216
- });
217
- }
218
- }
@@ -1,293 +0,0 @@
1
- /**
2
- * Message sending API for the QQ Open Platform.
3
- *
4
- * Key design improvements:
5
- * - Unified `sendMessage(scope, ...)` replaces `sendC2CMessage` + `sendGroupMessage`.
6
- * - `onMessageSent` hook is scoped to the instance, not a module-level global.
7
- * - Markdown support flag is per-instance, not a global Map.
8
- */
9
-
10
- import type {
11
- ChatScope,
12
- MessageResponse,
13
- OutboundMeta,
14
- EngineLogger,
15
- InlineKeyboard,
16
- StreamMessageRequest,
17
- } from "../types.js";
18
- import { formatErrorMessage } from "../utils/format.js";
19
- import { ApiClient } from "./api-client.js";
20
- import {
21
- messagePath,
22
- channelMessagePath,
23
- dmMessagePath,
24
- gatewayPath,
25
- interactionPath,
26
- getNextMsgSeq,
27
- streamMessagePath,
28
- } from "./routes.js";
29
- import { TokenManager } from "./token.js";
30
-
31
- interface MessageApiConfig {
32
- /** Whether the QQ Bot has markdown permission. */
33
- markdownSupport: boolean;
34
- /** Logger for diagnostics. */
35
- logger?: EngineLogger;
36
- }
37
-
38
- type OnMessageSentCallback = (refIdx: string, meta: OutboundMeta) => void;
39
-
40
- /**
41
- * Message sending module.
42
- *
43
- * Usage:
44
- * ```ts
45
- * const api = new MessageApi(client, tokenMgr, { markdownSupport: true });
46
- * await api.sendMessage('c2c', openid, 'Hello!', { appId, clientSecret, msgId });
47
- * ```
48
- */
49
- export class MessageApi {
50
- private readonly client: ApiClient;
51
- private readonly tokenManager: TokenManager;
52
- private readonly markdownSupport: boolean;
53
- private readonly logger?: EngineLogger;
54
- private messageSentHook: OnMessageSentCallback | null = null;
55
-
56
- constructor(client: ApiClient, tokenManager: TokenManager, config: MessageApiConfig) {
57
- this.client = client;
58
- this.tokenManager = tokenManager;
59
- this.markdownSupport = config.markdownSupport;
60
- this.logger = config.logger;
61
- }
62
-
63
- /** Register a callback invoked when a sent message returns a ref_idx. */
64
- onMessageSent(callback: OnMessageSentCallback): void {
65
- this.messageSentHook = callback;
66
- }
67
-
68
- /**
69
- * Notify the registered hook about a sent message.
70
- * Use this for media sends that bypass `sendAndNotify`.
71
- */
72
- notifyMessageSent(refIdx: string, meta: OutboundMeta): void {
73
- if (this.messageSentHook) {
74
- try {
75
- this.messageSentHook(refIdx, meta);
76
- } catch (err) {
77
- this.logger?.error?.(
78
- `[qqbot:messages] onMessageSent hook error: ${formatErrorMessage(err)}`,
79
- );
80
- }
81
- }
82
- }
83
-
84
- // ---- Unified message sending ----
85
-
86
- /**
87
- * Send a text message to a C2C or Group target.
88
- *
89
- * Automatically constructs the correct path, body format (markdown vs plain),
90
- * and message sequence number.
91
- */
92
- async sendMessage(
93
- scope: ChatScope,
94
- targetId: string,
95
- content: string,
96
- creds: Credentials,
97
- opts?: {
98
- msgId?: string;
99
- messageReference?: string;
100
- inlineKeyboard?: InlineKeyboard;
101
- },
102
- ): Promise<MessageResponse> {
103
- const token = await this.tokenManager.getAccessToken(creds.appId, creds.clientSecret);
104
- const msgSeq = opts?.msgId ? getNextMsgSeq(opts.msgId) : 1;
105
- const body = this.buildMessageBody(
106
- content,
107
- opts?.msgId,
108
- msgSeq,
109
- opts?.messageReference,
110
- opts?.inlineKeyboard,
111
- );
112
- const path = messagePath(scope, targetId);
113
- return this.sendAndNotify(creds.appId, token, "POST", path, body, { text: content });
114
- }
115
-
116
- /** Send a proactive (no msgId) message to a C2C or Group target. */
117
- async sendProactiveMessage(
118
- scope: ChatScope,
119
- targetId: string,
120
- content: string,
121
- creds: Credentials,
122
- ): Promise<MessageResponse> {
123
- if (!content?.trim()) {
124
- throw new Error("Proactive message content must not be empty");
125
- }
126
- const token = await this.tokenManager.getAccessToken(creds.appId, creds.clientSecret);
127
- const body = this.buildProactiveBody(content);
128
- const path = messagePath(scope, targetId);
129
- return this.sendAndNotify(creds.appId, token, "POST", path, body, { text: content });
130
- }
131
-
132
- // ---- Channel / DM ----
133
-
134
- /** Send a channel message. */
135
- async sendChannelMessage(opts: {
136
- channelId: string;
137
- content: string;
138
- creds: Credentials;
139
- msgId?: string;
140
- }): Promise<MessageResponse> {
141
- const token = await this.tokenManager.getAccessToken(opts.creds.appId, opts.creds.clientSecret);
142
- return this.client.request<MessageResponse>(token, "POST", channelMessagePath(opts.channelId), {
143
- content: opts.content,
144
- ...(opts.msgId ? { msg_id: opts.msgId } : {}),
145
- });
146
- }
147
-
148
- /** Send a DM (guild direct message). */
149
- async sendDmMessage(opts: {
150
- guildId: string;
151
- content: string;
152
- creds: Credentials;
153
- msgId?: string;
154
- }): Promise<MessageResponse> {
155
- const token = await this.tokenManager.getAccessToken(opts.creds.appId, opts.creds.clientSecret);
156
- return this.client.request<MessageResponse>(token, "POST", dmMessagePath(opts.guildId), {
157
- content: opts.content,
158
- ...(opts.msgId ? { msg_id: opts.msgId } : {}),
159
- });
160
- }
161
-
162
- // ---- C2C Input Notify ----
163
-
164
- /** Send a typing indicator to a C2C user. */
165
- async sendInputNotify(opts: {
166
- openid: string;
167
- creds: Credentials;
168
- msgId?: string;
169
- inputSecond?: number;
170
- }): Promise<{ refIdx?: string }> {
171
- const inputSecond = opts.inputSecond ?? 60;
172
- const token = await this.tokenManager.getAccessToken(opts.creds.appId, opts.creds.clientSecret);
173
- const msgSeq = opts.msgId ? getNextMsgSeq(opts.msgId) : 1;
174
- const response = await this.client.request<{ ext_info?: { ref_idx?: string } }>(
175
- token,
176
- "POST",
177
- messagePath("c2c", opts.openid),
178
- {
179
- msg_type: 6,
180
- input_notify: { input_type: 1, input_second: inputSecond },
181
- msg_seq: msgSeq,
182
- ...(opts.msgId ? { msg_id: opts.msgId } : {}),
183
- },
184
- );
185
- return { refIdx: response.ext_info?.ref_idx };
186
- }
187
-
188
- // ---- Interaction ----
189
-
190
- /** Acknowledge an INTERACTION_CREATE event. */
191
- async acknowledgeInteraction(
192
- interactionId: string,
193
- creds: Credentials,
194
- code: 0 | 1 | 2 | 3 | 4 | 5 = 0,
195
- ): Promise<void> {
196
- const token = await this.tokenManager.getAccessToken(creds.appId, creds.clientSecret);
197
- await this.client.request(token, "PUT", interactionPath(interactionId), { code });
198
- }
199
-
200
- // ---- Gateway ----
201
-
202
- /** Get the WebSocket gateway URL. */
203
- async getGatewayUrl(creds: Credentials): Promise<string> {
204
- const token = await this.tokenManager.getAccessToken(creds.appId, creds.clientSecret);
205
- const data = await this.client.request<{ url: string }>(token, "GET", gatewayPath());
206
- return data.url;
207
- }
208
-
209
- /**
210
- * Send a C2C stream message chunk (`/v2/users/{openid}/stream_messages`).
211
- * Only supported for one-to-one chats.
212
- */
213
- async sendC2CStreamMessage(
214
- creds: Credentials,
215
- openid: string,
216
- req: StreamMessageRequest,
217
- ): Promise<MessageResponse> {
218
- const token = await this.tokenManager.getAccessToken(creds.appId, creds.clientSecret);
219
- const path = streamMessagePath(openid);
220
- const body: Record<string, unknown> = {
221
- input_mode: req.input_mode,
222
- input_state: req.input_state,
223
- content_type: req.content_type,
224
- content_raw: req.content_raw,
225
- event_id: req.event_id,
226
- msg_id: req.msg_id,
227
- msg_seq: req.msg_seq,
228
- index: req.index,
229
- };
230
- if (req.stream_msg_id) {
231
- body.stream_msg_id = req.stream_msg_id;
232
- }
233
- return this.client.request<MessageResponse>(token, "POST", path, body);
234
- }
235
-
236
- // ---- Internal ----
237
-
238
- private async sendAndNotify(
239
- appId: string,
240
- accessToken: string,
241
- method: string,
242
- path: string,
243
- body: unknown,
244
- meta: OutboundMeta,
245
- ): Promise<MessageResponse> {
246
- const result = await this.client.request<MessageResponse>(accessToken, method, path, body);
247
- if (result.ext_info?.ref_idx && this.messageSentHook) {
248
- try {
249
- this.messageSentHook(result.ext_info.ref_idx, meta);
250
- } catch (err) {
251
- this.logger?.error?.(
252
- `[qqbot:messages] onMessageSent hook error: ${formatErrorMessage(err)}`,
253
- );
254
- }
255
- }
256
- return result;
257
- }
258
-
259
- private buildMessageBody(
260
- content: string,
261
- msgId: string | undefined,
262
- msgSeq: number,
263
- messageReference?: string,
264
- inlineKeyboard?: InlineKeyboard,
265
- ): Record<string, unknown> {
266
- const body: Record<string, unknown> = this.markdownSupport
267
- ? { markdown: { content }, msg_type: 2, msg_seq: msgSeq }
268
- : { content, msg_type: 0, msg_seq: msgSeq };
269
-
270
- if (msgId) {
271
- body.msg_id = msgId;
272
- }
273
- if (messageReference && !this.markdownSupport) {
274
- body.message_reference = { message_id: messageReference };
275
- }
276
- if (inlineKeyboard) {
277
- body.keyboard = inlineKeyboard;
278
- }
279
- return body;
280
- }
281
-
282
- private buildProactiveBody(content: string): Record<string, unknown> {
283
- return this.markdownSupport ? { markdown: { content }, msg_type: 2 } : { content, msg_type: 0 };
284
- }
285
- }
286
-
287
- // ---- Shared helpers ----
288
-
289
- /** Credentials needed to authenticate API requests. */
290
- export interface Credentials {
291
- appId: string;
292
- clientSecret: string;
293
- }