|
1 | 1 | import { jsonResult } from "openclaw/plugin-sdk/channel-actions"; |
2 | 2 | import { |
3 | 3 | isWhatsAppGroupJid, |
| 4 | + resolveAuthorizedWhatsAppOutboundTarget, |
| 5 | + resolveWhatsAppAccount, |
| 6 | + resolveWhatsAppMediaMaxBytes, |
4 | 7 | resolveReactionMessageId, |
5 | 8 | handleWhatsAppAction, |
6 | 9 | normalizeWhatsAppTarget, |
@@ -50,25 +53,122 @@ function readUploadFileCaptionText(args: Record<string, unknown>): string { |
50 | 53 | ); |
51 | 54 | } |
52 | 55 |
|
53 | | -async function handleWhatsAppUploadFileAction(params: WhatsAppMessageActionParams) { |
54 | | - const mediaUrl = readUploadFileMediaSource(params.params); |
55 | | - if (!mediaUrl) { |
56 | | - if (readStringParam(params.params, "buffer", { trim: false })) { |
| 56 | +function readBooleanParam(args: Record<string, unknown>, key: string): boolean | undefined { |
| 57 | + const value = args[key]; |
| 58 | + if (typeof value === "boolean") { |
| 59 | + return value; |
| 60 | + } |
| 61 | + if (typeof value !== "string") { |
| 62 | + return undefined; |
| 63 | + } |
| 64 | + const normalized = value.trim().toLowerCase(); |
| 65 | + if (normalized === "true") { |
| 66 | + return true; |
| 67 | + } |
| 68 | + if (normalized === "false") { |
| 69 | + return false; |
| 70 | + } |
| 71 | + return undefined; |
| 72 | +} |
| 73 | + |
| 74 | +function hasUploadFileBufferPayload(args: Record<string, unknown>): boolean { |
| 75 | + return readStringParam(args, "buffer", { trim: false }) !== undefined; |
| 76 | +} |
| 77 | + |
| 78 | +function extractBase64Payload(encoded: string): string { |
| 79 | + const match = /^data:[^;]+;base64,(.*)$/i.exec(encoded.trim()); |
| 80 | + return match ? match[1] : encoded; |
| 81 | +} |
| 82 | + |
| 83 | +function estimateBase64DecodedBytes(encoded: string): number { |
| 84 | + const compact = extractBase64Payload(encoded).replace(/\s/g, ""); |
| 85 | + if (!compact) { |
| 86 | + return 0; |
| 87 | + } |
| 88 | + const padding = compact.endsWith("==") ? 2 : compact.endsWith("=") ? 1 : 0; |
| 89 | + return Math.max(0, Math.floor((compact.length * 3) / 4) - padding); |
| 90 | +} |
| 91 | + |
| 92 | +function decodeUploadFileMediaPayload(params: { |
| 93 | + args: Record<string, unknown>; |
| 94 | + encoded: string; |
| 95 | + maxBytes?: number; |
| 96 | +}): |
| 97 | + | { |
| 98 | + buffer: Buffer; |
| 99 | + contentType?: string; |
| 100 | + fileName?: string; |
| 101 | + } |
| 102 | + | undefined { |
| 103 | + if (params.maxBytes !== undefined) { |
| 104 | + const estimatedBytes = estimateBase64DecodedBytes(params.encoded); |
| 105 | + if (estimatedBytes > params.maxBytes) { |
57 | 106 | throw new Error( |
58 | | - "WhatsApp upload-file cannot send buffer payloads. Use media, mediaUrl, filePath, path, or fileUrl instead.", |
| 107 | + `WhatsApp upload-file buffer exceeds configured media limit (${estimatedBytes} bytes > ${params.maxBytes} bytes).`, |
59 | 108 | ); |
60 | 109 | } |
61 | | - throw new Error("WhatsApp upload-file requires media, mediaUrl, filePath, path, or fileUrl."); |
| 110 | + } |
| 111 | + const contentType = |
| 112 | + readStringParam(params.args, "contentType") ?? readStringParam(params.args, "mimeType"); |
| 113 | + const fileName = |
| 114 | + readStringParam(params.args, "filename") ?? readStringParam(params.args, "fileName"); |
| 115 | + const buffer = Buffer.from(extractBase64Payload(params.encoded), "base64"); |
| 116 | + if (params.maxBytes !== undefined && buffer.byteLength > params.maxBytes) { |
| 117 | + throw new Error( |
| 118 | + `WhatsApp upload-file buffer exceeds configured media limit (${buffer.byteLength} bytes > ${params.maxBytes} bytes).`, |
| 119 | + ); |
| 120 | + } |
| 121 | + return { |
| 122 | + buffer, |
| 123 | + ...(contentType ? { contentType } : {}), |
| 124 | + ...(fileName ? { fileName } : {}), |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +async function handleWhatsAppUploadFileAction(params: WhatsAppMessageActionParams) { |
| 129 | + const mediaUrl = readUploadFileMediaSource(params.params); |
| 130 | + const encodedPayload = readStringParam(params.params, "buffer", { trim: false }); |
| 131 | + if (!mediaUrl && !hasUploadFileBufferPayload(params.params)) { |
| 132 | + throw new Error( |
| 133 | + "WhatsApp upload-file requires media, mediaUrl, filePath, path, fileUrl, or buffer.", |
| 134 | + ); |
62 | 135 | } |
63 | 136 | const to = readStringParam(params.params, "to", { required: true }); |
64 | | - const result = await sendMessageWhatsApp(to, readUploadFileCaptionText(params.params), { |
| 137 | + const resolved = resolveAuthorizedWhatsAppOutboundTarget({ |
| 138 | + cfg: params.cfg, |
| 139 | + chatJid: to, |
| 140 | + accountId: params.accountId ?? undefined, |
| 141 | + actionLabel: "upload-file", |
| 142 | + }); |
| 143 | + const account = resolveWhatsAppAccount({ |
| 144 | + cfg: params.cfg, |
| 145 | + accountId: resolved.accountId, |
| 146 | + }); |
| 147 | + const mediaPayload = encodedPayload |
| 148 | + ? decodeUploadFileMediaPayload({ |
| 149 | + args: params.params, |
| 150 | + encoded: encodedPayload, |
| 151 | + maxBytes: resolveWhatsAppMediaMaxBytes(account), |
| 152 | + }) |
| 153 | + : undefined; |
| 154 | + const result = await sendMessageWhatsApp(resolved.to, readUploadFileCaptionText(params.params), { |
65 | 155 | verbose: false, |
66 | 156 | cfg: params.cfg, |
67 | | - mediaUrl, |
| 157 | + ...(mediaUrl && !mediaPayload ? { mediaUrl } : {}), |
| 158 | + ...(mediaPayload ? { mediaPayload } : {}), |
68 | 159 | mediaAccess: params.mediaAccess, |
69 | 160 | mediaLocalRoots: params.mediaLocalRoots, |
70 | 161 | mediaReadFile: params.mediaReadFile, |
71 | | - accountId: params.accountId ?? undefined, |
| 162 | + gifPlayback: readBooleanParam(params.params, "gifPlayback") ?? undefined, |
| 163 | + audioAsVoice: |
| 164 | + readBooleanParam(params.params, "asVoice") ?? |
| 165 | + readBooleanParam(params.params, "audioAsVoice") ?? |
| 166 | + undefined, |
| 167 | + forceDocument: |
| 168 | + readBooleanParam(params.params, "forceDocument") ?? |
| 169 | + readBooleanParam(params.params, "asDocument") ?? |
| 170 | + undefined, |
| 171 | + accountId: resolved.accountId, |
72 | 172 | }); |
73 | 173 | return jsonResult({ |
74 | 174 | ok: true, |
|
0 commit comments