@nuiisweety/baileys 0.1.3 → 0.1.5

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.
@@ -0,0 +1,1768 @@
1
+ import { Boom } from '@hapi/boom';
2
+ import { randomBytes } from 'crypto';
3
+ import { zip } from 'fflate';
4
+ import { promises as fs } from 'fs';
5
+ import {} from 'stream';
6
+ import { proto } from '../../WAProto/index.js';
7
+ import { CALL_AUDIO_PREFIX, CALL_VIDEO_PREFIX, DONATE_URL, LIBRARY_NAME, MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from '../Defaults/index.js';
8
+ import { AssociationType, ButtonHeaderType, ButtonType, CarouselCardType, ListType, ProtocolType, WAMessageStatus, WAProto } from '../Types/index.js';
9
+ import { isJidGroup, isJidNewsletter, isJidStatusBroadcast, isLidUser, isPnUser, jidNormalizedUser } from '../WABinary/index.js';
10
+ import { sha256 } from './crypto.js';
11
+ import { generateMessageIDV2, getKeyAuthor, unixTimestampSeconds } from './generics.js';
12
+ import { downloadContentFromMessage, encryptedStream, generateThumbnail, getAudioDuration, getAudioWaveform, getImageProcessingLibrary, getRawMediaUploadData, getStream, toBuffer } from './messages-media.js';
13
+ import { prepareRichResponseMessage } from './rich-message-utils.js';
14
+ import { shouldIncludeReportingToken } from './reporting-utils.js';
15
+ const CONCURRENCY_LIMIT = 10;
16
+ const MIMETYPE_MAP = {
17
+ image: 'image/jpeg',
18
+ video: 'video/mp4',
19
+ document: 'application/pdf',
20
+ audio: 'audio/ogg; codecs=opus',
21
+ sticker: 'image/webp',
22
+ 'product-catalog-image': 'image/jpeg'
23
+ };
24
+ const MessageTypeProto = {
25
+ image: WAProto.Message.ImageMessage,
26
+ video: WAProto.Message.VideoMessage,
27
+ audio: WAProto.Message.AudioMessage,
28
+ sticker: WAProto.Message.StickerMessage,
29
+ document: WAProto.Message.DocumentMessage
30
+ };
31
+ /**
32
+ * Uses a regex to test whether the string contains a URL, and returns the URL if it does.
33
+ * @param text eg. hello https://google.com
34
+ * @returns the URL, eg. https://google.com
35
+ */
36
+ export const extractUrlFromText = (text) => text.match(URL_REGEX)?.[0];
37
+ export const generateLinkPreviewIfRequired = async (text, getUrlInfo, logger) => {
38
+ const url = extractUrlFromText(text);
39
+ if (!!getUrlInfo && url) {
40
+ try {
41
+ const urlInfo = await getUrlInfo(url);
42
+ return urlInfo;
43
+ }
44
+ catch (error) {
45
+ // ignore if fails
46
+ logger?.warn({ trace: error.stack }, 'url generation failed');
47
+ }
48
+ }
49
+ };
50
+ const assertColor = async (color) => {
51
+ let assertedColor;
52
+ if (typeof color === 'number') {
53
+ assertedColor = color > 0 ? color : 0xffffffff + Number(color) + 1;
54
+ }
55
+ else {
56
+ let hex = color.trim().replace('#', '');
57
+ if (hex.length <= 6) {
58
+ hex = 'FF' + hex.padStart(6, '0');
59
+ }
60
+ assertedColor = parseInt(hex, 16);
61
+ return assertedColor;
62
+ }
63
+ };
64
+ export const prepareWAMessageMedia = async (message, options) => {
65
+ const logger = options.logger;
66
+ let mediaType;
67
+ for (const key of MEDIA_KEYS) {
68
+ if (key in message) {
69
+ mediaType = key;
70
+ }
71
+ }
72
+ if (!mediaType) {
73
+ throw new Boom('Invalid media type', { statusCode: 400 });
74
+ }
75
+ const uploadData = {
76
+ ...message,
77
+ media: message[mediaType]
78
+ };
79
+ delete uploadData[mediaType];
80
+ // check if cacheable + generate cache key
81
+ const cacheableKey = typeof uploadData.media === 'object' &&
82
+ 'url' in uploadData.media &&
83
+ !!uploadData.media.url &&
84
+ !!options.mediaCache &&
85
+ mediaType + ':' + uploadData.media.url.toString();
86
+ if (mediaType === 'document' && !uploadData.fileName) {
87
+ uploadData.fileName = 'file';
88
+ }
89
+ if (!uploadData.mimetype) {
90
+ uploadData.mimetype = MIMETYPE_MAP[mediaType];
91
+ }
92
+ if (cacheableKey) {
93
+ const mediaBuff = await options.mediaCache.get(cacheableKey);
94
+ if (mediaBuff) {
95
+ logger?.debug({ cacheableKey }, 'got media cache hit');
96
+ const obj = proto.Message.decode(mediaBuff);
97
+ const key = `${mediaType}Message`;
98
+ Object.assign(obj[key], { ...uploadData, media: undefined });
99
+ return obj;
100
+ }
101
+ }
102
+ const isNewsletter = !!options.jid && isJidNewsletter(options.jid);
103
+ if (isNewsletter) {
104
+ logger?.info({ key: cacheableKey }, 'Preparing raw media for newsletter');
105
+ const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(uploadData.media, options.mediaTypeOverride || mediaType, logger);
106
+ const fileSha256B64 = fileSha256.toString('base64');
107
+ const { mediaUrl, directPath } = await options.upload(filePath, {
108
+ fileEncSha256B64: fileSha256B64,
109
+ mediaType: mediaType,
110
+ timeoutMs: options.mediaUploadTimeoutMs
111
+ });
112
+ await fs.unlink(filePath);
113
+ const obj = WAProto.Message.fromObject({
114
+ // todo: add more support here
115
+ [`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
116
+ url: mediaUrl,
117
+ directPath,
118
+ fileSha256,
119
+ fileLength,
120
+ ...uploadData,
121
+ media: undefined
122
+ })
123
+ });
124
+ if (uploadData.ptv) {
125
+ obj.ptvMessage = obj.videoMessage;
126
+ delete obj.videoMessage;
127
+ }
128
+ if (obj.stickerMessage) {
129
+ obj.stickerMessage.stickerSentTs = Date.now();
130
+ }
131
+ if (cacheableKey) {
132
+ logger?.debug({ cacheableKey }, 'set cache');
133
+ await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish());
134
+ }
135
+ return obj;
136
+ }
137
+ const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined';
138
+ const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') && typeof uploadData['jpegThumbnail'] === 'undefined';
139
+ const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === true && typeof uploadData.waveform === 'undefined';
140
+ const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === true;
141
+ const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation;
142
+ const { mediaKey, encFilePath, originalFilePath, fileEncSha256, fileSha256, fileLength } = await encryptedStream(uploadData.media, options.mediaTypeOverride || mediaType, {
143
+ logger,
144
+ saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
145
+ opts: options.options
146
+ });
147
+ const fileEncSha256B64 = fileEncSha256.toString('base64');
148
+ const [{ mediaUrl, directPath }] = await Promise.all([
149
+ (async () => {
150
+ const result = await options.upload(encFilePath, {
151
+ fileEncSha256B64,
152
+ mediaType,
153
+ timeoutMs: options.mediaUploadTimeoutMs
154
+ });
155
+ logger?.debug({ mediaType, cacheableKey }, 'uploaded media');
156
+ return result;
157
+ })(),
158
+ (async () => {
159
+ try {
160
+ if (requiresThumbnailComputation) {
161
+ const { thumbnail, originalImageDimensions } = await generateThumbnail(originalFilePath, mediaType, options);
162
+ uploadData.jpegThumbnail = thumbnail;
163
+ if (!uploadData.width && originalImageDimensions) {
164
+ uploadData.width = originalImageDimensions.width;
165
+ uploadData.height = originalImageDimensions.height;
166
+ logger?.debug('set dimensions');
167
+ }
168
+ logger?.debug('generated thumbnail');
169
+ }
170
+ if (requiresDurationComputation) {
171
+ uploadData.seconds = await getAudioDuration(originalFilePath);
172
+ logger?.debug('computed audio duration');
173
+ }
174
+ if (requiresWaveformProcessing) {
175
+ uploadData.waveform = await getAudioWaveform(originalFilePath, logger);
176
+ logger?.debug('processed waveform');
177
+ }
178
+ if (requiresAudioBackground) {
179
+ uploadData.backgroundArgb = await assertColor(options.backgroundColor);
180
+ logger?.debug('computed backgroundColor audio status');
181
+ }
182
+ }
183
+ catch (error) {
184
+ logger?.warn({ trace: error.stack }, 'failed to obtain extra info');
185
+ }
186
+ })()
187
+ ]).finally(async () => {
188
+ try {
189
+ await fs.unlink(encFilePath);
190
+ if (originalFilePath) {
191
+ await fs.unlink(originalFilePath);
192
+ }
193
+ logger?.debug('removed tmp files');
194
+ }
195
+ catch (error) {
196
+ logger?.warn('failed to remove tmp file');
197
+ }
198
+ });
199
+ const obj = WAProto.Message.fromObject({
200
+ [`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
201
+ url: mediaUrl,
202
+ directPath,
203
+ mediaKey,
204
+ fileEncSha256,
205
+ fileSha256,
206
+ fileLength,
207
+ mediaKeyTimestamp: unixTimestampSeconds(),
208
+ ...uploadData,
209
+ media: undefined
210
+ })
211
+ });
212
+ if (uploadData.ptv) {
213
+ obj.ptvMessage = obj.videoMessage;
214
+ delete obj.videoMessage;
215
+ }
216
+ if (cacheableKey) {
217
+ logger?.debug({ cacheableKey }, 'set cache');
218
+ await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish());
219
+ }
220
+ return obj;
221
+ };
222
+ export const prepareDisappearingMessageSettingContent = (ephemeralExpiration) => {
223
+ ephemeralExpiration = ephemeralExpiration || 0;
224
+ const content = {
225
+ ephemeralMessage: {
226
+ message: {
227
+ protocolMessage: {
228
+ type: WAProto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
229
+ ephemeralExpiration
230
+ }
231
+ }
232
+ }
233
+ };
234
+ return WAProto.Message.fromObject(content);
235
+ };
236
+ /**
237
+ * Generate forwarded message content like WA does
238
+ * @param message the message to forward
239
+ * @param options.forceForward will show the message as forwarded even if it is from you
240
+ */
241
+ export const generateForwardMessageContent = (message, forceForward) => {
242
+ let content = message.message;
243
+ if (!content) {
244
+ throw new Boom('no content in message', { statusCode: 400 });
245
+ }
246
+ // hacky copy
247
+ content = normalizeMessageContent(content);
248
+ content = proto.Message.decode(proto.Message.encode(content).finish());
249
+ let key = Object.keys(content)[0];
250
+ let score = content?.[key]?.contextInfo?.forwardingScore || 0;
251
+ score += message.key.fromMe && !forceForward ? 0 : 1;
252
+ if (key === 'conversation') {
253
+ content.extendedTextMessage = { text: content[key] };
254
+ delete content.conversation;
255
+ key = 'extendedTextMessage';
256
+ }
257
+ const key_ = content?.[key];
258
+ if (score > 0) {
259
+ key_.contextInfo = { forwardingScore: score, isForwarded: true };
260
+ }
261
+ else {
262
+ key_.contextInfo = {};
263
+ }
264
+ return content;
265
+ };
266
+ export const hasNonNullishProperty = (message, key) => {
267
+ return message != null &&
268
+ typeof message === 'object' &&
269
+ key in message &&
270
+ message[key] != null;
271
+ };
272
+ export const hasOptionalProperty = (obj, key) => {
273
+ return obj != null &&
274
+ typeof obj === 'object' &&
275
+ key in obj &&
276
+ obj[key] != null;
277
+ };
278
+ // Validate album message media to avoid bug
279
+ export const hasValidAlbumMedia = (message) => {
280
+ return message.imageMessage ||
281
+ message.videoMessage;
282
+ };
283
+ export const hasValidInteractiveHeader = (message) => {
284
+ return message.imageMessage ||
285
+ message.videoMessage ||
286
+ message.documentMessage ||
287
+ message.productMessage ||
288
+ message.locationMessage;
289
+ };
290
+ // Validate carousel cards header to avoid bug
291
+ export const hasValidCarouselHeader = (message) => {
292
+ return message.imageMessage ||
293
+ message.videoMessage ||
294
+ message.productMessage;
295
+ };
296
+ // Extract product message into a standalone function so it can also be reused as the header for interactive messages
297
+ const prepareProductMessage = async (message, options) => {
298
+ if (!message.businessOwnerJid) {
299
+ throw new Boom('"businessOwnerJid" is missing from the content', { statusCode: 400 });
300
+ }
301
+ const { imageMessage } = await prepareWAMessageMedia({ image: message.image || message.product.productImage }, options);
302
+ const content = {
303
+ ...message,
304
+ product: {
305
+ currencyCode: 'IDR',
306
+ priceAmount1000: 1000,
307
+ title: LIBRARY_NAME,
308
+ ...message.product,
309
+ productImage: imageMessage
310
+ }
311
+ };
312
+ delete content.image;
313
+ return content;
314
+ };
315
+ const prepareStickerPackMessage = async (message, options) => {
316
+ const { cover, stickers = [], name = '📦 Sticker Pack', publisher = 'GitHub: nuiisweety', description = '🏷️ nuiisweety/baileys' } = message;
317
+ if (stickers.length > 60) {
318
+ throw new Boom('Sticker pack exceeds the maximum limit of 60 stickers', { statusCode: 400 });
319
+ }
320
+ if (stickers.length === 0) {
321
+ throw new Boom('Sticker pack must contain at least one sticker', { statusCode: 400 });
322
+ }
323
+ if (!cover) {
324
+ throw new Boom('Sticker pack must contain a cover', { statusCode: 400 });
325
+ }
326
+ const logger = options.logger;
327
+ let cacheableKey = false;
328
+ if (Array.isArray(stickers) && stickers.length && options.mediaCache) {
329
+ const urls = [];
330
+ for (let i = 0; i < stickers.length; i++) {
331
+ const data = stickers[i].data;
332
+ if (typeof data === 'object' && data?.url) {
333
+ urls.push(data.url);
334
+ }
335
+ }
336
+ if (urls.length > 0) {
337
+ cacheableKey = 'sticker:' + urls.join('@');
338
+ }
339
+ }
340
+ if (cacheableKey) {
341
+ const mediaBuff = await options.mediaCache.get(cacheableKey);
342
+ if (mediaBuff) {
343
+ logger?.debug({ cacheableKey }, 'got media cache hit');
344
+ return proto.Message.StickerPackMessage.decode(mediaBuff);
345
+ }
346
+ }
347
+ const lib = await getImageProcessingLibrary();
348
+ const hasSharp = 'sharp' in lib && !!lib.sharp?.default;
349
+ const hasImage = 'image' in lib && !!lib.image?.Transformer;
350
+ const hasJimp = 'jimp' in lib && !!lib.jimp?.Jimp;
351
+ if (!hasSharp && !hasImage) {
352
+ throw new Boom('No image processing library (sharp or @napi-rs/image) available for converting sticker to WebP.');
353
+ }
354
+ const stickerPackIdValue = generateMessageIDV2();
355
+ const stickerData = {};
356
+ const stickerMetadata = new Array(stickers.length);
357
+ for (let i = 0; i < stickers.length; i += CONCURRENCY_LIMIT) {
358
+ const promises = [];
359
+ const chunkEnd = Math.min(i + CONCURRENCY_LIMIT, stickers.length);
360
+ for (let j = i; j < chunkEnd; j++) {
361
+ promises.push((async (index) => {
362
+ const sticker = stickers[index];
363
+ const { stream } = await getStream(sticker.data);
364
+ const buffer = await toBuffer(stream);
365
+ let webpBuffer;
366
+ let isAnimated = false;
367
+ if (isWebPBuffer(buffer)) {
368
+ webpBuffer = buffer;
369
+ isAnimated = isAnimatedWebP(buffer);
370
+ }
371
+ else if (hasSharp) {
372
+ webpBuffer = await lib.sharp.default(buffer)
373
+ .resize(512, 512, { fit: 'inside' })
374
+ .webp({ quality: 80 })
375
+ .toBuffer();
376
+ }
377
+ else {
378
+ webpBuffer = await new lib.image.Transformer(buffer)
379
+ .resize(512, 512)
380
+ .webp(80);
381
+ }
382
+ if (webpBuffer.length > 1024 * 1024) {
383
+ throw new Boom(`Sticker at index ${index} exceeds the 1MB size limit`, { statusCode: 400 });
384
+ }
385
+ const hash = sha256(webpBuffer).toString('base64').replace(/\//g, '-');
386
+ const fileName = `${hash}.webp`;
387
+ stickerData[fileName] = [new Uint8Array(webpBuffer), { level: 0 }];
388
+ stickerMetadata[index] = {
389
+ fileName,
390
+ mimetype: 'image/webp',
391
+ isAnimated,
392
+ emojis: sticker.emojis || ['✨'],
393
+ accessibilityLabel: sticker.accessibilityLabel || '‎'
394
+ };
395
+ })(j));
396
+ }
397
+ await Promise.all(promises);
398
+ }
399
+ const trayIconFileName = `${stickerPackIdValue}.webp`;
400
+ const { stream: coverStream } = await getStream(cover);
401
+ const coverBuffer = await toBuffer(coverStream);
402
+ let coverWebpBuffer;
403
+ if (isWebPBuffer(coverBuffer)) {
404
+ coverWebpBuffer = coverBuffer;
405
+ }
406
+ else if (hasSharp) {
407
+ coverWebpBuffer = await lib.sharp.default(coverBuffer)
408
+ .resize(512, 512, { fit: 'inside' })
409
+ .webp({ quality: 80 })
410
+ .toBuffer();
411
+ }
412
+ else {
413
+ coverWebpBuffer = await new lib.image.Transformer(coverBuffer)
414
+ .resize(512, 512)
415
+ .webp(80);
416
+ }
417
+ stickerData[trayIconFileName] = [new Uint8Array(coverWebpBuffer), { level: 0 }];
418
+ const zipBuffer = await new Promise((resolve, reject) => {
419
+ zip(stickerData, (error, data) => error ? reject(error) : resolve(Buffer.from(data)));
420
+ });
421
+ const stickerPackUpload = await encryptedStream(zipBuffer, 'sticker-pack', {
422
+ logger,
423
+ opts: options.options
424
+ });
425
+ let stickerPackUploadResult;
426
+ try {
427
+ stickerPackUploadResult = await options.upload(stickerPackUpload.encFilePath, {
428
+ fileEncSha256B64: stickerPackUpload.fileEncSha256.toString('base64'),
429
+ mediaType: 'sticker-pack',
430
+ timeoutMs: options.mediaUploadTimeoutMs
431
+ });
432
+ }
433
+ finally {
434
+ fs.unlink(stickerPackUpload.encFilePath).catch(() => logger?.warn('failed to remove tmp file'));
435
+ }
436
+ const obj = {
437
+ name,
438
+ publisher,
439
+ stickerPackId: stickerPackIdValue,
440
+ packDescription: description,
441
+ stickerPackOrigin: proto.Message.StickerPackMessage.StickerPackOrigin.USER_CREATED,
442
+ stickerPackSize: zipBuffer.length,
443
+ stickers: stickerMetadata,
444
+ fileSha256: stickerPackUpload.fileSha256,
445
+ fileEncSha256: stickerPackUpload.fileEncSha256,
446
+ mediaKey: stickerPackUpload.mediaKey,
447
+ directPath: stickerPackUploadResult.directPath,
448
+ fileLength: stickerPackUpload.fileLength,
449
+ mediaKeyTimestamp: unixTimestampSeconds(),
450
+ trayIconFileName
451
+ };
452
+ try {
453
+ let thumbnailBuffer;
454
+ if (hasSharp) {
455
+ thumbnailBuffer = await lib.sharp.default(coverBuffer).resize(252, 252).jpeg().toBuffer();
456
+ }
457
+ else if (hasImage) {
458
+ thumbnailBuffer = await new lib.image.Transformer(coverBuffer).resize(252, 252).jpeg();
459
+ }
460
+ else if (hasJimp) {
461
+ const jimpImage = await lib.jimp.Jimp.read(coverBuffer);
462
+ thumbnailBuffer = await jimpImage.resize({ w: 252, h: 252 }).getBuffer('image/jpeg');
463
+ }
464
+ else {
465
+ throw new Error('No image processing library available for thumbnail generation');
466
+ }
467
+ if (!thumbnailBuffer || thumbnailBuffer.length === 0) {
468
+ throw new Error('Failed to generate thumbnail buffer');
469
+ }
470
+ const thumbUpload = await encryptedStream(thumbnailBuffer, 'thumbnail-sticker-pack', {
471
+ logger,
472
+ opts: options.options,
473
+ mediaKey: stickerPackUpload.mediaKey
474
+ });
475
+ let thumbUploadResult;
476
+ try {
477
+ thumbUploadResult = await options.upload(thumbUpload.encFilePath, {
478
+ fileEncSha256B64: thumbUpload.fileEncSha256.toString('base64'),
479
+ mediaType: 'thumbnail-sticker-pack',
480
+ timeoutMs: options.mediaUploadTimeoutMs
481
+ });
482
+ }
483
+ finally {
484
+ fs.unlink(thumbUpload.encFilePath).catch(() => logger?.warn('failed to remove tmp file'));
485
+ }
486
+ Object.assign(obj, {
487
+ thumbnailDirectPath: thumbUploadResult.directPath,
488
+ thumbnailSha256: thumbUpload.fileSha256,
489
+ thumbnailEncSha256: thumbUpload.fileEncSha256,
490
+ thumbnailHeight: 252,
491
+ thumbnailWidth: 252,
492
+ imageDataHash: sha256(thumbnailBuffer).toString('base64')
493
+ });
494
+ }
495
+ catch (error) {
496
+ logger?.warn(`Thumbnail generation failed: ${error}`);
497
+ }
498
+ if (cacheableKey) {
499
+ logger?.debug({ cacheableKey }, 'set cache (background)');
500
+ options.mediaCache.set(cacheableKey, WAProto.Message.StickerPackMessage.encode(obj).finish());
501
+ }
502
+ return WAProto.Message.StickerPackMessage.fromObject(obj);
503
+ };
504
+ const prepareNativeFlowButtons = (message) => {
505
+ const buttons = message.nativeFlow;
506
+ const isButtonsFieldArray = Array.isArray(buttons);
507
+ const correctedField = isButtonsFieldArray ? buttons : buttons.buttons;
508
+ const messageParamsJson = {};
509
+ if (hasOptionalProperty(message, 'offerText') && !!message.offerText) {
510
+ Object.assign(messageParamsJson, {
511
+ limited_time_offer: {
512
+ text: message.offerText || LIBRARY_NAME,
513
+ url: message.offerUrl || DONATE_URL,
514
+ copy_code: message.offerCode,
515
+ expiration_time: message.offerExpiration
516
+ }
517
+ });
518
+ }
519
+ if (hasOptionalProperty(message, 'optionText') && !!message.optionText) {
520
+ Object.assign(messageParamsJson, {
521
+ bottom_sheet: {
522
+ in_thread_buttons_limit: 1,
523
+ divider_indices: Array.from({ length: correctedField.length }, (_, index) => index),
524
+ list_title: message.optionTitle || '📄 Select Options',
525
+ button_title: message.optionText
526
+ }
527
+ });
528
+ }
529
+ return {
530
+ buttons: correctedField.map(button => {
531
+ const buttonText = button.text || button.buttonText;
532
+ const buttonIcon = button.icon?.toUpperCase();
533
+ if (hasOptionalProperty(button, 'id') && !!button.id) {
534
+ return { name: 'quick_reply', buttonParamsJson: JSON.stringify({ display_text: buttonText || '👉🏻 Click', id: button.id, icon: buttonIcon }) };
535
+ }
536
+ else if (hasOptionalProperty(button, 'copy') && !!button.copy) {
537
+ return { name: 'cta_copy', buttonParamsJson: JSON.stringify({ display_text: buttonText || '📋 Copy', copy_code: button.copy, icon: buttonIcon }) };
538
+ }
539
+ else if (hasOptionalProperty(button, 'url') && !!button.url) {
540
+ return { name: 'cta_url', buttonParamsJson: JSON.stringify({ display_text: buttonText || '🌐 Visit', url: button.url, merchant_url: button.url, webview_interaction: button.useWebview, icon: buttonIcon }) };
541
+ }
542
+ else if (hasOptionalProperty(button, 'call') && !!button.call) {
543
+ return { name: 'cta_call', buttonParamsJson: JSON.stringify({ display_text: buttonText || '📞 Call', phone_number: button.call, icon: buttonIcon }) };
544
+ }
545
+ else if (hasOptionalProperty(button, 'sections') && !!button.sections) {
546
+ return { name: 'single_select', buttonParamsJson: JSON.stringify({ title: buttonText || '📋 Select', sections: button.sections, icon: buttonIcon }) };
547
+ }
548
+ return button;
549
+ }),
550
+ messageParamsJson: JSON.stringify(messageParamsJson),
551
+ messageVersion: 3
552
+ };
553
+ };
554
+ export const generateWAMessageContent = async (message, options) => {
555
+ var _a, _b;
556
+ let m = {};
557
+ if (hasNonNullishProperty(message, 'raw')) {
558
+ delete message.raw;
559
+ return message;
560
+ }
561
+ else if (hasNonNullishProperty(message, 'code') ||
562
+ hasNonNullishProperty(message, 'links') ||
563
+ hasNonNullishProperty(message, 'table') ||
564
+ hasNonNullishProperty(message, 'richResponse') ||
565
+ hasNonNullishProperty(message, 'latex') ||
566
+ hasNonNullishProperty(message, 'gridImage') ||
567
+ hasNonNullishProperty(message, 'inlineImage') ||
568
+ hasNonNullishProperty(message, 'dynamic') ||
569
+ hasNonNullishProperty(message, 'map') ||
570
+ hasNonNullishProperty(message, 'contentItems')) {
571
+ m = prepareRichResponseMessage(message);
572
+ }
573
+ else if (hasNonNullishProperty(message, 'text')) {
574
+ const extContent = { text: message.text };
575
+ let urlInfo = message.linkPreview;
576
+ if (typeof urlInfo === 'undefined') {
577
+ urlInfo = await generateLinkPreviewIfRequired(message.text, options.getUrlInfo, options.logger);
578
+ }
579
+ if (urlInfo) {
580
+ extContent.matchedText = urlInfo['matched-text'];
581
+ extContent.jpegThumbnail = urlInfo.jpegThumbnail;
582
+ extContent.description = urlInfo.description;
583
+ extContent.title = urlInfo.title;
584
+ extContent.previewType = 0;
585
+ const img = urlInfo.highQualityThumbnail;
586
+ if (img) {
587
+ extContent.thumbnailDirectPath = img.directPath;
588
+ extContent.mediaKey = img.mediaKey;
589
+ extContent.mediaKeyTimestamp = img.mediaKeyTimestamp;
590
+ extContent.thumbnailWidth = img.width;
591
+ extContent.thumbnailHeight = img.height;
592
+ extContent.thumbnailSha256 = img.fileSha256;
593
+ extContent.thumbnailEncSha256 = img.fileEncSha256;
594
+ }
595
+ }
596
+ if (options.backgroundColor) {
597
+ extContent.backgroundArgb = await assertColor(options.backgroundColor);
598
+ }
599
+ if (options.font) {
600
+ extContent.font = options.font;
601
+ }
602
+ m.extendedTextMessage = extContent;
603
+ }
604
+ else if (hasNonNullishProperty(message, 'contacts')) {
605
+ const contactLen = message.contacts.contacts.length;
606
+ if (!contactLen) {
607
+ throw new Boom('require atleast 1 contact', { statusCode: 400 });
608
+ }
609
+ if (contactLen === 1) {
610
+ m.contactMessage = WAProto.Message.ContactMessage.create(message.contacts.contacts[0]);
611
+ }
612
+ else {
613
+ m.contactsArrayMessage = WAProto.Message.ContactsArrayMessage.create(message.contacts);
614
+ }
615
+ }
616
+ else if (hasNonNullishProperty(message, 'location')) {
617
+ m.locationMessage = WAProto.Message.LocationMessage.create(message.location);
618
+ }
619
+ else if (hasNonNullishProperty(message, 'react')) {
620
+ if (!message.react.senderTimestampMs) {
621
+ message.react.senderTimestampMs = Date.now();
622
+ }
623
+ m.reactionMessage = WAProto.Message.ReactionMessage.create(message.react);
624
+ }
625
+ else if (hasNonNullishProperty(message, 'delete')) {
626
+ m.protocolMessage = {
627
+ key: message.delete,
628
+ type: WAProto.Message.ProtocolMessage.Type.REVOKE
629
+ };
630
+ }
631
+ else if (hasNonNullishProperty(message, 'forward')) {
632
+ m = generateForwardMessageContent(message.forward, message.force);
633
+ }
634
+ else if (hasNonNullishProperty(message, 'disappearingMessagesInChat')) {
635
+ const exp = typeof message.disappearingMessagesInChat === 'boolean'
636
+ ? message.disappearingMessagesInChat
637
+ ? WA_DEFAULT_EPHEMERAL
638
+ : 0
639
+ : message.disappearingMessagesInChat;
640
+ m = prepareDisappearingMessageSettingContent(exp);
641
+ }
642
+ else if (hasNonNullishProperty(message, 'groupInvite')) {
643
+ m.groupInviteMessage = {};
644
+ m.groupInviteMessage.inviteCode = message.groupInvite.inviteCode;
645
+ m.groupInviteMessage.inviteExpiration = message.groupInvite.inviteExpiration;
646
+ m.groupInviteMessage.caption = message.groupInvite.text;
647
+ m.groupInviteMessage.groupJid = message.groupInvite.jid;
648
+ m.groupInviteMessage.groupName = message.groupInvite.subject;
649
+ //TODO: use built-in interface and get disappearing mode info etc.
650
+ //TODO: cache / use store!?
651
+ if (options.getProfilePicUrl) {
652
+ const pfpUrl = await options.getProfilePicUrl(message.groupInvite.jid, 'preview');
653
+ if (pfpUrl) {
654
+ const resp = await fetch(pfpUrl, { method: 'GET', dispatcher: options?.options?.dispatcher });
655
+ if (resp.ok) {
656
+ const buf = Buffer.from(await resp.arrayBuffer());
657
+ m.groupInviteMessage.jpegThumbnail = buf;
658
+ }
659
+ }
660
+ }
661
+ }
662
+ else if (hasNonNullishProperty(message, 'stickers')) {
663
+ m.stickerPackMessage = await prepareStickerPackMessage(message, options);
664
+ }
665
+ else if (hasNonNullishProperty(message, 'pin')) {
666
+ m.pinInChatMessage = {};
667
+ m.messageContextInfo = {};
668
+ m.pinInChatMessage.key = message.pin;
669
+ m.pinInChatMessage.type = message.type;
670
+ m.pinInChatMessage.senderTimestampMs = Date.now();
671
+ m.messageContextInfo.messageAddOnDurationInSecs = message.type === 1 ? message.time || 86400 : 0;
672
+ }
673
+ else if (hasNonNullishProperty(message, 'keep')) {
674
+ m.keepInChatMessage = {};
675
+ m.keepInChatMessage.key = message.keep;
676
+ m.keepInChatMessage.keepType = message.type;
677
+ m.keepInChatMessage.timestampMs = Date.now();
678
+ }
679
+ else if (hasNonNullishProperty(message, 'flowReply')) {
680
+ m.interactiveResponseMessage = {
681
+ body: {
682
+ format: message.flowReply.format || proto.Message.InteractiveResponseMessage.Body.Format.DEFAULT,
683
+ text: message.flowReply.text
684
+ },
685
+ nativeFlowResponseMessage: {
686
+ name: message.flowReply.name,
687
+ paramsJson: message.flowReply.paramsJson || '{}',
688
+ version: message.flowReply.version || 1
689
+ }
690
+ };
691
+ }
692
+ else if (hasNonNullishProperty(message, 'buttonReply')) {
693
+ switch (message.type) {
694
+ case 'template':
695
+ m.templateButtonReplyMessage = {
696
+ selectedDisplayText: message.buttonReply.displayText,
697
+ selectedId: message.buttonReply.id,
698
+ selectedIndex: message.buttonReply.index
699
+ };
700
+ break;
701
+ case 'plain':
702
+ m.buttonsResponseMessage = {
703
+ selectedButtonId: message.buttonReply.id,
704
+ selectedDisplayText: message.buttonReply.displayText,
705
+ type: proto.Message.ButtonsResponseMessage.Type.DISPLAY_TEXT
706
+ };
707
+ break;
708
+ }
709
+ }
710
+ else if (hasOptionalProperty(message, 'ptv') && message.ptv) {
711
+ const { videoMessage } = await prepareWAMessageMedia({ video: message.video }, options);
712
+ m.ptvMessage = videoMessage;
713
+ }
714
+ else if (hasNonNullishProperty(message, 'product')) {
715
+ if (!message.businessOwnerJid) {
716
+ throw new Boom('"businessOwnerJid" is missing from the content', { statusCode: 400 });
717
+ }
718
+ const { imageMessage } = await prepareWAMessageMedia({ image: message.image || message.product.productImage }, options);
719
+ const content = {
720
+ ...message,
721
+ product: {
722
+ currencyCode: 'IDR',
723
+ priceAmount1000: 1000,
724
+ title: LIBRARY_NAME,
725
+ ...message.product,
726
+ productImage: imageMessage
727
+ }
728
+ };
729
+ delete content.image;
730
+ m.productMessage = WAProto.Message.ProductMessage.create(content);
731
+ }
732
+ else if (hasNonNullishProperty(message, 'listReply')) {
733
+ m.listResponseMessage = {
734
+ description: message.listReply.description,
735
+ listType: proto.Message.ListResponseMessage.ListType.SINGLE_SELECT,
736
+ singleSelectReply: {
737
+ selectedRowId: message.listReply.id
738
+ },
739
+ title: message.listReply.title
740
+ };
741
+ }
742
+ else if (hasNonNullishProperty(message, 'event')) {
743
+ m.eventMessage = {};
744
+ const startTime = Math.floor(message.event.startDate.getTime() / 1000);
745
+ if (message.event.call && options.getCallLink) {
746
+ const token = await options.getCallLink(message.event.call, { startTime });
747
+ m.eventMessage.joinLink = (message.event.call === 'audio' ? CALL_AUDIO_PREFIX : CALL_VIDEO_PREFIX) + token;
748
+ }
749
+ m.messageContextInfo = {
750
+ // encKey
751
+ messageSecret: message.event.messageSecret || randomBytes(32)
752
+ };
753
+ m.eventMessage.name = message.event.name;
754
+ m.eventMessage.description = message.event.description;
755
+ m.eventMessage.startTime = startTime;
756
+ m.eventMessage.endTime = message.event.endDate ? message.event.endDate.getTime() / 1000 : undefined;
757
+ m.eventMessage.isCanceled = message.event.isCancelled ?? false;
758
+ m.eventMessage.extraGuestsAllowed = message.event.extraGuestsAllowed;
759
+ m.eventMessage.isScheduleCall = message.event.isScheduleCall ?? false;
760
+ m.eventMessage.location = message.event.location;
761
+ }
762
+ else if (hasNonNullishProperty(message, 'poll')) {
763
+ (_a = message.poll).selectableCount || (_a.selectableCount = 0);
764
+ (_b = message.poll).toAnnouncementGroup || (_b.toAnnouncementGroup = false);
765
+ if (!Array.isArray(message.poll.values)) {
766
+ throw new Boom('Invalid poll values', { statusCode: 400 });
767
+ }
768
+ if (message.poll.selectableCount < 0 || message.poll.selectableCount > message.poll.values.length) {
769
+ throw new Boom(`poll.selectableCount in poll should be >= 0 and <= ${message.poll.values.length}`, {
770
+ statusCode: 400
771
+ });
772
+ }
773
+ const pollCreationMessage = {
774
+ name: message.poll.name,
775
+ selectableOptionsCount: message.poll.selectableCount,
776
+ options: message.poll.values.map(optionName => ({ optionName })),
777
+ endTime: message.poll.endDate ? message.poll.endDate.getTime() : undefined,
778
+ hideParticipantName: message.poll.hideVoter ?? false,
779
+ allowAddOption: message.poll.canAddOption ?? false
780
+ };
781
+ if (message.poll.toAnnouncementGroup) {
782
+ // poll v2 is for community announcement groups (single select and multiple)
783
+ m.pollCreationMessageV2 = pollCreationMessage;
784
+ }
785
+ else {
786
+ // Add quiz message support
787
+ if (message.poll.pollType === 1) {
788
+ if (!message.poll.correctAnswer) {
789
+ throw new Boom('No "correctAnswer" provided for quiz', { statusCode: 400 });
790
+ }
791
+ m.pollCreationMessageV5 = {
792
+ // quiz for newsletter only
793
+ ...pollCreationMessage,
794
+ correctAnswer: {
795
+ optionName: message.poll.correctAnswer.toString()
796
+ },
797
+ pollType: 1,
798
+ selectableOptionsCount: 1
799
+ };
800
+ }
801
+ else if (message.poll.selectableCount === 1) {
802
+ //poll v3 is for single select polls
803
+ m.pollCreationMessageV3 = pollCreationMessage;
804
+ }
805
+ else {
806
+ // poll for multiple choice polls
807
+ m.pollCreationMessage = pollCreationMessage;
808
+ }
809
+ }
810
+ m.messageContextInfo = {
811
+ // encKey
812
+ messageSecret: message.poll.messageSecret || randomBytes(32)
813
+ };
814
+ }
815
+ else if (hasNonNullishProperty(message, 'pollResult')) {
816
+ const pollResultSnapshotMessage = {
817
+ name: message.pollResult.name,
818
+ pollVotes: message.pollResult.votes.map(vote => ({
819
+ optionName: vote.name,
820
+ optionVoteCount: parseInt(vote.voteCount)
821
+ }))
822
+ };
823
+ if (message.pollResult.pollType === 1) {
824
+ pollResultSnapshotMessage.pollType = proto.Message.PollType.QUIZ;
825
+ m.pollResultSnapshotMessageV3 = pollResultSnapshotMessage;
826
+ }
827
+ else {
828
+ pollResultSnapshotMessage.pollType = proto.Message.PollType.POLL;
829
+ m.pollResultSnapshotMessage = pollResultSnapshotMessage;
830
+ }
831
+ }
832
+ else if (hasNonNullishProperty(message, 'pollUpdate')) {
833
+ if (!message.pollUpdate.key) {
834
+ throw new Boom('Message key is required', { statusCode: 400 });
835
+ }
836
+ if (!message.pollUpdate.vote) {
837
+ throw new Boom('Encrypted vote payload is required', { statusCode: 400 });
838
+ }
839
+ m.pollUpdateMessage = {
840
+ metadata: message.pollUpdate.metadata,
841
+ pollCreationMessageKey: message.pollUpdate.key,
842
+ senderTimestampMs: Date.now(),
843
+ vote: message.pollUpdate.vote
844
+ };
845
+ }
846
+ else if (hasNonNullishProperty(message, 'paymentInviteServiceType')) {
847
+ m.paymentInviteMessage = {
848
+ expiryTimestamp: Date.now(),
849
+ serviceType: message.paymentInviteServiceType
850
+ };
851
+ }
852
+ else if (hasNonNullishProperty(message, 'orderText')) {
853
+ if (!Buffer.isBuffer(message.thumbnail)) {
854
+ throw new Boom('Must provide thumbnail buffer in order message', { statusCode: 400 });
855
+ }
856
+ m.orderMessage = {
857
+ itemCount: 1,
858
+ messageVersion: 1,
859
+ orderTitle: LIBRARY_NAME,
860
+ status: proto.Message.OrderMessage.OrderStatus.INQUIRY,
861
+ surface: proto.Message.OrderMessage.OrderSurface.CATALOG,
862
+ token: generateMessageIDV2(),
863
+ totalAmount1000: 1000,
864
+ totalCurrencyCode: 'IDR',
865
+ ...message,
866
+ message: message.orderText
867
+ };
868
+ delete m.orderMessage.orderText;
869
+ }
870
+ else if (hasNonNullishProperty(message, 'album')) {
871
+ if (!Array.isArray(message.album)) {
872
+ throw new Boom('Invalid album type. Expected an array.', { statusCode: 400 });
873
+ }
874
+ let videoCount = 0;
875
+ for (let i = 0; i < message.album.length; i++) {
876
+ if (message.album[i].video)
877
+ videoCount++;
878
+ }
879
+ ;
880
+ let imageCount = 0;
881
+ for (let i = 0; i < message.album.length; i++) {
882
+ if (message.album[i].image)
883
+ imageCount++;
884
+ }
885
+ ;
886
+ if ((videoCount + imageCount) < 2) {
887
+ throw new Boom('Minimum provide 2 media to upload album message', { statusCode: 400 });
888
+ }
889
+ m.albumMessage = {
890
+ expectedImageCount: imageCount,
891
+ expectedVideoCount: videoCount
892
+ };
893
+ }
894
+ else if (hasNonNullishProperty(message, 'sharePhoneNumber')) {
895
+ m.protocolMessage = {
896
+ type: proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER
897
+ };
898
+ }
899
+ else if (hasNonNullishProperty(message, 'requestPhoneNumber')) {
900
+ m.requestPhoneNumberMessage = {};
901
+ }
902
+ else if (hasNonNullishProperty(message, 'limitSharing')) {
903
+ m.protocolMessage = {
904
+ type: proto.Message.ProtocolMessage.Type.LIMIT_SHARING,
905
+ limitSharing: {
906
+ sharingLimited: message.limitSharing === true,
907
+ trigger: 1,
908
+ limitSharingSettingTimestamp: Date.now(),
909
+ initiatedByMe: true
910
+ }
911
+ };
912
+ }
913
+ else {
914
+ m = await prepareWAMessageMedia(message, options);
915
+ }
916
+ if (hasNonNullishProperty(message, 'buttons')) {
917
+ const buttonsMessage = {
918
+ buttons: message.buttons.map(button => {
919
+ const buttonText = button.text || button.buttonText;
920
+ if (hasOptionalProperty(button, 'sections')) {
921
+ return {
922
+ nativeFlowInfo: {
923
+ name: 'single_select',
924
+ paramsJson: JSON.stringify({
925
+ title: buttonText,
926
+ sections: button.sections
927
+ })
928
+ },
929
+ type: ButtonType.NATIVE_FLOW
930
+ };
931
+ }
932
+ else if (hasOptionalProperty(button, 'name')) {
933
+ return {
934
+ nativeFlowInfo: {
935
+ name: button.name,
936
+ paramsJson: button.paramsJson
937
+ },
938
+ type: ButtonType.NATIVE_FLOW
939
+ };
940
+ }
941
+ return {
942
+ buttonId: button.id || button.buttonId,
943
+ buttonText: typeof buttonText === 'string' ? { displayText: buttonText } : buttonText,
944
+ type: button.type || ButtonType.RESPONSE
945
+ };
946
+ })
947
+ };
948
+ if (hasOptionalProperty(message, 'text')) {
949
+ buttonsMessage.contentText = message.text;
950
+ buttonsMessage.headerType = ButtonHeaderType.EMPTY;
951
+ }
952
+ else {
953
+ if (hasOptionalProperty(message, 'caption')) {
954
+ buttonsMessage.contentText = message.caption;
955
+ }
956
+ const type = Object.keys(m)[0].replace('Message', '').toUpperCase();
957
+ buttonsMessage.headerType = ButtonHeaderType[type];
958
+ Object.assign(buttonsMessage, m);
959
+ }
960
+ if (hasOptionalProperty(message, 'footer')) {
961
+ buttonsMessage.footerText = message.footer;
962
+ }
963
+ m = { buttonsMessage };
964
+ }
965
+ else if (hasNonNullishProperty(message, 'sections')) {
966
+ const listMessage = {
967
+ sections: message.sections,
968
+ buttonText: message.buttonText,
969
+ title: message.title,
970
+ footerText: message.footer,
971
+ description: message.text,
972
+ listType: ListType.SINGLE_SELECT
973
+ };
974
+ m = { listMessage };
975
+ }
976
+ else if (hasNonNullishProperty(message, 'templateButtons')) {
977
+ const hydratedTemplate = {
978
+ hydratedButtons: message.templateButtons.map((button, i) => {
979
+ const buttonText = button.text || button.buttonText;
980
+ if (hasOptionalProperty(button, 'id')) {
981
+ return {
982
+ index: i,
983
+ quickReplyButton: {
984
+ displayText: buttonText || '👉🏻 Click',
985
+ id: button.id
986
+ }
987
+ };
988
+ }
989
+ else if (hasOptionalProperty(button, 'url')) {
990
+ return {
991
+ index: i,
992
+ urlButton: {
993
+ displayText: buttonText || '🌐 Visit',
994
+ url: button.url
995
+ }
996
+ };
997
+ }
998
+ else if (hasOptionalProperty(button, 'call')) {
999
+ return {
1000
+ index: i,
1001
+ callButton: {
1002
+ displayText: buttonText || '📞 Call',
1003
+ phoneNumber: button.call
1004
+ }
1005
+ };
1006
+ }
1007
+ button.index = button.index || i;
1008
+ return button;
1009
+ })
1010
+ };
1011
+ if (hasOptionalProperty(message, 'text')) {
1012
+ hydratedTemplate.hydratedContentText = message.text;
1013
+ }
1014
+ else {
1015
+ if (hasOptionalProperty(message, 'caption')) {
1016
+ hydratedTemplate.hydratedTitleText = message.title;
1017
+ hydratedTemplate.hydratedContentText = message.caption;
1018
+ }
1019
+ ;
1020
+ Object.assign(hydratedTemplate, m);
1021
+ }
1022
+ if (hasOptionalProperty(message, 'footer')) {
1023
+ hydratedTemplate.hydratedFooterText = message.footer;
1024
+ }
1025
+ hydratedTemplate.templateId = message.id || 'template-' + Date.now();
1026
+ m = {
1027
+ templateMessage: {
1028
+ hydratedFourRowTemplate: hydratedTemplate,
1029
+ hydratedTemplate: hydratedTemplate
1030
+ }
1031
+ };
1032
+ }
1033
+ else if (hasNonNullishProperty(message, 'nativeFlow')) {
1034
+ const interactiveMessage = {
1035
+ nativeFlowMessage: prepareNativeFlowButtons(message)
1036
+ };
1037
+ if (hasOptionalProperty(message, 'bizJid')) {
1038
+ interactiveMessage.collectionMessage = {
1039
+ bizJid: message.bizJid,
1040
+ id: message.id,
1041
+ messageVersion: 1
1042
+ };
1043
+ }
1044
+ else if (hasOptionalProperty(message, 'shopSurface')) {
1045
+ interactiveMessage.shopStorefrontMessage = {
1046
+ surface: message.shopSurface,
1047
+ id: message.id,
1048
+ messageVersion: 1
1049
+ };
1050
+ }
1051
+ if (hasOptionalProperty(message, 'text')) {
1052
+ interactiveMessage.body = { text: message.text };
1053
+ }
1054
+ else {
1055
+ if (hasOptionalProperty(message, 'caption')) {
1056
+ const isValidHeader = hasValidInteractiveHeader(m);
1057
+ if (!isValidHeader) {
1058
+ throw new Boom('Invalid media type for interactive message header', { statusCode: 400 });
1059
+ }
1060
+ interactiveMessage.header = {
1061
+ title: message.title || '',
1062
+ subtitle: message.subtitle || '',
1063
+ hasMediaAttachment: isValidHeader
1064
+ };
1065
+ interactiveMessage.body = { text: message.caption };
1066
+ }
1067
+ if (hasOptionalProperty(message, 'thumbnail') && !!message.thumbnail) {
1068
+ interactiveMessage.jpegThumbnail = message.thumbnail;
1069
+ }
1070
+ Object.assign(interactiveMessage.header, m);
1071
+ }
1072
+ if (hasOptionalProperty(message, 'audioFooter')) {
1073
+ const { audioMessage } = await prepareWAMessageMedia({
1074
+ audio: message.audioFooter
1075
+ }, options);
1076
+ interactiveMessage.footer = {
1077
+ audioMessage,
1078
+ hasMediaAttachment: true
1079
+ };
1080
+ }
1081
+ else if (hasOptionalProperty(message, 'footer')) {
1082
+ interactiveMessage.footer = { text: message.footer };
1083
+ }
1084
+ m = { interactiveMessage };
1085
+ }
1086
+ else if (hasNonNullishProperty(message, 'cards')) {
1087
+ const interactiveMessage = {
1088
+ carouselMessage: {
1089
+ cards: await Promise.all(message.cards.map(async (card) => {
1090
+ let carouselHeader = {};
1091
+ if (hasNonNullishProperty(card, 'product')) {
1092
+ carouselHeader.productMessage = await prepareProductMessage(card, options);
1093
+ }
1094
+ else {
1095
+ carouselHeader = await prepareWAMessageMedia(card, options).catch(() => ({}));
1096
+ }
1097
+ const isValidHeader = hasValidCarouselHeader(carouselHeader);
1098
+ if (!isValidHeader) {
1099
+ throw new Boom('Invalid media type for carousel card', { statusCode: 400 });
1100
+ }
1101
+ const carouselCard = {
1102
+ nativeFlowMessage: prepareNativeFlowButtons(card.nativeFlow ? card : [])
1103
+ };
1104
+ if (hasOptionalProperty(card, 'text')) {
1105
+ carouselCard.body = { text: card.text };
1106
+ }
1107
+ else {
1108
+ if (hasOptionalProperty(card, 'caption')) {
1109
+ carouselCard.header = {
1110
+ title: card.title || '',
1111
+ subtitle: card.subtitle || '',
1112
+ hasMediaAttachment: isValidHeader
1113
+ };
1114
+ carouselCard.body = { text: card.caption };
1115
+ }
1116
+ if (hasOptionalProperty(card, 'thumbnail') && !!card.thumbnail) {
1117
+ carouselCard.jpegThumbnail = card.thumbnail;
1118
+ }
1119
+ Object.assign(carouselCard.header, carouselHeader);
1120
+ }
1121
+ if (hasOptionalProperty(card, 'audioFooter')) {
1122
+ const { audioMessage } = await prepareWAMessageMedia({
1123
+ audio: card.audioFooter
1124
+ }, options);
1125
+ carouselCard.footer = {
1126
+ audioMessage,
1127
+ hasMediaAttachment: true
1128
+ };
1129
+ }
1130
+ else if (hasOptionalProperty(card, 'footer')) {
1131
+ carouselCard.footer = { text: card.footer };
1132
+ }
1133
+ return carouselCard;
1134
+ })),
1135
+ carouselCardType: CarouselCardType.UNKNOWN,
1136
+ messageVersion: 1
1137
+ }
1138
+ };
1139
+ if (hasOptionalProperty(message, 'text')) {
1140
+ interactiveMessage.body = { text: message.text };
1141
+ }
1142
+ if (hasOptionalProperty(message, 'footer')) {
1143
+ interactiveMessage.footer = { text: message.footer };
1144
+ }
1145
+ m = { interactiveMessage };
1146
+ }
1147
+ else if (hasNonNullishProperty(message, 'requestPaymentFrom')) {
1148
+ const requestPaymentMessage = {
1149
+ amount: {
1150
+ currencyCode: 'IDR',
1151
+ offset: 1000,
1152
+ value: 1000
1153
+ },
1154
+ amount1000: 1000,
1155
+ currencyCodeIso4217: 'IDR',
1156
+ expiryTimestamp: Date.now(),
1157
+ noteMessage: m,
1158
+ requestFrom: message.requestPaymentFrom,
1159
+ ...message
1160
+ };
1161
+ delete requestPaymentMessage.requestPaymentFrom;
1162
+ if (hasNonNullishProperty(m, 'extendedTextMessage') || hasNonNullishProperty(m, 'stickerMessage')) {
1163
+ Object.assign(requestPaymentMessage.noteMessage, m);
1164
+ }
1165
+ else {
1166
+ throw new Boom('Invalid message type for request payment note message', { statusCode: 400 });
1167
+ }
1168
+ m = { requestPaymentMessage };
1169
+ }
1170
+ else if (hasNonNullishProperty(message, 'invoiceNote')) {
1171
+ const attachment = m.imageMessage || m.documentMessage;
1172
+ const type = Object.keys(m)[0].replace('Message', '').toUpperCase();
1173
+ const invoiceMessage = {
1174
+ attachmentType: proto.Message.InvoiceMessage.AttachmentType[type === 'DOCUMENT' ? 'PDF' : 'IMAGE'],
1175
+ note: message.invoiceNote
1176
+ };
1177
+ if (attachment) {
1178
+ const { directPath, fileEncSha256, fileSha256, jpegThumbnail = undefined, mediaKey, mediaKeyTimestamp, mimetype } = attachment;
1179
+ Object.assign(invoiceMessage, {
1180
+ attachmentDirectPath: directPath,
1181
+ attachmentFileEncSha256: fileEncSha256,
1182
+ attachmentFileSha256: fileSha256,
1183
+ attachmentJpegThumbnail: jpegThumbnail,
1184
+ attachmentMediaKey: mediaKey,
1185
+ attachmentMediaKeyTimestamp: mediaKeyTimestamp,
1186
+ attachmentMimetype: mimetype,
1187
+ token: generateMessageIDV2()
1188
+ });
1189
+ }
1190
+ else {
1191
+ throw new Boom('Invalid media type for invoice message', { statusCode: 400 });
1192
+ }
1193
+ m = { invoiceMessage };
1194
+ }
1195
+ if (hasOptionalProperty(message, 'externalAdReply') && !!message.externalAdReply) {
1196
+ const messageType = Object.keys(m)[0];
1197
+ const key = m[messageType];
1198
+ const content = message.externalAdReply;
1199
+ if ('thumbnail' in content && !Buffer.isBuffer(content.thumbnail)) {
1200
+ throw new Boom('Thumbnail must in buffer type', { statusCode: 400 });
1201
+ }
1202
+ if (!content.url || typeof content.url !== 'string') {
1203
+ content.url = DONATE_URL;
1204
+ }
1205
+ const externalAdReply = {
1206
+ ...content,
1207
+ body: content.body,
1208
+ mediaType: content.mediaType || 1,
1209
+ mediaUrl: content.url,
1210
+ renderLargerThumbnail: content.largeThumbnail,
1211
+ sourceUrl: content.url,
1212
+ thumbnail: content.thumbnail,
1213
+ thumbnailUrl: content.url + '?update=' + Date.now(),
1214
+ title: content.title || LIBRARY_NAME
1215
+ };
1216
+ delete externalAdReply.subTitle;
1217
+ delete externalAdReply.largeThumbnail;
1218
+ delete externalAdReply.url;
1219
+ if ('contextInfo' in key && !!key.contextInfo) {
1220
+ key.contextInfo.externalAdReply = { ...key.contextInfo.externalAdReply, ...externalAdReply };
1221
+ }
1222
+ else if (key) {
1223
+ key.contextInfo = { externalAdReply };
1224
+ }
1225
+ }
1226
+ if ((hasOptionalProperty(message, 'mentions') && message.mentions?.length) ||
1227
+ (hasOptionalProperty(message, 'mentionAll') && message.mentionAll)) {
1228
+ const messageType = Object.keys(m)[0];
1229
+ const key = m[messageType];
1230
+ if (key && 'contextInfo' in key) {
1231
+ key.contextInfo = key.contextInfo || {};
1232
+ if (message.mentions?.length) {
1233
+ key.contextInfo.mentionedJid = message.mentions;
1234
+ }
1235
+ if (message.mentionAll) {
1236
+ key.contextInfo.nonJidMentions = 1;
1237
+ }
1238
+ }
1239
+ else if (key) {
1240
+ key.contextInfo = {
1241
+ mentionedJid: message.mentions,
1242
+ nonJidMentions: message.mentionAll ? 1 : 0
1243
+ };
1244
+ }
1245
+ }
1246
+ if (hasOptionalProperty(message, 'contextInfo') && !!message.contextInfo) {
1247
+ const messageType = Object.keys(m)[0];
1248
+ const key = m[messageType];
1249
+ if ('contextInfo' in key && !!key.contextInfo) {
1250
+ key.contextInfo = { ...key.contextInfo, ...message.contextInfo };
1251
+ }
1252
+ else if (key) {
1253
+ key.contextInfo = message.contextInfo;
1254
+ }
1255
+ }
1256
+ if (hasOptionalProperty(message, 'groupStatus') && !!message.groupStatus) {
1257
+ const messageType = Object.keys(m)[0];
1258
+ const key = m[messageType];
1259
+ if ('contextInfo' in key && !!key.contextInfo) {
1260
+ key.contextInfo.isGroupStatus = message.groupStatus;
1261
+ }
1262
+ else if (key) {
1263
+ key.contextInfo = {
1264
+ isGroupStatus: message.groupStatus
1265
+ };
1266
+ }
1267
+ m = { groupStatusMessageV2: { message: m } };
1268
+ delete message.groupStatus;
1269
+ }
1270
+ if (hasOptionalProperty(message, 'spoiler') && !!message.spoiler) {
1271
+ const messageType = Object.keys(m)[0];
1272
+ const key = m[messageType];
1273
+ if ('contextInfo' in key && !!key.contextInfo) {
1274
+ key.contextInfo.isSpoiler = message.spoiler;
1275
+ }
1276
+ else if (key) {
1277
+ key.contextInfo = {
1278
+ isSpoiler: message.spoiler
1279
+ };
1280
+ }
1281
+ m = { spoilerMessage: { message: m } };
1282
+ delete message.spoiler;
1283
+ }
1284
+ else if (hasOptionalProperty(message, 'interactiveAsTemplate') && !!message.interactiveAsTemplate) {
1285
+ if (!m.interactiveMessage) {
1286
+ throw new Boom('Invalid message type for template', { statusCode: 400 });
1287
+ }
1288
+ m = {
1289
+ templateMessage: {
1290
+ interactiveMessageTemplate: m.interactiveMessage,
1291
+ templateId: message.id || 'template-' + Date.now()
1292
+ }
1293
+ };
1294
+ delete message.interactiveAsTemplate;
1295
+ }
1296
+ if (hasOptionalProperty(message, 'ephemeral') && !!message.ephemeral) {
1297
+ m = { ephemeralMessage: { message: m } };
1298
+ delete message.ephemeral;
1299
+ }
1300
+ if (hasOptionalProperty(message, 'isLottie') && !!message.isLottie) {
1301
+ m = { lottieStickerMessage: { message: m } };
1302
+ }
1303
+ else if (hasOptionalProperty(message, 'viewOnce') && !!message.viewOnce) {
1304
+ m = { viewOnceMessage: { message: m } };
1305
+ }
1306
+ else if (hasOptionalProperty(message, 'viewOnceV2') && !!message.viewOnceV2) {
1307
+ m = { viewOnceMessageV2: { message: m } };
1308
+ delete message.viewOnceV2;
1309
+ }
1310
+ else if (hasOptionalProperty(message, 'viewOnceV2Extension') && !!message.viewOnceV2Extension) {
1311
+ m = { viewOnceMessageV2Extension: { message: m } };
1312
+ delete message.viewOnceV2Extension;
1313
+ }
1314
+ if (hasOptionalProperty(message, 'edit')) {
1315
+ m = {
1316
+ protocolMessage: {
1317
+ key: message.edit,
1318
+ editedMessage: m,
1319
+ timestampMs: Date.now(),
1320
+ type: WAProto.Message.ProtocolMessage.Type.MESSAGE_EDIT
1321
+ }
1322
+ };
1323
+ }
1324
+ if (shouldIncludeReportingToken(m)) {
1325
+ m.messageContextInfo = m.messageContextInfo || {};
1326
+ if (!m.messageContextInfo.messageSecret) {
1327
+ m.messageContextInfo.messageSecret = randomBytes(32);
1328
+ }
1329
+ }
1330
+ return WAProto.Message.create(m);
1331
+ };
1332
+ export const generateWAMessageFromContent = (jid, message, options) => {
1333
+ // set timestamp to now
1334
+ // if not specified
1335
+ if (!options.timestamp) {
1336
+ options.timestamp = new Date();
1337
+ }
1338
+ const innerMessage = normalizeMessageContent(message);
1339
+ const messageContextInfo = message.messageContextInfo;
1340
+ const key = getContentType(innerMessage);
1341
+ const timestamp = unixTimestampSeconds(options.timestamp);
1342
+ const isNewsletter = isJidNewsletter(jid);
1343
+ const { quoted, userJid } = options;
1344
+ if (quoted) {
1345
+ const participant = quoted.key.fromMe
1346
+ ? userJid // TODO: Add support for LIDs
1347
+ : quoted.participant || quoted.key.participant || quoted.key.remoteJid;
1348
+ let quotedMsg = normalizeMessageContent(quoted.message);
1349
+ const msgType = getContentType(quotedMsg);
1350
+ // strip any redundant properties
1351
+ quotedMsg = proto.Message.create({ [msgType]: quotedMsg[msgType] });
1352
+ const quotedContent = quotedMsg[msgType];
1353
+ if (typeof quotedContent === 'object' && quotedContent && 'contextInfo' in quotedContent) {
1354
+ delete quotedContent.contextInfo;
1355
+ }
1356
+ const contextInfo = ('contextInfo' in innerMessage[key] && innerMessage[key]?.contextInfo) || {};
1357
+ contextInfo.participant = jidNormalizedUser(participant);
1358
+ contextInfo.stanzaId = quoted.key.id;
1359
+ contextInfo.quotedMessage = quotedMsg;
1360
+ // if a participant is quoted, then it must be a group
1361
+ // hence, remoteJid of group must also be entered
1362
+ if (!isNewsletter && jid !== quoted.key.remoteJid) {
1363
+ contextInfo.remoteJid = quoted.key.remoteJid;
1364
+ }
1365
+ if (contextInfo && innerMessage[key]) {
1366
+ /* @ts-ignore */
1367
+ innerMessage[key].contextInfo = contextInfo;
1368
+ }
1369
+ }
1370
+ if (
1371
+ // if we want to send a disappearing message
1372
+ !!options?.ephemeralExpiration &&
1373
+ // and it's not a protocol message -- delete, toggle disappear message
1374
+ key !== 'protocolMessage' &&
1375
+ // already not converted to disappearing message
1376
+ key !== 'ephemeralMessage' &&
1377
+ // newsletters don't support ephemeral messages
1378
+ !isNewsletter) {
1379
+ /* @ts-ignore */
1380
+ innerMessage[key].contextInfo = {
1381
+ ...(innerMessage[key].contextInfo || {}),
1382
+ expiration: options.ephemeralExpiration || WA_DEFAULT_EPHEMERAL
1383
+ //ephemeralSettingTimestamp: options.ephemeralOptions.eph_setting_ts?.toString()
1384
+ };
1385
+ }
1386
+ if (messageContextInfo?.messageSecret && (isPnUser(jid) || isLidUser(jid))) {
1387
+ messageContextInfo.deviceListMetadata = {
1388
+ recipientKeyHash: randomBytes(10),
1389
+ recipientTimestamp: unixTimestampSeconds()
1390
+ };
1391
+ messageContextInfo.deviceListMetadataVersion = 2;
1392
+ }
1393
+ message = WAProto.Message.create(message);
1394
+ const messageJSON = {
1395
+ key: {
1396
+ remoteJid: jid,
1397
+ fromMe: true,
1398
+ id: options?.messageId || generateMessageIDV2()
1399
+ },
1400
+ message: message,
1401
+ messageTimestamp: timestamp,
1402
+ messageStubParameters: [],
1403
+ participant: isJidGroup(jid) || isJidStatusBroadcast(jid) ? userJid : undefined, // TODO: Add support for LIDs
1404
+ status: WAMessageStatus.PENDING
1405
+ };
1406
+ return WAProto.WebMessageInfo.fromObject(messageJSON);
1407
+ };
1408
+ export const generateWAMessage = async (jid, content, options) => {
1409
+ // ensure msg ID is with every log
1410
+ options.logger = options?.logger?.child({ msgId: options.messageId });
1411
+ // Pass jid in the options to generateWAMessageContent
1412
+ if (jid) {
1413
+ options.jid = jid;
1414
+ }
1415
+ return generateWAMessageFromContent(jid, await generateWAMessageContent(content, options), options);
1416
+ };
1417
+ /** Get the key to access the true type of content */
1418
+ export const getContentType = (content) => {
1419
+ if (content) {
1420
+ const keys = Object.keys(content);
1421
+ const key = keys.find(k => (k === 'conversation' || k.includes('Message')) && k !== 'senderKeyDistributionMessage');
1422
+ return key;
1423
+ }
1424
+ };
1425
+ export const normalizeMessageContent = (content) => {
1426
+ if (!content) {
1427
+ return undefined;
1428
+ }
1429
+ // set max iterations to prevent an infinite loop
1430
+ for (let i = 0; i < 5; i++) {
1431
+ const inner = getFutureProofMessage(content);
1432
+ if (!inner) {
1433
+ break;
1434
+ }
1435
+ content = inner.message;
1436
+ }
1437
+ return content;
1438
+ function getFutureProofMessage(message) {
1439
+ return (message?.associatedChildMessage ||
1440
+ message?.botForwardedMessage ||
1441
+ message?.botInvokeMessage ||
1442
+ message?.botTaskMessage ||
1443
+ message?.documentWithCaptionMessage ||
1444
+ message?.editedMessage ||
1445
+ message?.ephemeralMessage ||
1446
+ message?.eventCoverImage ||
1447
+ message?.groupMentionedMessage ||
1448
+ message?.groupStatusMentionMessage ||
1449
+ message?.groupStatusMessage ||
1450
+ message?.groupStatusMessageV2 ||
1451
+ message?.limitSharingMessage ||
1452
+ message?.lottieStickerMessage ||
1453
+ message?.newsletterAdminProfileMessage ||
1454
+ message?.newsletterAdminProfileMessageV2 ||
1455
+ message?.newsletterAdminProfileStatusMessage ||
1456
+ message?.pollCreationMessageV4 ||
1457
+ message?.pollCreationOptionImageMessage ||
1458
+ message?.questionMessage ||
1459
+ message?.questionReplyMessage ||
1460
+ message?.spoilerMessage ||
1461
+ message?.statusAddYours ||
1462
+ message?.statusMentionMessage ||
1463
+ message?.viewOnceMessage ||
1464
+ message?.viewOnceMessageV2 ||
1465
+ message?.viewOnceMessageV2Extension);
1466
+ }
1467
+ };
1468
+ /**
1469
+ * Extract the true message content from a message
1470
+ * Eg. extracts the inner message from a disappearing message/view once message
1471
+ */
1472
+ export const extractMessageContent = (content) => {
1473
+ const extractFromTemplateMessage = (msg) => {
1474
+ if (msg.imageMessage) {
1475
+ return { imageMessage: msg.imageMessage };
1476
+ }
1477
+ else if (msg.documentMessage) {
1478
+ return { documentMessage: msg.documentMessage };
1479
+ }
1480
+ else if (msg.videoMessage) {
1481
+ return { videoMessage: msg.videoMessage };
1482
+ }
1483
+ else if (msg.locationMessage) {
1484
+ return { locationMessage: msg.locationMessage };
1485
+ }
1486
+ else {
1487
+ return {
1488
+ conversation: 'contentText' in msg ? msg.contentText : 'hydratedContentText' in msg ? msg.hydratedContentText : ''
1489
+ };
1490
+ }
1491
+ };
1492
+ content = normalizeMessageContent(content);
1493
+ if (content?.buttonsMessage) {
1494
+ return extractFromTemplateMessage(content.buttonsMessage);
1495
+ }
1496
+ if (content?.templateMessage?.hydratedFourRowTemplate) {
1497
+ return extractFromTemplateMessage(content?.templateMessage?.hydratedFourRowTemplate);
1498
+ }
1499
+ if (content?.templateMessage?.hydratedTemplate) {
1500
+ return extractFromTemplateMessage(content?.templateMessage?.hydratedTemplate);
1501
+ }
1502
+ if (content?.templateMessage?.fourRowTemplate) {
1503
+ return extractFromTemplateMessage(content?.templateMessage?.fourRowTemplate);
1504
+ }
1505
+ return content;
1506
+ };
1507
+ /**
1508
+ * Returns the device predicted by message ID
1509
+ */
1510
+ export const getDevice = (id) => /^3A.{18}$/.test(id)
1511
+ ? 'ios'
1512
+ : /^3E.{20}$/.test(id)
1513
+ ? 'web'
1514
+ : /^(.{21}|.{32})$/.test(id)
1515
+ ? 'android'
1516
+ : /^(3F|.{18}$)/.test(id)
1517
+ ? 'desktop'
1518
+ : 'unknown';
1519
+ /** Upserts a receipt in the message */
1520
+ export const updateMessageWithReceipt = (msg, receipt) => {
1521
+ msg.userReceipt = msg.userReceipt || [];
1522
+ const recp = msg.userReceipt.find(m => m.userJid === receipt.userJid);
1523
+ if (recp) {
1524
+ Object.assign(recp, receipt);
1525
+ }
1526
+ else {
1527
+ msg.userReceipt.push(receipt);
1528
+ }
1529
+ };
1530
+ /** Update the message with a new reaction */
1531
+ export const updateMessageWithReaction = (msg, reaction) => {
1532
+ const authorID = getKeyAuthor(reaction.key);
1533
+ const reactions = (msg.reactions || []).filter(r => getKeyAuthor(r.key) !== authorID);
1534
+ reaction.text = reaction.text || '';
1535
+ reactions.push(reaction);
1536
+ msg.reactions = reactions;
1537
+ };
1538
+ /** Update the message with a new poll update */
1539
+ export const updateMessageWithPollUpdate = (msg, update) => {
1540
+ const authorID = getKeyAuthor(update.pollUpdateMessageKey);
1541
+ const reactions = (msg.pollUpdates || []).filter(r => getKeyAuthor(r.pollUpdateMessageKey) !== authorID);
1542
+ if (update.vote?.selectedOptions?.length) {
1543
+ reactions.push(update);
1544
+ }
1545
+ msg.pollUpdates = reactions;
1546
+ };
1547
+ /** Update the message with a new event response */
1548
+ export const updateMessageWithEventResponse = (msg, update) => {
1549
+ const authorID = getKeyAuthor(update.eventResponseMessageKey);
1550
+ const responses = (msg.eventResponses || []).filter(r => getKeyAuthor(r.eventResponseMessageKey) !== authorID);
1551
+ responses.push(update);
1552
+ msg.eventResponses = responses;
1553
+ };
1554
+ /**
1555
+ * Aggregates all poll updates in a poll.
1556
+ * @param msg the poll creation message
1557
+ * @param meId your jid
1558
+ * @returns A list of options & their voters
1559
+ */
1560
+ export function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
1561
+ const opts = message?.pollCreationMessage?.options ||
1562
+ message?.pollCreationMessageV2?.options ||
1563
+ message?.pollCreationMessageV3?.options ||
1564
+ [];
1565
+ const voteHashMap = opts.reduce((acc, opt) => {
1566
+ const hash = sha256(Buffer.from(opt.optionName || '')).toString();
1567
+ acc[hash] = {
1568
+ name: opt.optionName || '',
1569
+ voters: []
1570
+ };
1571
+ return acc;
1572
+ }, {});
1573
+ for (const update of pollUpdates || []) {
1574
+ const { vote } = update;
1575
+ if (!vote) {
1576
+ continue;
1577
+ }
1578
+ for (const option of vote.selectedOptions || []) {
1579
+ const hash = option.toString();
1580
+ let data = voteHashMap[hash];
1581
+ if (!data) {
1582
+ voteHashMap[hash] = {
1583
+ name: 'Unknown',
1584
+ voters: []
1585
+ };
1586
+ data = voteHashMap[hash];
1587
+ }
1588
+ voteHashMap[hash].voters.push(getKeyAuthor(update.pollUpdateMessageKey, meId));
1589
+ }
1590
+ }
1591
+ return Object.values(voteHashMap);
1592
+ }
1593
+ /**
1594
+ * Aggregates all event responses in an event message.
1595
+ * @param msg the event creation message
1596
+ * @param meId your jid
1597
+ * @returns A list of response types & their responders
1598
+ */
1599
+ export function getAggregateResponsesInEventMessage({ eventResponses }, meId) {
1600
+ const responseTypes = ['GOING', 'NOT_GOING', 'MAYBE'];
1601
+ const responseMap = {};
1602
+ for (const type of responseTypes) {
1603
+ responseMap[type] = {
1604
+ response: type,
1605
+ responders: []
1606
+ };
1607
+ }
1608
+ for (const update of eventResponses || []) {
1609
+ const responseType = update.eventResponse || 'UNKNOWN';
1610
+ if (responseType !== 'UNKNOWN' && responseMap[responseType]) {
1611
+ responseMap[responseType].responders.push(getKeyAuthor(update.eventResponseMessageKey, meId));
1612
+ }
1613
+ }
1614
+ return Object.values(responseMap);
1615
+ }
1616
+ /** Given a list of message keys, aggregates them by chat & sender. Useful for sending read receipts in bulk */
1617
+ export const aggregateMessageKeysNotFromMe = (keys) => {
1618
+ const keyMap = {};
1619
+ for (const { remoteJid, id, participant, fromMe } of keys) {
1620
+ if (!fromMe) {
1621
+ const uqKey = `${remoteJid}:${participant || ''}`;
1622
+ if (!keyMap[uqKey]) {
1623
+ keyMap[uqKey] = {
1624
+ jid: remoteJid,
1625
+ participant: participant,
1626
+ messageIds: []
1627
+ };
1628
+ }
1629
+ keyMap[uqKey].messageIds.push(id);
1630
+ }
1631
+ }
1632
+ return Object.values(keyMap);
1633
+ };
1634
+ const REUPLOAD_REQUIRED_STATUS = [410, 404];
1635
+ /**
1636
+ * Downloads the given message. Throws an error if it's not a media message
1637
+ */
1638
+ export const downloadMediaMessage = async (message, type, options, ctx) => {
1639
+ const result = await downloadMsg().catch(async (error) => {
1640
+ if (ctx &&
1641
+ typeof error?.status === 'number' && // treat errors with status as HTTP failures requiring reupload
1642
+ REUPLOAD_REQUIRED_STATUS.includes(error.status)) {
1643
+ ctx.logger.info({ key: message.key }, 'sending reupload media request...');
1644
+ // request reupload
1645
+ message = await ctx.reuploadRequest(message);
1646
+ const result = await downloadMsg();
1647
+ return result;
1648
+ }
1649
+ throw error;
1650
+ });
1651
+ return result;
1652
+ async function downloadMsg() {
1653
+ const mContent = extractMessageContent(message.message);
1654
+ if (!mContent) {
1655
+ throw new Boom('No message present', { statusCode: 400, data: message });
1656
+ }
1657
+ const contentType = getContentType(mContent);
1658
+ let mediaType = contentType?.replace('Message', '');
1659
+ const media = mContent[contentType];
1660
+ if (!media || typeof media !== 'object' || (!('url' in media) && !('thumbnailDirectPath' in media))) {
1661
+ throw new Boom(`"${contentType}" message is not a media message`);
1662
+ }
1663
+ let download;
1664
+ if ('thumbnailDirectPath' in media && !('url' in media)) {
1665
+ download = {
1666
+ directPath: media.thumbnailDirectPath,
1667
+ mediaKey: media.mediaKey
1668
+ };
1669
+ mediaType = 'thumbnail-link';
1670
+ }
1671
+ else {
1672
+ download = media;
1673
+ }
1674
+ const stream = await downloadContentFromMessage(download, mediaType, options);
1675
+ if (type === 'buffer') {
1676
+ const bufferArray = [];
1677
+ for await (const chunk of stream) {
1678
+ bufferArray.push(chunk);
1679
+ }
1680
+ return Buffer.concat(bufferArray);
1681
+ }
1682
+ return stream;
1683
+ }
1684
+ };
1685
+ /** Checks whether the given message is a media message; if it is returns the inner content */
1686
+ export const assertMediaContent = (content) => {
1687
+ content = extractMessageContent(content);
1688
+ const mediaContent = content?.documentMessage ||
1689
+ content?.imageMessage ||
1690
+ content?.videoMessage ||
1691
+ content?.audioMessage ||
1692
+ content?.stickerMessage;
1693
+ if (!mediaContent) {
1694
+ throw new Boom('given message is not a media message', { statusCode: 400, data: content });
1695
+ }
1696
+ return mediaContent;
1697
+ };
1698
+ /**
1699
+ * Checks if a WebP buffer is animated by looking for VP8X chunk with animation flag
1700
+ * or ANIM/ANMF chunks
1701
+ */
1702
+ const isAnimatedWebP = (buffer) => {
1703
+ // WebP must start with RIFF....WEBP
1704
+ if (buffer.length < 12 ||
1705
+ buffer[0] !== 0x52 ||
1706
+ buffer[1] !== 0x49 ||
1707
+ buffer[2] !== 0x46 ||
1708
+ buffer[3] !== 0x46 ||
1709
+ buffer[8] !== 0x57 ||
1710
+ buffer[9] !== 0x45 ||
1711
+ buffer[10] !== 0x42 ||
1712
+ buffer[11] !== 0x50) {
1713
+ return false;
1714
+ }
1715
+ ;
1716
+ // Parse chunks starting after RIFF header (12 bytes)
1717
+ let offset = 12;
1718
+ while (offset < buffer.length - 8) {
1719
+ const chunkFourCC = buffer.toString('ascii', offset, offset + 4);
1720
+ const chunkSize = buffer.readUInt32LE(offset + 4);
1721
+ if (chunkFourCC === 'VP8X') {
1722
+ // VP8X extended header, check animation flag (bit 1 at offset+8)
1723
+ const flagsOffset = offset + 8;
1724
+ if (flagsOffset < buffer.length) {
1725
+ const flags = buffer[flagsOffset];
1726
+ if (flags & 0x02) {
1727
+ return true;
1728
+ }
1729
+ ;
1730
+ }
1731
+ ;
1732
+ }
1733
+ else if (chunkFourCC === 'ANIM' || chunkFourCC === 'ANMF') {
1734
+ // ANIM or ANMF chunks indicate animation
1735
+ return true;
1736
+ }
1737
+ ;
1738
+ // Move to next chunk (chunk size + 8 bytes header, padded to even)
1739
+ offset += 8 + chunkSize + (chunkSize % 2);
1740
+ }
1741
+ ;
1742
+ return false;
1743
+ };
1744
+ /**
1745
+ * Checks if a buffer is a WebP file
1746
+ */
1747
+ const isWebPBuffer = (buffer) => {
1748
+ return (buffer.length >= 12 &&
1749
+ buffer[0] === 0x52 &&
1750
+ buffer[1] === 0x49 &&
1751
+ buffer[2] === 0x46 &&
1752
+ buffer[3] === 0x46 &&
1753
+ buffer[8] === 0x57 &&
1754
+ buffer[9] === 0x45 &&
1755
+ buffer[10] === 0x42 &&
1756
+ buffer[11] === 0x50);
1757
+ };
1758
+ export const shouldIncludeBizBinaryNode = (message) => {
1759
+ const hasValidInteractive = message.interactiveMessage &&
1760
+ !message.interactiveMessage.carouselMessage &&
1761
+ !message.interactiveMessage.collectionMessage &&
1762
+ !message.interactiveMessage.shopStorefrontMessage;
1763
+ return (message.buttonsMessage ||
1764
+ message.interactiveMessage ||
1765
+ message.listMessage ||
1766
+ hasValidInteractive);
1767
+ };
1768
+ //# sourceMappingURL=messages.js.map