@nextclaw/channel-plugin-feishu 0.2.29-beta.0 → 0.2.29-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.
- package/dist/index.d.ts +23 -0
- package/dist/index.js +45 -0
- package/dist/src/accounts.js +141 -0
- package/dist/src/app-scope-checker.js +36 -0
- package/dist/src/async.js +34 -0
- package/dist/src/auth-errors.js +72 -0
- package/dist/src/bitable.js +495 -0
- package/dist/src/bot.d.ts +35 -0
- package/dist/src/bot.js +941 -0
- package/dist/src/calendar-calendar.js +54 -0
- package/dist/src/calendar-event-attendee.js +98 -0
- package/dist/src/calendar-event.js +193 -0
- package/dist/src/calendar-freebusy.js +40 -0
- package/dist/src/calendar-shared.js +23 -0
- package/dist/src/calendar.js +16 -0
- package/dist/src/card-action.js +49 -0
- package/dist/src/channel.d.ts +7 -0
- package/dist/src/channel.js +413 -0
- package/dist/src/chat-schema.js +25 -0
- package/dist/src/chat.js +87 -0
- package/dist/src/client.d.ts +16 -0
- package/dist/src/client.js +112 -0
- package/dist/src/config-schema.d.ts +357 -0
- package/dist/src/dedup.js +126 -0
- package/dist/src/device-flow.js +109 -0
- package/dist/src/directory.js +101 -0
- package/dist/src/doc-schema.js +148 -0
- package/dist/src/docx-batch-insert.js +104 -0
- package/dist/src/docx-color-text.js +80 -0
- package/dist/src/docx-table-ops.js +197 -0
- package/dist/src/docx.js +858 -0
- package/dist/src/domains.js +14 -0
- package/dist/src/drive-schema.js +41 -0
- package/dist/src/drive.js +126 -0
- package/dist/src/dynamic-agent.js +93 -0
- package/dist/src/external-keys.js +13 -0
- package/dist/src/feishu-fetch.js +12 -0
- package/dist/src/identity.js +92 -0
- package/dist/src/lark-ticket.js +11 -0
- package/dist/src/media.d.ts +75 -0
- package/dist/src/media.js +304 -0
- package/dist/src/mention.d.ts +52 -0
- package/dist/src/mention.js +82 -0
- package/dist/src/monitor.account.d.ts +1 -0
- package/dist/src/monitor.account.js +393 -0
- package/dist/src/monitor.d.ts +11 -0
- package/dist/src/monitor.js +58 -0
- package/dist/src/monitor.startup.js +24 -0
- package/dist/src/monitor.state.d.ts +1 -0
- package/dist/src/monitor.state.js +80 -0
- package/dist/src/monitor.transport.js +167 -0
- package/dist/src/nextclaw-sdk/account-id.js +15 -0
- package/dist/src/nextclaw-sdk/core-channel.js +150 -0
- package/dist/src/nextclaw-sdk/core-pairing.js +151 -0
- package/dist/src/nextclaw-sdk/dedupe.js +164 -0
- package/dist/src/nextclaw-sdk/feishu.d.ts +1 -0
- package/dist/src/nextclaw-sdk/feishu.js +14 -0
- package/dist/src/nextclaw-sdk/history.js +69 -0
- package/dist/src/nextclaw-sdk/network-body.js +180 -0
- package/dist/src/nextclaw-sdk/network-fetch.js +63 -0
- package/dist/src/nextclaw-sdk/network-webhook.js +126 -0
- package/dist/src/nextclaw-sdk/network.js +4 -0
- package/dist/src/nextclaw-sdk/runtime-store.js +21 -0
- package/dist/src/nextclaw-sdk/secrets-config.js +65 -0
- package/dist/src/nextclaw-sdk/secrets-core.d.ts +1 -0
- package/dist/src/nextclaw-sdk/secrets-core.js +68 -0
- package/dist/src/nextclaw-sdk/secrets-prompt.js +193 -0
- package/dist/src/nextclaw-sdk/secrets.d.ts +1 -0
- package/dist/src/nextclaw-sdk/secrets.js +4 -0
- package/dist/src/nextclaw-sdk/types.d.ts +242 -0
- package/dist/src/oauth.js +171 -0
- package/dist/src/onboarding.js +381 -0
- package/dist/src/outbound.js +150 -0
- package/dist/src/perm-schema.js +49 -0
- package/dist/src/perm.js +90 -0
- package/dist/src/policy.js +61 -0
- package/dist/src/post.js +160 -0
- package/dist/src/probe.d.ts +11 -0
- package/dist/src/probe.js +85 -0
- package/dist/src/raw-request.js +24 -0
- package/dist/src/reactions.d.ts +67 -0
- package/dist/src/reactions.js +91 -0
- package/dist/src/reply-dispatcher.js +250 -0
- package/dist/src/runtime.js +5 -0
- package/dist/src/secret-input.js +3 -0
- package/dist/src/send-result.js +12 -0
- package/dist/src/send-target.js +22 -0
- package/dist/src/send.d.ts +51 -0
- package/dist/src/send.js +265 -0
- package/dist/src/sheets-shared.js +193 -0
- package/dist/src/sheets.js +95 -0
- package/dist/src/streaming-card.js +263 -0
- package/dist/src/targets.js +39 -0
- package/dist/src/task-comment.js +76 -0
- package/dist/src/task-shared.js +13 -0
- package/dist/src/task-subtask.js +79 -0
- package/dist/src/task-task.js +144 -0
- package/dist/src/task-tasklist.js +136 -0
- package/dist/src/task.js +16 -0
- package/dist/src/token-store.js +154 -0
- package/dist/src/tool-account.js +65 -0
- package/dist/src/tool-result.js +18 -0
- package/dist/src/tool-scopes.js +62 -0
- package/dist/src/tools-config.js +30 -0
- package/dist/src/types.d.ts +43 -0
- package/dist/src/typing.js +145 -0
- package/dist/src/uat-client.js +102 -0
- package/dist/src/user-tool-client.js +132 -0
- package/dist/src/user-tool-helpers.js +110 -0
- package/dist/src/user-tool-result.js +10 -0
- package/dist/src/wiki-schema.js +45 -0
- package/dist/src/wiki.js +144 -0
- package/package.json +8 -4
- package/index.ts +0 -75
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { withTempDownloadPath } from "./nextclaw-sdk/network-fetch.js";
|
|
2
|
+
import "./nextclaw-sdk/feishu.js";
|
|
3
|
+
import { resolveFeishuAccount } from "./accounts.js";
|
|
4
|
+
import { createFeishuClient } from "./client.js";
|
|
5
|
+
import { normalizeFeishuExternalKey } from "./external-keys.js";
|
|
6
|
+
import { getFeishuRuntime } from "./runtime.js";
|
|
7
|
+
import { assertFeishuMessageApiSuccess, toFeishuSendResult } from "./send-result.js";
|
|
8
|
+
import { resolveFeishuSendTarget } from "./send-target.js";
|
|
9
|
+
import fs from "fs";
|
|
10
|
+
import path from "path";
|
|
11
|
+
//#region src/media.ts
|
|
12
|
+
const FEISHU_MEDIA_HTTP_TIMEOUT_MS = 12e4;
|
|
13
|
+
function createConfiguredFeishuMediaClient(params) {
|
|
14
|
+
const account = resolveFeishuAccount({
|
|
15
|
+
cfg: params.cfg,
|
|
16
|
+
accountId: params.accountId
|
|
17
|
+
});
|
|
18
|
+
if (!account.configured) throw new Error(`Feishu account "${account.accountId}" not configured`);
|
|
19
|
+
return {
|
|
20
|
+
account,
|
|
21
|
+
client: createFeishuClient({
|
|
22
|
+
...account,
|
|
23
|
+
httpTimeoutMs: FEISHU_MEDIA_HTTP_TIMEOUT_MS
|
|
24
|
+
})
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function extractFeishuUploadKey(response, params) {
|
|
28
|
+
const responseAny = response;
|
|
29
|
+
if (responseAny.code !== void 0 && responseAny.code !== 0) throw new Error(`${params.errorPrefix}: ${responseAny.msg || `code ${responseAny.code}`}`);
|
|
30
|
+
const key = responseAny[params.key] ?? responseAny.data?.[params.key];
|
|
31
|
+
if (!key) throw new Error(`${params.errorPrefix}: no ${params.key} returned`);
|
|
32
|
+
return key;
|
|
33
|
+
}
|
|
34
|
+
async function readFeishuResponseBuffer(params) {
|
|
35
|
+
const { response } = params;
|
|
36
|
+
const responseAny = response;
|
|
37
|
+
if (responseAny.code !== void 0 && responseAny.code !== 0) throw new Error(`${params.errorPrefix}: ${responseAny.msg || `code ${responseAny.code}`}`);
|
|
38
|
+
if (Buffer.isBuffer(response)) return response;
|
|
39
|
+
if (response instanceof ArrayBuffer) return Buffer.from(response);
|
|
40
|
+
if (responseAny.data && Buffer.isBuffer(responseAny.data)) return responseAny.data;
|
|
41
|
+
if (responseAny.data instanceof ArrayBuffer) return Buffer.from(responseAny.data);
|
|
42
|
+
if (typeof responseAny.getReadableStream === "function") {
|
|
43
|
+
const stream = responseAny.getReadableStream();
|
|
44
|
+
const chunks = [];
|
|
45
|
+
for await (const chunk of stream) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
46
|
+
return Buffer.concat(chunks);
|
|
47
|
+
}
|
|
48
|
+
if (typeof responseAny.writeFile === "function") return await withTempDownloadPath({ prefix: params.tmpDirPrefix }, async (tmpPath) => {
|
|
49
|
+
await responseAny.writeFile(tmpPath);
|
|
50
|
+
return await fs.promises.readFile(tmpPath);
|
|
51
|
+
});
|
|
52
|
+
if (typeof responseAny[Symbol.asyncIterator] === "function") {
|
|
53
|
+
const chunks = [];
|
|
54
|
+
for await (const chunk of responseAny) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
55
|
+
return Buffer.concat(chunks);
|
|
56
|
+
}
|
|
57
|
+
if (typeof responseAny.read === "function") {
|
|
58
|
+
const chunks = [];
|
|
59
|
+
for await (const chunk of responseAny) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
60
|
+
return Buffer.concat(chunks);
|
|
61
|
+
}
|
|
62
|
+
const types = Object.keys(responseAny).map((k) => `${k}: ${typeof responseAny[k]}`).join(", ");
|
|
63
|
+
throw new Error(`${params.errorPrefix}: unexpected response format. Keys: [${types}]`);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Download a message resource (file/image/audio/video) from Feishu.
|
|
67
|
+
* Used for downloading files, audio, and video from messages.
|
|
68
|
+
*/
|
|
69
|
+
async function downloadMessageResourceFeishu(params) {
|
|
70
|
+
const { cfg, messageId, fileKey, type, accountId } = params;
|
|
71
|
+
const normalizedFileKey = normalizeFeishuExternalKey(fileKey);
|
|
72
|
+
if (!normalizedFileKey) throw new Error("Feishu message resource download failed: invalid file_key");
|
|
73
|
+
const { client } = createConfiguredFeishuMediaClient({
|
|
74
|
+
cfg,
|
|
75
|
+
accountId
|
|
76
|
+
});
|
|
77
|
+
return { buffer: await readFeishuResponseBuffer({
|
|
78
|
+
response: await client.im.messageResource.get({
|
|
79
|
+
path: {
|
|
80
|
+
message_id: messageId,
|
|
81
|
+
file_key: normalizedFileKey
|
|
82
|
+
},
|
|
83
|
+
params: { type }
|
|
84
|
+
}),
|
|
85
|
+
tmpDirPrefix: "openclaw-feishu-resource-",
|
|
86
|
+
errorPrefix: "Feishu message resource download failed"
|
|
87
|
+
}) };
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Upload an image to Feishu and get an image_key for sending.
|
|
91
|
+
* Supports: JPEG, PNG, WEBP, GIF, TIFF, BMP, ICO
|
|
92
|
+
*/
|
|
93
|
+
async function uploadImageFeishu(params) {
|
|
94
|
+
const { cfg, image, imageType = "message", accountId } = params;
|
|
95
|
+
const { client } = createConfiguredFeishuMediaClient({
|
|
96
|
+
cfg,
|
|
97
|
+
accountId
|
|
98
|
+
});
|
|
99
|
+
const imageData = typeof image === "string" ? fs.createReadStream(image) : image;
|
|
100
|
+
return { imageKey: extractFeishuUploadKey(await client.im.image.create({ data: {
|
|
101
|
+
image_type: imageType,
|
|
102
|
+
image: imageData
|
|
103
|
+
} }), {
|
|
104
|
+
key: "image_key",
|
|
105
|
+
errorPrefix: "Feishu image upload failed"
|
|
106
|
+
}) };
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Sanitize a filename for safe use in Feishu multipart/form-data uploads.
|
|
110
|
+
* Strips control characters and multipart-injection vectors (CWE-93) while
|
|
111
|
+
* preserving the original UTF-8 display name (Chinese, emoji, etc.).
|
|
112
|
+
*
|
|
113
|
+
* Previous versions percent-encoded non-ASCII characters, but the Feishu
|
|
114
|
+
* `im.file.create` API uses `file_name` as a literal display name — it does
|
|
115
|
+
* NOT decode percent-encoding — so encoded filenames appeared as garbled text
|
|
116
|
+
* in chat (regression in v2026.3.2).
|
|
117
|
+
*/
|
|
118
|
+
function sanitizeFileNameForUpload(fileName) {
|
|
119
|
+
return fileName.replace(/[\x00-\x1F\x7F\r\n"\\]/g, "_");
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Upload a file to Feishu and get a file_key for sending.
|
|
123
|
+
* Max file size: 30MB
|
|
124
|
+
*/
|
|
125
|
+
async function uploadFileFeishu(params) {
|
|
126
|
+
const { cfg, file, fileName, fileType, duration, accountId } = params;
|
|
127
|
+
const { client } = createConfiguredFeishuMediaClient({
|
|
128
|
+
cfg,
|
|
129
|
+
accountId
|
|
130
|
+
});
|
|
131
|
+
const fileData = typeof file === "string" ? fs.createReadStream(file) : file;
|
|
132
|
+
const safeFileName = sanitizeFileNameForUpload(fileName);
|
|
133
|
+
return { fileKey: extractFeishuUploadKey(await client.im.file.create({ data: {
|
|
134
|
+
file_type: fileType,
|
|
135
|
+
file_name: safeFileName,
|
|
136
|
+
file: fileData,
|
|
137
|
+
...duration !== void 0 && { duration }
|
|
138
|
+
} }), {
|
|
139
|
+
key: "file_key",
|
|
140
|
+
errorPrefix: "Feishu file upload failed"
|
|
141
|
+
}) };
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Send an image message using an image_key
|
|
145
|
+
*/
|
|
146
|
+
async function sendImageFeishu(params) {
|
|
147
|
+
const { cfg, to, imageKey, replyToMessageId, replyInThread, accountId } = params;
|
|
148
|
+
const { client, receiveId, receiveIdType } = resolveFeishuSendTarget({
|
|
149
|
+
cfg,
|
|
150
|
+
to,
|
|
151
|
+
accountId
|
|
152
|
+
});
|
|
153
|
+
const content = JSON.stringify({ image_key: imageKey });
|
|
154
|
+
if (replyToMessageId) {
|
|
155
|
+
const response = await client.im.message.reply({
|
|
156
|
+
path: { message_id: replyToMessageId },
|
|
157
|
+
data: {
|
|
158
|
+
content,
|
|
159
|
+
msg_type: "image",
|
|
160
|
+
...replyInThread ? { reply_in_thread: true } : {}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
assertFeishuMessageApiSuccess(response, "Feishu image reply failed");
|
|
164
|
+
return toFeishuSendResult(response, receiveId);
|
|
165
|
+
}
|
|
166
|
+
const response = await client.im.message.create({
|
|
167
|
+
params: { receive_id_type: receiveIdType },
|
|
168
|
+
data: {
|
|
169
|
+
receive_id: receiveId,
|
|
170
|
+
content,
|
|
171
|
+
msg_type: "image"
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
assertFeishuMessageApiSuccess(response, "Feishu image send failed");
|
|
175
|
+
return toFeishuSendResult(response, receiveId);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Send a file message using a file_key
|
|
179
|
+
*/
|
|
180
|
+
async function sendFileFeishu(params) {
|
|
181
|
+
const { cfg, to, fileKey, replyToMessageId, replyInThread, accountId } = params;
|
|
182
|
+
const msgType = params.msgType ?? "file";
|
|
183
|
+
const { client, receiveId, receiveIdType } = resolveFeishuSendTarget({
|
|
184
|
+
cfg,
|
|
185
|
+
to,
|
|
186
|
+
accountId
|
|
187
|
+
});
|
|
188
|
+
const content = JSON.stringify({ file_key: fileKey });
|
|
189
|
+
if (replyToMessageId) {
|
|
190
|
+
const response = await client.im.message.reply({
|
|
191
|
+
path: { message_id: replyToMessageId },
|
|
192
|
+
data: {
|
|
193
|
+
content,
|
|
194
|
+
msg_type: msgType,
|
|
195
|
+
...replyInThread ? { reply_in_thread: true } : {}
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
assertFeishuMessageApiSuccess(response, "Feishu file reply failed");
|
|
199
|
+
return toFeishuSendResult(response, receiveId);
|
|
200
|
+
}
|
|
201
|
+
const response = await client.im.message.create({
|
|
202
|
+
params: { receive_id_type: receiveIdType },
|
|
203
|
+
data: {
|
|
204
|
+
receive_id: receiveId,
|
|
205
|
+
content,
|
|
206
|
+
msg_type: msgType
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
assertFeishuMessageApiSuccess(response, "Feishu file send failed");
|
|
210
|
+
return toFeishuSendResult(response, receiveId);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Helper to detect file type from extension
|
|
214
|
+
*/
|
|
215
|
+
function detectFileType(fileName) {
|
|
216
|
+
switch (path.extname(fileName).toLowerCase()) {
|
|
217
|
+
case ".opus":
|
|
218
|
+
case ".ogg": return "opus";
|
|
219
|
+
case ".mp4":
|
|
220
|
+
case ".mov":
|
|
221
|
+
case ".avi": return "mp4";
|
|
222
|
+
case ".pdf": return "pdf";
|
|
223
|
+
case ".doc":
|
|
224
|
+
case ".docx": return "doc";
|
|
225
|
+
case ".xls":
|
|
226
|
+
case ".xlsx": return "xls";
|
|
227
|
+
case ".ppt":
|
|
228
|
+
case ".pptx": return "ppt";
|
|
229
|
+
default: return "stream";
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Upload and send media (image or file) from URL, local path, or buffer.
|
|
234
|
+
* When mediaUrl is a local path, mediaLocalRoots (from core outbound context)
|
|
235
|
+
* must be passed so loadWebMedia allows the path (post CVE-2026-26321).
|
|
236
|
+
*/
|
|
237
|
+
async function sendMediaFeishu(params) {
|
|
238
|
+
const { cfg, to, mediaUrl, mediaBuffer, fileName, replyToMessageId, replyInThread, accountId, mediaLocalRoots } = params;
|
|
239
|
+
const account = resolveFeishuAccount({
|
|
240
|
+
cfg,
|
|
241
|
+
accountId
|
|
242
|
+
});
|
|
243
|
+
if (!account.configured) throw new Error(`Feishu account "${account.accountId}" not configured`);
|
|
244
|
+
const mediaMaxBytes = (account.config?.mediaMaxMb ?? 30) * 1024 * 1024;
|
|
245
|
+
let buffer;
|
|
246
|
+
let name;
|
|
247
|
+
if (mediaBuffer) {
|
|
248
|
+
buffer = mediaBuffer;
|
|
249
|
+
name = fileName ?? "file";
|
|
250
|
+
} else if (mediaUrl) {
|
|
251
|
+
const loaded = await getFeishuRuntime().media.loadWebMedia(mediaUrl, {
|
|
252
|
+
maxBytes: mediaMaxBytes,
|
|
253
|
+
optimizeImages: false,
|
|
254
|
+
localRoots: mediaLocalRoots?.length ? mediaLocalRoots : void 0
|
|
255
|
+
});
|
|
256
|
+
buffer = loaded.buffer;
|
|
257
|
+
name = fileName ?? loaded.fileName ?? "file";
|
|
258
|
+
} else throw new Error("Either mediaUrl or mediaBuffer must be provided");
|
|
259
|
+
const ext = path.extname(name).toLowerCase();
|
|
260
|
+
if ([
|
|
261
|
+
".jpg",
|
|
262
|
+
".jpeg",
|
|
263
|
+
".png",
|
|
264
|
+
".gif",
|
|
265
|
+
".webp",
|
|
266
|
+
".bmp",
|
|
267
|
+
".ico",
|
|
268
|
+
".tiff"
|
|
269
|
+
].includes(ext)) {
|
|
270
|
+
const { imageKey } = await uploadImageFeishu({
|
|
271
|
+
cfg,
|
|
272
|
+
image: buffer,
|
|
273
|
+
accountId
|
|
274
|
+
});
|
|
275
|
+
return sendImageFeishu({
|
|
276
|
+
cfg,
|
|
277
|
+
to,
|
|
278
|
+
imageKey,
|
|
279
|
+
replyToMessageId,
|
|
280
|
+
replyInThread,
|
|
281
|
+
accountId
|
|
282
|
+
});
|
|
283
|
+
} else {
|
|
284
|
+
const fileType = detectFileType(name);
|
|
285
|
+
const { fileKey } = await uploadFileFeishu({
|
|
286
|
+
cfg,
|
|
287
|
+
file: buffer,
|
|
288
|
+
fileName: name,
|
|
289
|
+
fileType,
|
|
290
|
+
accountId
|
|
291
|
+
});
|
|
292
|
+
return sendFileFeishu({
|
|
293
|
+
cfg,
|
|
294
|
+
to,
|
|
295
|
+
fileKey,
|
|
296
|
+
msgType: fileType === "opus" ? "audio" : fileType === "mp4" ? "media" : "file",
|
|
297
|
+
replyToMessageId,
|
|
298
|
+
replyInThread,
|
|
299
|
+
accountId
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
//#endregion
|
|
304
|
+
export { downloadMessageResourceFeishu, sendFileFeishu, sendImageFeishu, sendMediaFeishu, uploadFileFeishu, uploadImageFeishu };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { FeishuMessageEvent } from "./bot.js";
|
|
2
|
+
|
|
3
|
+
//#region src/mention.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Mention target user info
|
|
6
|
+
*/
|
|
7
|
+
type MentionTarget = {
|
|
8
|
+
openId: string;
|
|
9
|
+
name: string;
|
|
10
|
+
key: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Extract mention targets from message event (excluding the bot itself)
|
|
14
|
+
*/
|
|
15
|
+
declare function extractMentionTargets(event: FeishuMessageEvent, botOpenId?: string): MentionTarget[];
|
|
16
|
+
/**
|
|
17
|
+
* Check if message is a mention forward request
|
|
18
|
+
* Rules:
|
|
19
|
+
* - Group: message mentions bot + at least one other user
|
|
20
|
+
* - DM: message mentions any user (no need to mention bot)
|
|
21
|
+
*/
|
|
22
|
+
declare function isMentionForwardRequest(event: FeishuMessageEvent, botOpenId?: string): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Extract message body from text (remove @ placeholders)
|
|
25
|
+
*/
|
|
26
|
+
declare function extractMessageBody(text: string, allMentionKeys: string[]): string;
|
|
27
|
+
/**
|
|
28
|
+
* Format @mention for text message
|
|
29
|
+
*/
|
|
30
|
+
declare function formatMentionForText(target: MentionTarget): string;
|
|
31
|
+
/**
|
|
32
|
+
* Format @everyone for text message
|
|
33
|
+
*/
|
|
34
|
+
declare function formatMentionAllForText(): string;
|
|
35
|
+
/**
|
|
36
|
+
* Format @mention for card message (lark_md)
|
|
37
|
+
*/
|
|
38
|
+
declare function formatMentionForCard(target: MentionTarget): string;
|
|
39
|
+
/**
|
|
40
|
+
* Format @everyone for card message
|
|
41
|
+
*/
|
|
42
|
+
declare function formatMentionAllForCard(): string;
|
|
43
|
+
/**
|
|
44
|
+
* Build complete message with @mentions (text format)
|
|
45
|
+
*/
|
|
46
|
+
declare function buildMentionedMessage(targets: MentionTarget[], message: string): string;
|
|
47
|
+
/**
|
|
48
|
+
* Build card content with @mentions (Markdown format)
|
|
49
|
+
*/
|
|
50
|
+
declare function buildMentionedCardContent(targets: MentionTarget[], message: string): string;
|
|
51
|
+
//#endregion
|
|
52
|
+
export { MentionTarget, buildMentionedCardContent, buildMentionedMessage, extractMentionTargets, extractMessageBody, formatMentionAllForCard, formatMentionAllForText, formatMentionForCard, formatMentionForText, isMentionForwardRequest };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
//#region src/mention.ts
|
|
2
|
+
/**
|
|
3
|
+
* Escape regex metacharacters so user-controlled mention fields are treated literally.
|
|
4
|
+
*/
|
|
5
|
+
function escapeRegExp(input) {
|
|
6
|
+
return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Extract mention targets from message event (excluding the bot itself)
|
|
10
|
+
*/
|
|
11
|
+
function extractMentionTargets(event, botOpenId) {
|
|
12
|
+
return (event.message.mentions ?? []).filter((m) => {
|
|
13
|
+
if (botOpenId && m.id.open_id === botOpenId) return false;
|
|
14
|
+
return !!m.id.open_id;
|
|
15
|
+
}).map((m) => ({
|
|
16
|
+
openId: m.id.open_id,
|
|
17
|
+
name: m.name,
|
|
18
|
+
key: m.key
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Check if message is a mention forward request
|
|
23
|
+
* Rules:
|
|
24
|
+
* - Group: message mentions bot + at least one other user
|
|
25
|
+
* - DM: message mentions any user (no need to mention bot)
|
|
26
|
+
*/
|
|
27
|
+
function isMentionForwardRequest(event, botOpenId) {
|
|
28
|
+
const mentions = event.message.mentions ?? [];
|
|
29
|
+
if (mentions.length === 0) return false;
|
|
30
|
+
const isDirectMessage = event.message.chat_type !== "group";
|
|
31
|
+
const hasOtherMention = mentions.some((m) => m.id.open_id !== botOpenId);
|
|
32
|
+
if (isDirectMessage) return hasOtherMention;
|
|
33
|
+
else return mentions.some((m) => m.id.open_id === botOpenId) && hasOtherMention;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Extract message body from text (remove @ placeholders)
|
|
37
|
+
*/
|
|
38
|
+
function extractMessageBody(text, allMentionKeys) {
|
|
39
|
+
let result = text;
|
|
40
|
+
for (const key of allMentionKeys) result = result.replace(new RegExp(escapeRegExp(key), "g"), "");
|
|
41
|
+
return result.replace(/\s+/g, " ").trim();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Format @mention for text message
|
|
45
|
+
*/
|
|
46
|
+
function formatMentionForText(target) {
|
|
47
|
+
return `<at user_id="${target.openId}">${target.name}</at>`;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Format @everyone for text message
|
|
51
|
+
*/
|
|
52
|
+
function formatMentionAllForText() {
|
|
53
|
+
return `<at user_id="all">Everyone</at>`;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Format @mention for card message (lark_md)
|
|
57
|
+
*/
|
|
58
|
+
function formatMentionForCard(target) {
|
|
59
|
+
return `<at id=${target.openId}></at>`;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Format @everyone for card message
|
|
63
|
+
*/
|
|
64
|
+
function formatMentionAllForCard() {
|
|
65
|
+
return `<at id=all></at>`;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Build complete message with @mentions (text format)
|
|
69
|
+
*/
|
|
70
|
+
function buildMentionedMessage(targets, message) {
|
|
71
|
+
if (targets.length === 0) return message;
|
|
72
|
+
return `${targets.map((t) => formatMentionForText(t)).join(" ")} ${message}`;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Build card content with @mentions (Markdown format)
|
|
76
|
+
*/
|
|
77
|
+
function buildMentionedCardContent(targets, message) {
|
|
78
|
+
if (targets.length === 0) return message;
|
|
79
|
+
return `${targets.map((t) => formatMentionForCard(t)).join(" ")} ${message}`;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
export { buildMentionedCardContent, buildMentionedMessage, extractMentionTargets, extractMessageBody, formatMentionAllForCard, formatMentionAllForText, formatMentionForCard, formatMentionForText, isMentionForwardRequest };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|