@marshulll/openclaw-wecom 0.1.16 → 0.1.17
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/package.json +1 -1
- package/wecom/src/wecom-app.ts +151 -20
- package/wecom/src/wecom-bot.ts +159 -28
package/package.json
CHANGED
package/wecom/src/wecom-app.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
2
|
import crypto from "node:crypto";
|
|
3
3
|
import { XMLParser } from "fast-xml-parser";
|
|
4
|
-
import { mkdir, readdir, rm, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import { mkdir, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
|
|
5
5
|
import { tmpdir } from "node:os";
|
|
6
|
-
import { join } from "node:path";
|
|
6
|
+
import { basename, extname, join } from "node:path";
|
|
7
7
|
|
|
8
8
|
import type { WecomWebhookTarget } from "./monitor.js";
|
|
9
9
|
import { decryptWecomEncrypted, verifyWecomSignature } from "./crypto.js";
|
|
@@ -173,6 +173,139 @@ function normalizeMediaType(raw?: string): "image" | "voice" | "video" | "file"
|
|
|
173
173
|
return null;
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
function pickString(...values: unknown[]): string {
|
|
177
|
+
for (const value of values) {
|
|
178
|
+
if (typeof value === "string" && value.trim()) return value.trim();
|
|
179
|
+
}
|
|
180
|
+
return "";
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function resolveContentTypeFromExt(ext: string): string {
|
|
184
|
+
const value = ext.toLowerCase();
|
|
185
|
+
if (value === "png") return "image/png";
|
|
186
|
+
if (value === "gif") return "image/gif";
|
|
187
|
+
if (value === "jpg" || value === "jpeg") return "image/jpeg";
|
|
188
|
+
if (value === "webp") return "image/webp";
|
|
189
|
+
if (value === "bmp") return "image/bmp";
|
|
190
|
+
if (value === "amr") return "audio/amr";
|
|
191
|
+
if (value === "wav") return "audio/wav";
|
|
192
|
+
if (value === "mp3") return "audio/mpeg";
|
|
193
|
+
if (value === "m4a") return "audio/mp4";
|
|
194
|
+
if (value === "mp4") return "video/mp4";
|
|
195
|
+
if (value === "mov") return "video/quicktime";
|
|
196
|
+
if (value === "avi") return "video/x-msvideo";
|
|
197
|
+
if (value === "pdf") return "application/pdf";
|
|
198
|
+
if (value === "txt") return "text/plain";
|
|
199
|
+
if (value === "csv") return "text/csv";
|
|
200
|
+
if (value === "json") return "application/json";
|
|
201
|
+
if (value === "doc") return "application/msword";
|
|
202
|
+
if (value === "docx") return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
203
|
+
if (value === "xls") return "application/vnd.ms-excel";
|
|
204
|
+
if (value === "xlsx") return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
205
|
+
if (value === "ppt") return "application/vnd.ms-powerpoint";
|
|
206
|
+
if (value === "pptx") return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
|
|
207
|
+
if (value === "zip") return "application/zip";
|
|
208
|
+
return "application/octet-stream";
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function resolveMediaTypeFromContentType(contentType: string): "image" | "voice" | "video" | "file" {
|
|
212
|
+
const value = contentType.toLowerCase();
|
|
213
|
+
if (value.startsWith("image/")) return "image";
|
|
214
|
+
if (value.startsWith("audio/")) return "voice";
|
|
215
|
+
if (value.startsWith("video/")) return "video";
|
|
216
|
+
return "file";
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function stripFileProtocol(rawPath: string): string {
|
|
220
|
+
return rawPath.startsWith("file://") ? rawPath.replace(/^file:\/\//, "") : rawPath;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function parseBase64Input(input: string): { data: string; mimeType?: string } {
|
|
224
|
+
const match = input.match(/^data:([^;]+);base64,(.*)$/i);
|
|
225
|
+
if (match) {
|
|
226
|
+
return { data: match[2], mimeType: match[1] };
|
|
227
|
+
}
|
|
228
|
+
return { data: input };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function resolveOutboundMediaSpec(payload: any): {
|
|
232
|
+
type?: string;
|
|
233
|
+
url?: string;
|
|
234
|
+
path?: string;
|
|
235
|
+
base64?: string;
|
|
236
|
+
filename?: string;
|
|
237
|
+
mimeType?: string;
|
|
238
|
+
} | null {
|
|
239
|
+
if (!payload || typeof payload !== "object") return null;
|
|
240
|
+
const mediaBlockRaw = payload.media ?? payload.attachment ?? payload.file ?? payload.files;
|
|
241
|
+
const mediaBlock = Array.isArray(mediaBlockRaw) ? mediaBlockRaw[0] : mediaBlockRaw;
|
|
242
|
+
const url = pickString(
|
|
243
|
+
payload.mediaUrl,
|
|
244
|
+
mediaBlock?.url,
|
|
245
|
+
mediaBlock?.mediaUrl,
|
|
246
|
+
mediaBlock?.fileUrl,
|
|
247
|
+
mediaBlock?.file_url,
|
|
248
|
+
);
|
|
249
|
+
const path = pickString(
|
|
250
|
+
payload.mediaPath,
|
|
251
|
+
payload.filePath,
|
|
252
|
+
mediaBlock?.path,
|
|
253
|
+
mediaBlock?.filePath,
|
|
254
|
+
mediaBlock?.localPath,
|
|
255
|
+
);
|
|
256
|
+
const base64 = pickString(
|
|
257
|
+
payload.mediaBase64,
|
|
258
|
+
payload.base64,
|
|
259
|
+
mediaBlock?.base64,
|
|
260
|
+
mediaBlock?.data,
|
|
261
|
+
);
|
|
262
|
+
const type = pickString(payload.mediaType, mediaBlock?.type, mediaBlock?.mediaType);
|
|
263
|
+
const filename = pickString(payload.filename, payload.fileName, mediaBlock?.filename, mediaBlock?.fileName, mediaBlock?.name);
|
|
264
|
+
const mimeType = pickString(payload.mimeType, payload.mediaMimeType, mediaBlock?.mimeType, mediaBlock?.contentType);
|
|
265
|
+
if (!url && !path && !base64) return null;
|
|
266
|
+
return { type, url, path, base64, filename, mimeType };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async function loadOutboundMedia(params: {
|
|
270
|
+
payload: any;
|
|
271
|
+
account: WecomWebhookTarget["account"];
|
|
272
|
+
maxBytes: number | undefined;
|
|
273
|
+
}): Promise<{ buffer: Buffer; contentType: string; type: "image" | "voice" | "video" | "file"; filename: string } | null> {
|
|
274
|
+
const spec = resolveOutboundMediaSpec(params.payload);
|
|
275
|
+
if (!spec) return null;
|
|
276
|
+
|
|
277
|
+
let buffer: Buffer | null = null;
|
|
278
|
+
let contentType = spec.mimeType ?? "";
|
|
279
|
+
let filename = spec.filename ?? "";
|
|
280
|
+
|
|
281
|
+
if (spec.base64) {
|
|
282
|
+
const parsed = parseBase64Input(spec.base64);
|
|
283
|
+
buffer = Buffer.from(parsed.data, "base64");
|
|
284
|
+
if (!contentType && parsed.mimeType) contentType = parsed.mimeType;
|
|
285
|
+
} else if (spec.path) {
|
|
286
|
+
const resolvedPath = stripFileProtocol(spec.path);
|
|
287
|
+
buffer = await readFile(resolvedPath);
|
|
288
|
+
if (!filename) filename = basename(resolvedPath);
|
|
289
|
+
if (!contentType) {
|
|
290
|
+
const ext = extname(resolvedPath).replace(".", "");
|
|
291
|
+
contentType = resolveContentTypeFromExt(ext);
|
|
292
|
+
}
|
|
293
|
+
} else if (spec.url) {
|
|
294
|
+
const media = await fetchMediaFromUrl(spec.url, params.account);
|
|
295
|
+
buffer = media.buffer;
|
|
296
|
+
if (!contentType) contentType = media.contentType;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (!buffer) return null;
|
|
300
|
+
if (params.maxBytes && buffer.length > params.maxBytes) return null;
|
|
301
|
+
|
|
302
|
+
const type = normalizeMediaType(spec.type) ?? resolveMediaTypeFromContentType(contentType || "application/octet-stream");
|
|
303
|
+
const ext = resolveExtFromContentType(contentType || "application/octet-stream", type);
|
|
304
|
+
const safeName = sanitizeFilename(filename, `${type}.${ext}`);
|
|
305
|
+
|
|
306
|
+
return { buffer, contentType: contentType || resolveContentTypeFromExt(ext), type, filename: safeName };
|
|
307
|
+
}
|
|
308
|
+
|
|
176
309
|
function sanitizeFilename(name: string, fallback: string): string {
|
|
177
310
|
const base = name.split(/[/\\\\]/).pop() ?? "";
|
|
178
311
|
const trimmed = base.trim();
|
|
@@ -329,38 +462,35 @@ async function startAgentForApp(params: {
|
|
|
329
462
|
cfg: config,
|
|
330
463
|
dispatcherOptions: {
|
|
331
464
|
deliver: async (payload, info) => {
|
|
332
|
-
const
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
const media = await fetchMediaFromUrl(maybeMediaUrl, account);
|
|
337
|
-
const type = normalizeMediaType(maybeMediaType) ?? "file";
|
|
338
|
-
const ext = resolveExtFromContentType(media.contentType, type);
|
|
465
|
+
const maxBytes = resolveMediaMaxBytes(target);
|
|
466
|
+
try {
|
|
467
|
+
const outbound = await loadOutboundMedia({ payload, account, maxBytes });
|
|
468
|
+
if (outbound) {
|
|
339
469
|
const mediaId = await uploadWecomMedia({
|
|
340
470
|
account,
|
|
341
|
-
type: type
|
|
342
|
-
buffer:
|
|
343
|
-
filename:
|
|
471
|
+
type: outbound.type,
|
|
472
|
+
buffer: outbound.buffer,
|
|
473
|
+
filename: outbound.filename,
|
|
344
474
|
});
|
|
345
|
-
if (type === "image") {
|
|
475
|
+
if (outbound.type === "image") {
|
|
346
476
|
await sendWecomImage({ account, toUser: fromUser, chatId: isGroup ? chatId : undefined, mediaId });
|
|
347
477
|
logVerbose(target, `app image reply delivered (${info.kind}) to ${fromUser}`);
|
|
348
|
-
} else if (type === "voice") {
|
|
478
|
+
} else if (outbound.type === "voice") {
|
|
349
479
|
await sendWecomVoice({ account, toUser: fromUser, chatId: isGroup ? chatId : undefined, mediaId });
|
|
350
480
|
logVerbose(target, `app voice reply delivered (${info.kind}) to ${fromUser}`);
|
|
351
|
-
} else if (type === "video") {
|
|
481
|
+
} else if (outbound.type === "video") {
|
|
352
482
|
const title = (payload as any).title as string | undefined;
|
|
353
483
|
const description = (payload as any).description as string | undefined;
|
|
354
484
|
await sendWecomVideo({ account, toUser: fromUser, chatId: isGroup ? chatId : undefined, mediaId, title, description });
|
|
355
485
|
logVerbose(target, `app video reply delivered (${info.kind}) to ${fromUser}`);
|
|
356
|
-
} else if (type === "file") {
|
|
486
|
+
} else if (outbound.type === "file") {
|
|
357
487
|
await sendWecomFile({ account, toUser: fromUser, chatId: isGroup ? chatId : undefined, mediaId });
|
|
358
488
|
logVerbose(target, `app file reply delivered (${info.kind}) to ${fromUser}`);
|
|
359
489
|
}
|
|
360
490
|
target.statusSink?.({ lastOutboundAt: Date.now() });
|
|
361
|
-
} catch (err) {
|
|
362
|
-
target.runtime.error?.(`wecom app media reply failed: ${String(err)}`);
|
|
363
491
|
}
|
|
492
|
+
} catch (err) {
|
|
493
|
+
target.runtime.error?.(`wecom app media reply failed: ${String(err)}`);
|
|
364
494
|
}
|
|
365
495
|
|
|
366
496
|
const text = markdownToWecomText(core.channel.text.convertMarkdownTables(payload.text ?? "", tableMode));
|
|
@@ -599,7 +729,8 @@ async function processAppMessage(params: {
|
|
|
599
729
|
if (cached) {
|
|
600
730
|
mediaContext = { type: cached.type, path: cached.path, mimeType: cached.mimeType, url: cached.url };
|
|
601
731
|
logVerbose(target, `app file cache hit: ${cached.path}`);
|
|
602
|
-
|
|
732
|
+
const cachedName = fileName || basename(cached.path) || "未知文件";
|
|
733
|
+
messageText = `[用户发送了一个文件: ${cachedName},已保存到: ${cached.path}]\n\n请使用 Read 工具查看这个文件的内容并回复用户。`;
|
|
603
734
|
} else {
|
|
604
735
|
const media = await downloadWecomMedia({ account: target.account, mediaId });
|
|
605
736
|
const maxBytes = resolveMediaMaxBytes(target);
|
|
@@ -627,7 +758,7 @@ async function processAppMessage(params: {
|
|
|
627
758
|
size: media.buffer.length,
|
|
628
759
|
});
|
|
629
760
|
logVerbose(target, `app file saved (${media.buffer.length} bytes): ${tempFilePath}`);
|
|
630
|
-
messageText = `[用户发送了一个文件: ${safeName}]\n\n
|
|
761
|
+
messageText = `[用户发送了一个文件: ${safeName},已保存到: ${tempFilePath}]\n\n请使用 Read 工具查看这个文件的内容并回复用户。`;
|
|
631
762
|
}
|
|
632
763
|
}
|
|
633
764
|
} catch (err) {
|
package/wecom/src/wecom-bot.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
2
|
import crypto from "node:crypto";
|
|
3
|
-
import { mkdir, readdir, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { mkdir, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
|
-
import { join } from "node:path";
|
|
5
|
+
import { basename, extname, join } from "node:path";
|
|
6
6
|
|
|
7
7
|
import type { PluginRuntime } from "openclaw/plugin-sdk";
|
|
8
8
|
|
|
@@ -414,49 +414,44 @@ async function startAgentForStream(params: {
|
|
|
414
414
|
cfg: config,
|
|
415
415
|
dispatcherOptions: {
|
|
416
416
|
deliver: async (payload) => {
|
|
417
|
-
const maybeMediaUrl = (payload as any).mediaUrl as string | undefined;
|
|
418
|
-
const maybeMediaType = (payload as any).mediaType as string | undefined;
|
|
419
417
|
const canBridgeMedia = account.config.botMediaBridge !== false
|
|
420
418
|
&& Boolean(account.corpId && account.corpSecret && account.agentId);
|
|
421
419
|
const toChatId = chatType === "group" ? chatId : undefined;
|
|
422
420
|
|
|
423
|
-
if (
|
|
421
|
+
if (canBridgeMedia) {
|
|
424
422
|
try {
|
|
425
|
-
const
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
:
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
: media.contentType.includes("amr") ? "amr"
|
|
432
|
-
: media.contentType.includes("wav") ? "wav"
|
|
433
|
-
: media.contentType.includes("mp3") ? "mp3"
|
|
434
|
-
: "bin";
|
|
423
|
+
const outbound = await loadOutboundMedia({
|
|
424
|
+
payload,
|
|
425
|
+
account,
|
|
426
|
+
maxBytes: resolveMediaMaxBytes(target),
|
|
427
|
+
});
|
|
428
|
+
if (outbound) {
|
|
435
429
|
const mediaId = await uploadWecomMedia({
|
|
436
430
|
account,
|
|
437
|
-
type: type
|
|
438
|
-
buffer:
|
|
439
|
-
filename:
|
|
431
|
+
type: outbound.type,
|
|
432
|
+
buffer: outbound.buffer,
|
|
433
|
+
filename: outbound.filename,
|
|
440
434
|
});
|
|
441
|
-
if (type === "image") {
|
|
435
|
+
if (outbound.type === "image") {
|
|
442
436
|
await sendWecomImage({ account, toUser: userid, chatId: toChatId, mediaId });
|
|
443
|
-
} else if (type === "voice") {
|
|
437
|
+
} else if (outbound.type === "voice") {
|
|
444
438
|
await sendWecomVoice({ account, toUser: userid, chatId: toChatId, mediaId });
|
|
445
|
-
} else if (type === "video") {
|
|
439
|
+
} else if (outbound.type === "video") {
|
|
446
440
|
const title = (payload as any).title as string | undefined;
|
|
447
441
|
const description = (payload as any).description as string | undefined;
|
|
448
442
|
await sendWecomVideo({ account, toUser: userid, chatId: toChatId, mediaId, title, description });
|
|
449
|
-
} else if (type === "file") {
|
|
443
|
+
} else if (outbound.type === "file") {
|
|
450
444
|
await sendWecomFile({ account, toUser: userid, chatId: toChatId, mediaId });
|
|
451
445
|
}
|
|
452
446
|
const current = streams.get(streamId);
|
|
453
447
|
if (current) {
|
|
454
|
-
const note = mediaSentLabel(type);
|
|
448
|
+
const note = mediaSentLabel(outbound.type);
|
|
455
449
|
const nextText = current.content ? `${current.content}\n\n${note}` : note;
|
|
456
450
|
current.content = truncateUtf8Bytes(nextText.trim(), STREAM_MAX_BYTES);
|
|
457
451
|
current.updatedAt = Date.now();
|
|
458
452
|
}
|
|
459
453
|
target.statusSink?.({ lastOutboundAt: Date.now() });
|
|
454
|
+
}
|
|
460
455
|
} catch (err) {
|
|
461
456
|
target.runtime.error?.(`[${account.accountId}] wecom bot media bridge failed: ${String(err)}`);
|
|
462
457
|
}
|
|
@@ -594,11 +589,21 @@ async function buildBotMediaMessage(params: {
|
|
|
594
589
|
const cacheKey = buildMediaCacheKey({ url, base64 });
|
|
595
590
|
const cached = await getCachedMedia(cacheKey, resolveMediaRetentionMs(target));
|
|
596
591
|
if (cached) {
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
592
|
+
if (msgtype === "image" && cached.summary) {
|
|
593
|
+
return {
|
|
594
|
+
text: `[用户发送了一张图片]\n\n[图片识别结果]\n${cached.summary}\n\n请根据识别结果回复用户。`,
|
|
595
|
+
media: cached.media,
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
if (msgtype === "file") {
|
|
599
|
+
const safeName = sanitizeFilename(filename || basename(cached.media.path), "file");
|
|
600
|
+
return {
|
|
601
|
+
text: `[用户发送了一个文件: ${safeName},已保存到: ${cached.media.path}]\n\n请使用 Read 工具查看这个文件的内容并回复用户。`,
|
|
602
|
+
media: cached.media,
|
|
603
|
+
};
|
|
604
|
+
}
|
|
600
605
|
return {
|
|
601
|
-
text,
|
|
606
|
+
text: buildInboundMediaPrompt(msgtype, filename),
|
|
602
607
|
media: cached.media,
|
|
603
608
|
};
|
|
604
609
|
}
|
|
@@ -656,7 +661,7 @@ async function buildBotMediaMessage(params: {
|
|
|
656
661
|
};
|
|
657
662
|
storeCachedMedia(cacheKey, media, buffer.length);
|
|
658
663
|
return {
|
|
659
|
-
text:
|
|
664
|
+
text: `[用户发送了一个文件: ${safeName},已保存到: ${tempFilePath}]\n\n请使用 Read 工具查看这个文件的内容并回复用户。`,
|
|
660
665
|
media,
|
|
661
666
|
};
|
|
662
667
|
}
|
|
@@ -790,6 +795,132 @@ function normalizeMediaType(raw?: string): "image" | "voice" | "video" | "file"
|
|
|
790
795
|
return null;
|
|
791
796
|
}
|
|
792
797
|
|
|
798
|
+
function resolveContentTypeFromExt(ext: string): string {
|
|
799
|
+
const value = ext.toLowerCase();
|
|
800
|
+
if (value === "png") return "image/png";
|
|
801
|
+
if (value === "gif") return "image/gif";
|
|
802
|
+
if (value === "jpg" || value === "jpeg") return "image/jpeg";
|
|
803
|
+
if (value === "webp") return "image/webp";
|
|
804
|
+
if (value === "bmp") return "image/bmp";
|
|
805
|
+
if (value === "amr") return "audio/amr";
|
|
806
|
+
if (value === "wav") return "audio/wav";
|
|
807
|
+
if (value === "mp3") return "audio/mpeg";
|
|
808
|
+
if (value === "m4a") return "audio/mp4";
|
|
809
|
+
if (value === "mp4") return "video/mp4";
|
|
810
|
+
if (value === "mov") return "video/quicktime";
|
|
811
|
+
if (value === "avi") return "video/x-msvideo";
|
|
812
|
+
if (value === "pdf") return "application/pdf";
|
|
813
|
+
if (value === "txt") return "text/plain";
|
|
814
|
+
if (value === "csv") return "text/csv";
|
|
815
|
+
if (value === "json") return "application/json";
|
|
816
|
+
if (value === "doc") return "application/msword";
|
|
817
|
+
if (value === "docx") return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
818
|
+
if (value === "xls") return "application/vnd.ms-excel";
|
|
819
|
+
if (value === "xlsx") return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
820
|
+
if (value === "ppt") return "application/vnd.ms-powerpoint";
|
|
821
|
+
if (value === "pptx") return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
|
|
822
|
+
if (value === "zip") return "application/zip";
|
|
823
|
+
return "application/octet-stream";
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
function resolveMediaTypeFromContentType(contentType: string): "image" | "voice" | "video" | "file" {
|
|
827
|
+
const value = contentType.toLowerCase();
|
|
828
|
+
if (value.startsWith("image/")) return "image";
|
|
829
|
+
if (value.startsWith("audio/")) return "voice";
|
|
830
|
+
if (value.startsWith("video/")) return "video";
|
|
831
|
+
return "file";
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
function stripFileProtocol(rawPath: string): string {
|
|
835
|
+
return rawPath.startsWith("file://") ? rawPath.replace(/^file:\/\//, "") : rawPath;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
function parseBase64Input(input: string): { data: string; mimeType?: string } {
|
|
839
|
+
const match = input.match(/^data:([^;]+);base64,(.*)$/i);
|
|
840
|
+
if (match) {
|
|
841
|
+
return { data: match[2], mimeType: match[1] };
|
|
842
|
+
}
|
|
843
|
+
return { data: input };
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
function resolveOutboundMediaSpec(payload: any): {
|
|
847
|
+
type?: string;
|
|
848
|
+
url?: string;
|
|
849
|
+
path?: string;
|
|
850
|
+
base64?: string;
|
|
851
|
+
filename?: string;
|
|
852
|
+
mimeType?: string;
|
|
853
|
+
} | null {
|
|
854
|
+
if (!payload || typeof payload !== "object") return null;
|
|
855
|
+
const mediaBlockRaw = payload.media ?? payload.attachment ?? payload.file ?? payload.files;
|
|
856
|
+
const mediaBlock = Array.isArray(mediaBlockRaw) ? mediaBlockRaw[0] : mediaBlockRaw;
|
|
857
|
+
const url = pickString(
|
|
858
|
+
payload.mediaUrl,
|
|
859
|
+
mediaBlock?.url,
|
|
860
|
+
mediaBlock?.mediaUrl,
|
|
861
|
+
mediaBlock?.fileUrl,
|
|
862
|
+
mediaBlock?.file_url,
|
|
863
|
+
);
|
|
864
|
+
const path = pickString(
|
|
865
|
+
payload.mediaPath,
|
|
866
|
+
payload.filePath,
|
|
867
|
+
mediaBlock?.path,
|
|
868
|
+
mediaBlock?.filePath,
|
|
869
|
+
mediaBlock?.localPath,
|
|
870
|
+
);
|
|
871
|
+
const base64 = pickString(
|
|
872
|
+
payload.mediaBase64,
|
|
873
|
+
payload.base64,
|
|
874
|
+
mediaBlock?.base64,
|
|
875
|
+
mediaBlock?.data,
|
|
876
|
+
);
|
|
877
|
+
const type = pickString(payload.mediaType, mediaBlock?.type, mediaBlock?.mediaType);
|
|
878
|
+
const filename = pickString(payload.filename, payload.fileName, mediaBlock?.filename, mediaBlock?.fileName, mediaBlock?.name);
|
|
879
|
+
const mimeType = pickString(payload.mimeType, payload.mediaMimeType, mediaBlock?.mimeType, mediaBlock?.contentType);
|
|
880
|
+
if (!url && !path && !base64) return null;
|
|
881
|
+
return { type, url, path, base64, filename, mimeType };
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
async function loadOutboundMedia(params: {
|
|
885
|
+
payload: any;
|
|
886
|
+
account: ResolvedWecomAccount;
|
|
887
|
+
maxBytes: number | undefined;
|
|
888
|
+
}): Promise<{ buffer: Buffer; contentType: string; type: "image" | "voice" | "video" | "file"; filename: string } | null> {
|
|
889
|
+
const spec = resolveOutboundMediaSpec(params.payload);
|
|
890
|
+
if (!spec) return null;
|
|
891
|
+
|
|
892
|
+
let buffer: Buffer | null = null;
|
|
893
|
+
let contentType = spec.mimeType ?? "";
|
|
894
|
+
let filename = spec.filename ?? "";
|
|
895
|
+
|
|
896
|
+
if (spec.base64) {
|
|
897
|
+
const parsed = parseBase64Input(spec.base64);
|
|
898
|
+
buffer = Buffer.from(parsed.data, "base64");
|
|
899
|
+
if (!contentType && parsed.mimeType) contentType = parsed.mimeType;
|
|
900
|
+
} else if (spec.path) {
|
|
901
|
+
const resolvedPath = stripFileProtocol(spec.path);
|
|
902
|
+
buffer = await readFile(resolvedPath);
|
|
903
|
+
if (!filename) filename = basename(resolvedPath);
|
|
904
|
+
if (!contentType) {
|
|
905
|
+
const ext = extname(resolvedPath).replace(".", "");
|
|
906
|
+
contentType = resolveContentTypeFromExt(ext);
|
|
907
|
+
}
|
|
908
|
+
} else if (spec.url) {
|
|
909
|
+
const media = await fetchMediaFromUrl(spec.url, params.account);
|
|
910
|
+
buffer = media.buffer;
|
|
911
|
+
if (!contentType) contentType = media.contentType;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
if (!buffer) return null;
|
|
915
|
+
if (params.maxBytes && buffer.length > params.maxBytes) return null;
|
|
916
|
+
|
|
917
|
+
const type = normalizeMediaType(spec.type) ?? resolveMediaTypeFromContentType(contentType || "application/octet-stream");
|
|
918
|
+
const ext = resolveExtFromContentType(contentType || "application/octet-stream", type);
|
|
919
|
+
const safeName = sanitizeFilename(filename, `${type}.${ext}`);
|
|
920
|
+
|
|
921
|
+
return { buffer, contentType: contentType || resolveContentTypeFromExt(ext), type, filename: safeName };
|
|
922
|
+
}
|
|
923
|
+
|
|
793
924
|
function mediaSentLabel(type: string): string {
|
|
794
925
|
if (type === "image") return "[已发送图片]";
|
|
795
926
|
if (type === "voice") return "[已发送语音]";
|