@dnuzi/baileys 0.0.1

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