@realvare/based 2.5.7 → 2.5.8

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.
@@ -1,1373 +1,1338 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.assertMediaContent = exports.downloadMediaMessage = exports.aggregateMessageKeysNotFromMe = exports.updateMessageWithPollUpdate = exports.updateMessageWithReaction = exports.updateMessageWithReceipt = exports.getDevice = exports.extractMessageContent = exports.normalizeMessageContent = exports.getContentType = exports.generateWAMessage = exports.generateWAMessageFromContent = exports.generateWAMessageContent = exports.generateForwardMessageContent = exports.prepareDisappearingMessageSettingContent = exports.prepareWAMessageMedia = exports.generateLinkPreviewIfRequired = exports.extractUrlFromText = void 0;
7
- exports.getAggregateVotesInPollMessage = getAggregateVotesInPollMessage;
8
- const boom_1 = require("@hapi/boom");
9
- const axios_1 = __importDefault(require("axios"));
10
- const crypto_1 = require("crypto");
11
- const fs_1 = require("fs");
12
- const WAProto_1 = require("../../WAProto");
13
- const Defaults_1 = require("../Defaults");
14
- const Types_1 = require("../Types");
15
- const WABinary_1 = require("../WABinary");
16
- const crypto_2 = require("./crypto");
17
- const generics_1 = require("./generics");
18
- const messages_media_1 = require("./messages-media");
19
- const MIMETYPE_MAP = {
20
- image: 'image/jpeg',
21
- video: 'video/mp4',
22
- document: 'application/pdf',
23
- audio: 'audio/ogg; codecs=opus',
24
- sticker: 'image/webp',
25
- 'product-catalog-image': 'image/jpeg',
26
- };
27
- const MessageTypeProto = {
28
- 'image': Types_1.WAProto.Message.ImageMessage,
29
- 'video': Types_1.WAProto.Message.VideoMessage,
30
- 'audio': Types_1.WAProto.Message.AudioMessage,
31
- 'sticker': Types_1.WAProto.Message.StickerMessage,
32
- 'document': Types_1.WAProto.Message.DocumentMessage,
33
- };
34
- const ButtonType = WAProto_1.proto.Message.ButtonsMessage.HeaderType;
35
- /**
36
- * Uses a regex to test whether the string contains a URL, and returns the URL if it does.
37
- * @param text eg. hello https://google.com
38
- * @returns the URL, eg. https://google.com
39
- */
40
- const extractUrlFromText = (text) => { var _a; return (_a = text.match(Defaults_1.URL_REGEX)) === null || _a === void 0 ? void 0 : _a[0]; };
41
- exports.extractUrlFromText = extractUrlFromText;
42
- const generateLinkPreviewIfRequired = async (text, getUrlInfo, logger) => {
43
- const url = (0, exports.extractUrlFromText)(text);
44
- if (!!getUrlInfo && url) {
45
- try {
46
- const urlInfo = await getUrlInfo(url);
47
- return urlInfo;
48
- }
49
- catch (error) { // ignore if fails
50
- logger === null || logger === void 0 ? void 0 : logger.warn({ trace: error.stack }, 'url generation failed');
51
- }
52
- }
53
- };
54
- exports.generateLinkPreviewIfRequired = generateLinkPreviewIfRequired;
55
- const assertColor = async (color) => {
56
- let assertedColor;
57
- if (typeof color === 'number') {
58
- assertedColor = color > 0 ? color : 0xffffffff + Number(color) + 1;
59
- }
60
- else {
61
- let hex = color.trim().replace('#', '');
62
- if (hex.length <= 6) {
63
- hex = 'FF' + hex.padStart(6, '0');
64
- }
65
- assertedColor = parseInt(hex, 16);
66
- return assertedColor;
67
- }
68
- };
69
- const prepareWAMessageMedia = async (message, options) => {
70
- const logger = options.logger;
71
- let mediaType;
72
- for (const key of Defaults_1.MEDIA_KEYS) {
73
- if (key in message) {
74
- mediaType = key;
75
- }
76
- }
77
- if (!mediaType) {
78
- throw new boom_1.Boom('Invalid media type', { statusCode: 400 });
79
- }
80
- const uploadData = {
81
- ...message,
82
- media: message[mediaType]
83
- };
84
- delete uploadData[mediaType];
85
- // check if cacheable + generate cache key
86
- const cacheableKey = typeof uploadData.media === 'object' &&
87
- ('url' in uploadData.media) &&
88
- !!uploadData.media.url &&
89
- !!options.mediaCache && (
90
- // generate the key
91
- mediaType + ':' + uploadData.media.url.toString());
92
- if (mediaType === 'document' && !uploadData.fileName) {
93
- uploadData.fileName = 'file';
94
- }
95
- if (!uploadData.mimetype) {
96
- uploadData.mimetype = MIMETYPE_MAP[mediaType];
97
- }
98
- // check for cache hit
99
- if (cacheableKey) {
100
- const mediaBuff = options.mediaCache.get(cacheableKey);
101
- if (mediaBuff) {
102
- logger === null || logger === void 0 ? void 0 : logger.debug({ cacheableKey }, 'got media cache hit');
103
- const obj = Types_1.WAProto.Message.decode(mediaBuff);
104
- const key = `${mediaType}Message`;
105
- Object.assign(obj[key], { ...uploadData, media: undefined });
106
- return obj;
107
- }
108
- }
109
- const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined';
110
- const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') &&
111
- (typeof uploadData['jpegThumbnail'] === 'undefined');
112
- const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === true;
113
- const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === true;
114
- const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation;
115
- const { mediaKey, encWriteStream, bodyPath, fileEncSha256, fileSha256, fileLength, didSaveToTmpPath, } = await (options.newsletter ? messages_media_1.prepareStream : messages_media_1.encryptedStream)(uploadData.media, options.mediaTypeOverride || mediaType, {
116
- logger,
117
- saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
118
- opts: options.options
119
- });
120
- // url safe Base64 encode the SHA256 hash of the body
121
- const fileEncSha256B64 = (options.newsletter ? fileSha256 : fileEncSha256 !== null && fileEncSha256 !== void 0 ? fileEncSha256 : fileSha256).toString('base64');
122
- const [{ mediaUrl, directPath, handle }] = await Promise.all([
123
- (async () => {
124
- const result = await options.upload(encWriteStream, { fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs });
125
- logger === null || logger === void 0 ? void 0 : logger.debug({ mediaType, cacheableKey }, 'uploaded media');
126
- return result;
127
- })(),
128
- (async () => {
129
- try {
130
- if (requiresThumbnailComputation) {
131
- const { thumbnail, originalImageDimensions } = await (0, messages_media_1.generateThumbnail)(bodyPath, mediaType, options);
132
- uploadData.jpegThumbnail = thumbnail;
133
- if (!uploadData.width && originalImageDimensions) {
134
- uploadData.width = originalImageDimensions.width;
135
- uploadData.height = originalImageDimensions.height;
136
- logger === null || logger === void 0 ? void 0 : logger.debug('set dimensions');
137
- }
138
- logger === null || logger === void 0 ? void 0 : logger.debug('generated thumbnail');
139
- }
140
- if (requiresDurationComputation) {
141
- uploadData.seconds = await (0, messages_media_1.getAudioDuration)(bodyPath);
142
- logger === null || logger === void 0 ? void 0 : logger.debug('computed audio duration');
143
- }
144
- if (requiresWaveformProcessing) {
145
- uploadData.waveform = await (0, messages_media_1.getAudioWaveform)(bodyPath, logger);
146
- logger === null || logger === void 0 ? void 0 : logger.debug('processed waveform');
147
- }
148
- if (requiresAudioBackground) {
149
- uploadData.backgroundArgb = await assertColor(options.backgroundColor);
150
- logger === null || logger === void 0 ? void 0 : logger.debug('computed backgroundColor audio status');
151
- }
152
- }
153
- catch (error) {
154
- logger === null || logger === void 0 ? void 0 : logger.warn({ trace: error.stack }, 'failed to obtain extra info');
155
- }
156
- })(),
157
- ])
158
- .finally(async () => {
159
- if (!Buffer.isBuffer(encWriteStream)) {
160
- encWriteStream.destroy();
161
- }
162
- // remove tmp files
163
- if (didSaveToTmpPath && bodyPath) {
164
- try {
165
- await fs_1.promises.access(bodyPath);
166
- await fs_1.promises.unlink(bodyPath);
167
- logger === null || logger === void 0 ? void 0 : logger.debug('removed tmp file');
168
- }
169
- catch (error) {
170
- logger === null || logger === void 0 ? void 0 : logger.warn('failed to remove tmp file');
171
- }
172
- }
173
- });
174
- const obj = Types_1.WAProto.Message.fromObject({
175
- [`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
176
- url: handle ? undefined : mediaUrl,
177
- directPath,
178
- mediaKey: mediaKey,
179
- fileEncSha256: fileEncSha256,
180
- fileSha256,
181
- fileLength,
182
- mediaKeyTimestamp: handle ? undefined : (0, generics_1.unixTimestampSeconds)(),
183
- ...uploadData,
184
- media: undefined
185
- })
186
- });
187
- if (uploadData.ptv) {
188
- obj.ptvMessage = obj.videoMessage;
189
- delete obj.videoMessage;
190
- }
191
- if (cacheableKey) {
192
- logger === null || logger === void 0 ? void 0 : logger.debug({ cacheableKey }, 'set cache');
193
- options.mediaCache.set(cacheableKey, Types_1.WAProto.Message.encode(obj).finish());
194
- }
195
- return obj;
196
- };
197
- exports.prepareWAMessageMedia = prepareWAMessageMedia;
198
- const prepareDisappearingMessageSettingContent = (ephemeralExpiration) => {
199
- ephemeralExpiration = ephemeralExpiration || 0;
200
- const content = {
201
- ephemeralMessage: {
202
- message: {
203
- protocolMessage: {
204
- type: Types_1.WAProto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
205
- ephemeralExpiration
206
- }
207
- }
208
- }
209
- };
210
- return Types_1.WAProto.Message.fromObject(content);
211
- };
212
- exports.prepareDisappearingMessageSettingContent = prepareDisappearingMessageSettingContent;
213
- /**
214
- * Generate forwarded message content like WA does
215
- * @param message the message to forward
216
- * @param options.forceForward will show the message as forwarded even if it is from you
217
- */
218
- const generateForwardMessageContent = (message, forceForward) => {
219
- var _a;
220
- let content = message.message;
221
- if (!content) {
222
- throw new boom_1.Boom('no content in message', { statusCode: 400 });
223
- }
224
- // hacky copy
225
- content = (0, exports.normalizeMessageContent)(content);
226
- content = WAProto_1.proto.Message.decode(WAProto_1.proto.Message.encode(content).finish());
227
- let key = Object.keys(content)[0];
228
- let score = ((_a = content[key].contextInfo) === null || _a === void 0 ? void 0 : _a.forwardingScore) || 0;
229
- score += message.key.fromMe && !forceForward ? 0 : 1;
230
- if (key === 'conversation') {
231
- content.extendedTextMessage = { text: content[key] };
232
- delete content.conversation;
233
- key = 'extendedTextMessage';
234
- }
235
- if (score > 0) {
236
- content[key].contextInfo = { forwardingScore: score, isForwarded: true };
237
- }
238
- else {
239
- content[key].contextInfo = {};
240
- }
241
- return content;
242
- };
243
- exports.generateForwardMessageContent = generateForwardMessageContent;
244
- const generateWAMessageContent = async (message, options) => {
245
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
246
- var _p, _q;
247
-
248
- // iOS-specific externalAdReply handling
249
- if (message.contextInfo?.externalAdReply) {
250
- const externalAdReply = message.contextInfo.externalAdReply;
251
-
252
- // If thumbnailUrl is provided but no thumbnail binary data, try to fetch and convert for iOS
253
- if (externalAdReply.thumbnailUrl && !externalAdReply.thumbnail) {
254
- try {
255
- // Attempt to download the thumbnail image for iOS compatibility
256
- const axiosResponse = await axios_1.default.get(externalAdReply.thumbnailUrl, {
257
- responseType: 'arraybuffer',
258
- timeout: 5000,
259
- headers: {
260
- 'User-Agent': 'WhatsApp/2.23.20.15 iOS/16.0 Device/iPhone'
261
- }
262
- });
263
-
264
- if (axiosResponse.status === 200) {
265
- externalAdReply.thumbnail = Buffer.from(axiosResponse.data);
266
- // Clear thumbnailUrl since we now have binary data
267
- delete externalAdReply.thumbnailUrl;
268
- }
269
- } catch (error) {
270
- // If thumbnail download fails, keep thumbnailUrl as is
271
- options.logger?.warn('Failed to download externalAdReply thumbnail for iOS:', error.message);
272
- }
273
- }
274
-
275
- // Update the contextInfo with modified externalAdReply
276
- message.contextInfo.externalAdReply = externalAdReply;
277
- }
278
-
279
- let m = {};
280
- if ('text' in message) {
281
- const extContent = { text: message.text };
282
- let urlInfo = message.linkPreview;
283
- if (urlInfo === true) {
284
- urlInfo = await (0, exports.generateLinkPreviewIfRequired)(message.text, options.getUrlInfo, options.logger);
285
- }
286
- if (urlInfo && typeof urlInfo === 'object') {
287
- extContent.matchedText = urlInfo['matched-text'];
288
- extContent.jpegThumbnail = urlInfo.jpegThumbnail;
289
- extContent.description = urlInfo.description;
290
- extContent.title = urlInfo.title;
291
- extContent.previewType = 0;
292
- const img = urlInfo.highQualityThumbnail;
293
- if (img) {
294
- extContent.thumbnailDirectPath = img.directPath;
295
- extContent.mediaKey = img.mediaKey;
296
- extContent.mediaKeyTimestamp = img.mediaKeyTimestamp;
297
- extContent.thumbnailWidth = img.width;
298
- extContent.thumbnailHeight = img.height;
299
- extContent.thumbnailSha256 = img.fileSha256;
300
- extContent.thumbnailEncSha256 = img.fileEncSha256;
301
- }
302
- }
303
- if (options.backgroundColor) {
304
- extContent.backgroundArgb = await assertColor(options.backgroundColor);
305
- }
306
- if (options.font) {
307
- extContent.font = options.font;
308
- }
309
- m.extendedTextMessage = extContent;
310
- }
311
- else if ('contacts' in message) {
312
- const contactLen = message.contacts.contacts.length;
313
- if (!contactLen) {
314
- throw new boom_1.Boom('require atleast 1 contact', { statusCode: 400 });
315
- }
316
- if (contactLen === 1) {
317
- m.contactMessage = Types_1.WAProto.Message.ContactMessage.fromObject(message.contacts.contacts[0]);
318
- }
319
- else {
320
- m.contactsArrayMessage = Types_1.WAProto.Message.ContactsArrayMessage.fromObject(message.contacts);
321
- }
322
- }
323
- else if ('location' in message) {
324
- m.locationMessage = Types_1.WAProto.Message.LocationMessage.fromObject(message.location);
325
- }
326
- else if ('liveLocation' in message) {
327
- m.liveLocationMessage = Types_1.WAProto.Message.LiveLocationMessage.fromObject(message.liveLocation);
328
- }
329
- else if ('react' in message) {
330
- if (!message.react.senderTimestampMs) {
331
- message.react.senderTimestampMs = Date.now();
332
- }
333
- m.reactionMessage = Types_1.WAProto.Message.ReactionMessage.fromObject(message.react);
334
- }
335
- else if ('delete' in message) {
336
- m.protocolMessage = {
337
- key: message.delete,
338
- type: Types_1.WAProto.Message.ProtocolMessage.Type.REVOKE
339
- };
340
- }
341
- else if ('forward' in message) {
342
- m = (0, exports.generateForwardMessageContent)(message.forward, message.force);
343
- }
344
- else if ('disappearingMessagesInChat' in message) {
345
- const exp = typeof message.disappearingMessagesInChat === 'boolean' ?
346
- (message.disappearingMessagesInChat ? Defaults_1.WA_DEFAULT_EPHEMERAL : 0) :
347
- message.disappearingMessagesInChat;
348
- m = (0, exports.prepareDisappearingMessageSettingContent)(exp);
349
- }
350
- else if ('groupInvite' in message) {
351
- m.groupInviteMessage = {};
352
- m.groupInviteMessage.inviteCode = message.groupInvite.inviteCode;
353
- m.groupInviteMessage.inviteExpiration = message.groupInvite.inviteExpiration;
354
- m.groupInviteMessage.caption = message.groupInvite.text;
355
- m.groupInviteMessage.groupJid = message.groupInvite.jid;
356
- m.groupInviteMessage.groupName = message.groupInvite.subject;
357
- //TODO: use built-in interface and get disappearing mode info etc.
358
- //TODO: cache / use store!?
359
- if (options.getProfilePicUrl) {
360
- const pfpUrl = await options.getProfilePicUrl(message.groupInvite.jid, 'preview');
361
- if (pfpUrl) {
362
- const resp = await axios_1.default.get(pfpUrl, { responseType: 'arraybuffer' });
363
- if (resp.status === 200) {
364
- m.groupInviteMessage.jpegThumbnail = resp.data;
365
- }
366
- }
367
- }
368
- }
369
- else if ('pin' in message) {
370
- m.pinInChatMessage = {};
371
- m.messageContextInfo = {};
372
- m.pinInChatMessage.key = message.pin;
373
- m.pinInChatMessage.type = message.type;
374
- m.pinInChatMessage.senderTimestampMs = Date.now();
375
- m.messageContextInfo.messageAddOnDurationInSecs = message.type === 1 ? message.time || 86400 : 0;
376
- }
377
- else if ('keep' in message) {
378
- m.keepInChatMessage = {};
379
- m.keepInChatMessage.key = message.keep;
380
- m.keepInChatMessage.keepType = message.type;
381
- m.keepInChatMessage.timestampMs = Date.now();
382
- }
383
- else if ('call' in message) {
384
- m = {
385
- scheduledCallCreationMessage: {
386
- scheduledTimestampMs: (_a = message.call.time) !== null && _a !== void 0 ? _a : Date.now(),
387
- callType: (_b = message.call.type) !== null && _b !== void 0 ? _b : 1,
388
- title: message.call.title
389
- }
390
- };
391
- }
392
- else if ('paymentInvite' in message) {
393
- m.paymentInviteMessage = {
394
- serviceType: message.paymentInvite.type,
395
- expiryTimestamp: message.paymentInvite.expiry
396
- };
397
- }
398
- else if ('buttonReply' in message) {
399
- switch (message.type) {
400
- case 'template':
401
- m.templateButtonReplyMessage = {
402
- selectedDisplayText: message.buttonReply.displayText,
403
- selectedId: message.buttonReply.id,
404
- selectedIndex: message.buttonReply.index,
405
- };
406
- break;
407
- case 'plain':
408
- m.buttonsResponseMessage = {
409
- selectedButtonId: message.buttonReply.id,
410
- selectedDisplayText: message.buttonReply.displayText,
411
- type: WAProto_1.proto.Message.ButtonsResponseMessage.Type.DISPLAY_TEXT,
412
- };
413
- break;
414
- }
415
- }
416
- else if ('ptv' in message && message.ptv) {
417
- const { videoMessage } = await (0, exports.prepareWAMessageMedia)({ video: message.video }, options);
418
- m.ptvMessage = videoMessage;
419
- }
420
- else if ('product' in message) {
421
- const { imageMessage } = await (0, exports.prepareWAMessageMedia)({ image: message.product.productImage }, options);
422
- m.productMessage = Types_1.WAProto.Message.ProductMessage.fromObject({
423
- ...message,
424
- product: {
425
- ...message.product,
426
- productImage: imageMessage,
427
- }
428
- });
429
- }
430
- else if ('order' in message) {
431
- m.orderMessage = Types_1.WAProto.Message.OrderMessage.fromObject({
432
- orderId: message.order.id,
433
- thumbnail: message.order.thumbnail,
434
- itemCount: message.order.itemCount,
435
- status: message.order.status,
436
- surface: message.order.surface,
437
- orderTitle: message.order.title,
438
- message: message.order.text,
439
- sellerJid: message.order.seller,
440
- token: message.order.token,
441
- totalAmount1000: message.order.amount,
442
- totalCurrencyCode: message.order.currency
443
- });
444
- }
445
- else if ('listReply' in message) {
446
- m.listResponseMessage = { ...message.listReply };
447
- }
448
- else if ('poll' in message) {
449
- (_p = message.poll).selectableCount || (_p.selectableCount = 0);
450
- (_q = message.poll).toAnnouncementGroup || (_q.toAnnouncementGroup = false);
451
- if (!Array.isArray(message.poll.values)) {
452
- throw new boom_1.Boom('Invalid poll values', { statusCode: 400 });
453
- }
454
- if (message.poll.selectableCount < 0
455
- || message.poll.selectableCount > message.poll.values.length) {
456
- throw new boom_1.Boom(`poll.selectableCount in poll should be >= 0 and <= ${message.poll.values.length}`, { statusCode: 400 });
457
- }
458
- m.messageContextInfo = {
459
- // encKey
460
- messageSecret: message.poll.messageSecret || (0, crypto_1.randomBytes)(32),
461
- };
462
- const pollCreationMessage = {
463
- name: message.poll.name,
464
- selectableOptionsCount: message.poll.selectableCount,
465
- options: message.poll.values.map(optionName => ({ optionName })),
466
- };
467
- if (message.poll.toAnnouncementGroup) {
468
- // poll v2 is for community announcement groups (single select and multiple)
469
- m.pollCreationMessageV2 = pollCreationMessage;
470
- }
471
- else {
472
- if (message.poll.selectableCount === 1) {
473
- // poll v3 is for single select polls
474
- m.pollCreationMessageV3 = pollCreationMessage;
475
- }
476
- else {
477
- // poll for multiple choice polls
478
- m.pollCreationMessage = pollCreationMessage;
479
- }
480
- }
481
- }
482
- else if ('event' in message) {
483
- m.messageContextInfo = {
484
- messageSecret: message.event.messageSecret || (0, crypto_1.randomBytes)(32),
485
- };
486
- m.eventMessage = { ...message.event };
487
- }
488
- else if ('inviteAdmin' in message) {
489
- m.newsletterAdminInviteMessage = {};
490
- m.newsletterAdminInviteMessage.inviteExpiration = message.inviteAdmin.inviteExpiration;
491
- m.newsletterAdminInviteMessage.caption = message.inviteAdmin.text;
492
- m.newsletterAdminInviteMessage.newsletterJid = message.inviteAdmin.jid;
493
- m.newsletterAdminInviteMessage.newsletterName = message.inviteAdmin.subject;
494
- m.newsletterAdminInviteMessage.jpegThumbnail = message.inviteAdmin.thumbnail;
495
- }
496
- else if ('requestPayment' in message) {
497
- const reqPayment = message.requestPayment;
498
- const sticker = reqPayment.sticker ?
499
- await (0, exports.prepareWAMessageMedia)({ sticker: reqPayment.sticker, ...options }, options)
500
- : null;
501
- let notes = {};
502
- if (reqPayment.sticker) {
503
- notes = {
504
- stickerMessage: {
505
- ...sticker.stickerMessage,
506
- contextInfo: reqPayment.contextInfo
507
- }
508
- };
509
- }
510
- else if (reqPayment.note) {
511
- notes = {
512
- extendedTextMessage: {
513
- text: reqPayment.note,
514
- contextInfo: reqPayment.contextInfo,
515
- }
516
- };
517
- }
518
- else {
519
- throw new boom_1.Boom('Invalid media type', { statusCode: 400 });
520
- }
521
- m.requestPaymentMessage = Types_1.WAProto.Message.RequestPaymentMessage.fromObject({
522
- expiryTimestamp: reqPayment.expiryTimestamp || reqPayment.expiry,
523
- amount1000: reqPayment.amount1000 || reqPayment.amount,
524
- currencyCodeIso4217: reqPayment.currencyCodeIso4217 || reqPayment.currency,
525
- requestFrom: reqPayment.requestFrom || reqPayment.from,
526
- noteMessage: { ...notes },
527
- background: reqPayment.background,
528
- // Aggiungi altri parametri se disponibili
529
- ...reqPayment
530
- });
531
- }
532
- else if ('sharePhoneNumber' in message) {
533
- m.protocolMessage = {
534
- type: WAProto_1.proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER
535
- };
536
- }
537
- else if ('requestPhoneNumber' in message) {
538
- m.requestPhoneNumberMessage = {};
539
- }
540
- else if ('call' in message) {
541
- m.callMessage = Types_1.WAProto.Message.CallMessage.fromObject(message.call);
542
- }
543
- else if ('newsletterMessage' in message) {
544
- m.newsletterMessage = Types_1.WAProto.Message.NewsletterMessage.fromObject(message.newsletterMessage);
545
- }
546
- else if ('externalAdReply' in message) {
547
- // Handle sendNyanCat functionality - external ad reply
548
- const extAdReply = message.externalAdReply;
549
- m.extendedTextMessage = {
550
- text: message.text || '',
551
- contextInfo: {
552
- externalAdReply: {
553
- title: extAdReply.title,
554
- body: extAdReply.body,
555
- mediaType: extAdReply.mediaType || 1,
556
- thumbnailUrl: extAdReply.thumbnailUrl,
557
- thumbnail: extAdReply.thumbnail,
558
- sourceUrl: extAdReply.sourceUrl,
559
- showAdAttribution: extAdReply.showAdAttribution || false,
560
- renderLargerThumbnail: extAdReply.renderLargerThumbnail || false
561
- }
562
- }
563
- };
564
- }
565
- else if ('payment' in message) {
566
- // Handle sendPayment functionality
567
- m.requestPaymentMessage = Types_1.WAProto.Message.RequestPaymentMessage.fromObject({
568
- currencyCodeIso4217: message.payment.currency || 'EUR',
569
- amount1000: message.payment.amount1000 || message.payment.amount * 1000,
570
- requestFrom: message.payment.requestFrom || message.payment.from,
571
- noteMessage: {
572
- extendedTextMessage: {
573
- text: message.payment.text || message.payment.note || '',
574
- contextInfo: message.payment.contextInfo
575
- }
576
- },
577
- expiryTimestamp: message.payment.expiryTimestamp || message.payment.expiry,
578
- background: message.payment.background
579
- });
580
- }
581
- else if ('album' in message) {
582
- const imageMessages = message.album.filter(item => 'image' in item);
583
- const videoMessages = message.album.filter(item => 'video' in item);
584
- m.albumMessage = WAProto_1.proto.Message.AlbumMessage.fromObject({
585
- expectedImageCount: imageMessages.length,
586
- expectedVideoCount: videoMessages.length,
587
- });
588
- }
589
- else {
590
- m = await (0, exports.prepareWAMessageMedia)(message, options);
591
- }
592
- if ('buttons' in message && !!message.buttons) {
593
- const buttonsMessage = {
594
- buttons: message.buttons.map(b => ({ ...b, type: WAProto_1.proto.Message.ButtonsMessage.Button.Type.RESPONSE }))
595
- };
596
- if ('text' in message) {
597
- buttonsMessage.contentText = message.text;
598
- buttonsMessage.headerType = ButtonType.EMPTY;
599
- }
600
- else {
601
- if ('caption' in message) {
602
- buttonsMessage.contentText = message.caption;
603
- }
604
- const type = Object.keys(m)[0].replace('Message', '').toUpperCase();
605
- buttonsMessage.headerType = ButtonType[type];
606
- Object.assign(buttonsMessage, m);
607
- }
608
- if ('title' in message && !!message.title) {
609
- buttonsMessage.text = message.title,
610
- buttonsMessage.headerType = ButtonType.TEXT;
611
- }
612
- if ('footer' in message && !!message.footer) {
613
- buttonsMessage.footerText = message.footer;
614
- }
615
- if ('contextInfo' in message && !!message.contextInfo) {
616
- buttonsMessage.contextInfo = message.contextInfo;
617
- }
618
- if ('mentions' in message && !!message.mentions) {
619
- buttonsMessage.contextInfo = { mentionedJid: message.mentions };
620
- }
621
- m = { buttonsMessage };
622
- }
623
- else if ('templateButtons' in message && !!message.templateButtons) {
624
- const templateMsg = {
625
- hydratedButtons: message.templateButtons
626
- };
627
- if ('text' in message) {
628
- templateMsg.hydratedContentText = message.text;
629
- }
630
- else if ('caption' in message) {
631
- templateMsg.hydratedContentText = message.caption;
632
- }
633
- if ('footer' in message && !!message.footer) {
634
- templateMsg.hydratedFooterText = message.footer;
635
- }
636
- // Add media to template if present
637
- if (m && Object.keys(m).length > 0) {
638
- Object.assign(templateMsg, m);
639
- }
640
- m = {
641
- templateMessage: {
642
- fourRowTemplate: templateMsg,
643
- hydratedTemplate: templateMsg
644
- }
645
- };
646
- }
647
- if ('sections' in message && !!message.sections) {
648
- const listMessage = {
649
- sections: message.sections,
650
- buttonText: message.buttonText,
651
- title: message.title,
652
- footerText: message.footer,
653
- description: message.text,
654
- listType: WAProto_1.proto.Message.ListMessage.ListType.SINGLE_SELECT
655
- };
656
- m = { listMessage };
657
- }
658
- if ('interactiveButtons' in message && !!message.interactiveButtons) {
659
- const interactiveMessage = {
660
- nativeFlowMessage: Types_1.WAProto.Message.InteractiveMessage.NativeFlowMessage.fromObject({
661
- buttons: message.interactiveButtons,
662
- })
663
- };
664
- if ('text' in message) {
665
- interactiveMessage.body = {
666
- text: message.text
667
- };
668
- }
669
- else if ('caption' in message) {
670
- interactiveMessage.body = {
671
- text: message.caption
672
- };
673
- interactiveMessage.header = {
674
- title: message.title,
675
- subtitle: message.subtitle,
676
- hasMediaAttachment: (_j = message === null || message === void 0 ? void 0 : message.media) !== null && _j !== void 0 ? _j : false,
677
- };
678
- Object.assign(interactiveMessage.header, m);
679
- }
680
- if ('footer' in message && !!message.footer) {
681
- interactiveMessage.footer = {
682
- text: message.footer
683
- };
684
- }
685
- if ('title' in message && !!message.title) {
686
- interactiveMessage.header = {
687
- title: message.title,
688
- subtitle: message.subtitle,
689
- hasMediaAttachment: (_k = message === null || message === void 0 ? void 0 : message.media) !== null && _k !== void 0 ? _k : false,
690
- };
691
- Object.assign(interactiveMessage.header, m);
692
- }
693
- if ('contextInfo' in message && !!message.contextInfo) {
694
- interactiveMessage.contextInfo = message.contextInfo;
695
- }
696
- if ('mentions' in message && !!message.mentions) {
697
- interactiveMessage.contextInfo = { mentionedJid: message.mentions };
698
- }
699
- m = { interactiveMessage };
700
- }
701
- if ('shop' in message && !!message.shop) {
702
- const interactiveMessage = {
703
- shopStorefrontMessage: Types_1.WAProto.Message.InteractiveMessage.ShopMessage.fromObject({
704
- surface: message.shop,
705
- id: message.id
706
- })
707
- };
708
- if ('text' in message) {
709
- interactiveMessage.body = {
710
- text: message.text
711
- };
712
- }
713
- else if ('caption' in message) {
714
- interactiveMessage.body = {
715
- text: message.caption
716
- };
717
- interactiveMessage.header = {
718
- title: message.title,
719
- subtitle: message.subtitle,
720
- hasMediaAttachment: (_l = message === null || message === void 0 ? void 0 : message.media) !== null && _l !== void 0 ? _l : false,
721
- };
722
- Object.assign(interactiveMessage.header, m);
723
- }
724
- if ('footer' in message && !!message.footer) {
725
- interactiveMessage.footer = {
726
- text: message.footer
727
- };
728
- }
729
- if ('title' in message && !!message.title) {
730
- interactiveMessage.header = {
731
- title: message.title,
732
- subtitle: message.subtitle,
733
- hasMediaAttachment: (_m = message === null || message === void 0 ? void 0 : message.media) !== null && _m !== void 0 ? _m : false,
734
- };
735
- Object.assign(interactiveMessage.header, m);
736
- }
737
- if ('contextInfo' in message && !!message.contextInfo) {
738
- interactiveMessage.contextInfo = message.contextInfo;
739
- }
740
- if ('mentions' in message && !!message.mentions) {
741
- interactiveMessage.contextInfo = { mentionedJid: message.mentions };
742
- }
743
- m = { interactiveMessage };
744
- }
745
- if ('cards' in message && !!message.cards && message.cards.length > 0) {
746
- const carouselCards = await Promise.all(message.cards.map(async (card) => {
747
- const cardMessage = {
748
- header: {
749
- title: card.title || '',
750
- hasMediaAttachment: !!(card.image || card.video)
751
- }
752
- };
753
-
754
- // Add body as separate field if present
755
- if (card.body) {
756
- cardMessage.body = {
757
- text: card.body
758
- };
759
- }
760
- // Handle media attachments
761
- if (card.image) {
762
- const mediaMessage = await prepareWAMessageMedia({ image: card.image }, options);
763
- if (mediaMessage.imageMessage) {
764
- cardMessage.header.imageMessage = mediaMessage.imageMessage;
765
- }
766
- }
767
- else if (card.video) {
768
- const mediaMessage = await prepareWAMessageMedia({ video: card.video }, options);
769
- if (mediaMessage.videoMessage) {
770
- cardMessage.header.videoMessage = mediaMessage.videoMessage;
771
- }
772
- }
773
- // Handle buttons
774
- if (card.buttons && card.buttons.length > 0) {
775
- cardMessage.nativeFlowMessage = {
776
- buttons: card.buttons.map(button => ({
777
- name: button.name,
778
- buttonParamsJson: button.buttonParamsJson
779
- }))
780
- };
781
- }
782
- // Add footer if present
783
- if (card.footer) {
784
- cardMessage.footer = {
785
- text: card.footer
786
- };
787
- }
788
- return cardMessage;
789
- }));
790
- const interactiveMessage = {
791
- carouselMessage: Types_1.WAProto.Message.InteractiveMessage.CarouselMessage.fromObject({
792
- cards: carouselCards,
793
- messageVersion: 1
794
- })
795
- };
796
- if ('text' in message) {
797
- interactiveMessage.body = {
798
- text: message.text
799
- };
800
- }
801
- if ('footer' in message && !!message.footer) {
802
- interactiveMessage.footer = {
803
- text: message.footer
804
- };
805
- }
806
- if ('title' in message && !!message.title) {
807
- interactiveMessage.header = {
808
- title: message.title,
809
- subtitle: message.subtitle,
810
- hasMediaAttachment: false
811
- };
812
- }
813
- if ('contextInfo' in message && !!message.contextInfo) {
814
- interactiveMessage.contextInfo = message.contextInfo;
815
- }
816
- if ('mentions' in message && !!message.mentions) {
817
- interactiveMessage.contextInfo = { mentionedJid: message.mentions };
818
- }
819
- m = { interactiveMessage };
820
- }
821
- if ('viewOnce' in message && !!message.viewOnce) {
822
- m = { viewOnceMessage: { message: m } };
823
- }
824
- if ('mentions' in message && ((_o = message.mentions) === null || _o === void 0 ? void 0 : _o.length)) {
825
- const [messageType] = Object.keys(m);
826
- m[messageType].contextInfo = m[messageType] || {};
827
- m[messageType].contextInfo.mentionedJid = message.mentions;
828
- }
829
- if ('edit' in message) {
830
- m = {
831
- protocolMessage: {
832
- key: message.edit,
833
- editedMessage: m,
834
- timestampMs: Date.now(),
835
- type: Types_1.WAProto.Message.ProtocolMessage.Type.MESSAGE_EDIT
836
- }
837
- };
838
- }
839
- if ('contextInfo' in message && !!message.contextInfo) {
840
- const [messageType] = Object.keys(m);
841
- m[messageType] = m[messageType] || {};
842
- m[messageType].contextInfo = {
843
- ...m[messageType].contextInfo,
844
- ...message.contextInfo
845
- };
846
- }
847
- return Types_1.WAProto.Message.fromObject(m);
848
- };
849
- exports.generateWAMessageContent = generateWAMessageContent;
850
- const generateWAMessageFromContent = (jid, message, options) => {
851
- // set timestamp to now
852
- // if not specified
853
- if (!options.timestamp) {
854
- options.timestamp = new Date();
855
- }
856
- const innerMessage = (0, exports.normalizeMessageContent)(message);
857
- const key = (0, exports.getContentType)(innerMessage);
858
- const timestamp = (0, generics_1.unixTimestampSeconds)(options.timestamp);
859
- const { quoted, userJid } = options;
860
- // only set quoted if isn't a newsletter message
861
- if (quoted && !(0, WABinary_1.isJidNewsletter)(jid)) {
862
- const participant = quoted.key.fromMe ? userJid : (quoted.participant || quoted.key.participant || quoted.key.remoteJid);
863
- let quotedMsg = (0, exports.normalizeMessageContent)(quoted.message);
864
- const msgType = (0, exports.getContentType)(quotedMsg);
865
- // strip any redundant properties
866
- if (quotedMsg) {
867
- quotedMsg = WAProto_1.proto.Message.fromObject({ [msgType]: quotedMsg[msgType] });
868
- const quotedContent = quotedMsg[msgType];
869
- if (typeof quotedContent === 'object' && quotedContent && 'contextInfo' in quotedContent) {
870
- delete quotedContent.contextInfo;
871
- }
872
- const contextInfo = innerMessage[key].contextInfo || {};
873
- contextInfo.participant = (0, WABinary_1.jidNormalizedUser)(participant);
874
- contextInfo.stanzaId = quoted.key.id;
875
- contextInfo.quotedMessage = quotedMsg;
876
- // if a participant is quoted, then it must be a group
877
- // hence, remoteJid of group must also be entered
878
- if (jid !== quoted.key.remoteJid) {
879
- contextInfo.remoteJid = quoted.key.remoteJid;
880
- }
881
- innerMessage[key].contextInfo = contextInfo;
882
- }
883
- }
884
- if (
885
- // if we want to send a disappearing message
886
- !!(options === null || options === void 0 ? void 0 : options.ephemeralExpiration) &&
887
- // and it's not a protocol message -- delete, toggle disappear message
888
- key !== 'protocolMessage' &&
889
- // already not converted to disappearing message
890
- key !== 'ephemeralMessage' &&
891
- // newsletter not accept disappearing messages
892
- !(0, WABinary_1.isJidNewsletter)(jid)) {
893
- innerMessage[key].contextInfo = {
894
- ...(innerMessage[key].contextInfo || {}),
895
- expiration: options.ephemeralExpiration || Defaults_1.WA_DEFAULT_EPHEMERAL,
896
- //ephemeralSettingTimestamp: options.ephemeralOptions.eph_setting_ts?.toString()
897
- };
898
- }
899
- message = Types_1.WAProto.Message.fromObject(message);
900
- const messageJSON = {
901
- key: {
902
- remoteJid: jid,
903
- fromMe: true,
904
- id: (options === null || options === void 0 ? void 0 : options.messageId) || (0, generics_1.generateMessageIDV2)(),
905
- },
906
- message: message,
907
- messageTimestamp: timestamp,
908
- messageStubParameters: [],
909
- participant: (0, WABinary_1.isJidGroup)(jid) || (0, WABinary_1.isJidStatusBroadcast)(jid) ? userJid : undefined,
910
- status: Types_1.WAMessageStatus.PENDING
911
- };
912
- return Types_1.WAProto.WebMessageInfo.fromObject(messageJSON);
913
- };
914
- exports.generateWAMessageFromContent = generateWAMessageFromContent;
915
- const generateWAMessage = async (jid, content, options) => {
916
- var _a;
917
- // ensure msg ID is with every log
918
- options.logger = (_a = options === null || options === void 0 ? void 0 : options.logger) === null || _a === void 0 ? void 0 : _a.child({ msgId: options.messageId });
919
- return (0, exports.generateWAMessageFromContent)(jid, await (0, exports.generateWAMessageContent)(content, { newsletter: (0, WABinary_1.isJidNewsletter)(jid), ...options }), options);
920
- };
921
- exports.generateWAMessage = generateWAMessage;
922
- /** Get the key to access the true type of content */
923
- const getContentType = (content) => {
924
- if (content) {
925
- const keys = Object.keys(content);
926
- const key = keys.find(k => (k === 'conversation' || k.includes('Message')) && k !== 'senderKeyDistributionMessage');
927
- return key;
928
- }
929
- };
930
- exports.getContentType = getContentType;
931
- /**
932
- * Normalizes ephemeral, view once messages to regular message content
933
- * Eg. image messages in ephemeral messages, in view once messages etc.
934
- * @param content
935
- * @returns
936
- */
937
- const normalizeMessageContent = (content) => {
938
- if (!content) {
939
- return undefined;
940
- }
941
- // set max iterations to prevent an infinite loop
942
- for (let i = 0; i < 5; i++) {
943
- const inner = getFutureProofMessage(content);
944
- if (!inner) {
945
- break;
946
- }
947
- content = inner.message;
948
- }
949
- return content;
950
- function getFutureProofMessage(message) {
951
- return ((message === null || message === void 0 ? void 0 : message.ephemeralMessage)
952
- || (message === null || message === void 0 ? void 0 : message.viewOnceMessage)
953
- || (message === null || message === void 0 ? void 0 : message.documentWithCaptionMessage)
954
- || (message === null || message === void 0 ? void 0 : message.viewOnceMessageV2)
955
- || (message === null || message === void 0 ? void 0 : message.viewOnceMessageV2Extension)
956
- || (message === null || message === void 0 ? void 0 : message.editedMessage)
957
- || (message === null || message === void 0 ? void 0 : message.groupMentionedMessage)
958
- || (message === null || message === void 0 ? void 0 : message.botInvokeMessage)
959
- || (message === null || message === void 0 ? void 0 : message.lottieStickerMessage)
960
- || (message === null || message === void 0 ? void 0 : message.eventCoverImage)
961
- || (message === null || message === void 0 ? void 0 : message.statusMentionMessage)
962
- || (message === null || message === void 0 ? void 0 : message.pollCreationOptionImageMessage)
963
- || (message === null || message === void 0 ? void 0 : message.associatedChildMessage)
964
- || (message === null || message === void 0 ? void 0 : message.groupStatusMentionMessage)
965
- || (message === null || message === void 0 ? void 0 : message.pollCreationMessageV4)
966
- || (message === null || message === void 0 ? void 0 : message.pollCreationMessageV5)
967
- || (message === null || message === void 0 ? void 0 : message.statusAddYours)
968
- || (message === null || message === void 0 ? void 0 : message.groupStatusMessage)
969
- || (message === null || message === void 0 ? void 0 : message.limitSharingMessage)
970
- || (message === null || message === void 0 ? void 0 : message.botTaskMessage)
971
- || (message === null || message === void 0 ? void 0 : message.questionMessage)
972
- || (message === null || message === void 0 ? void 0 : message.groupStatusMessageV2)
973
- || (message === null || message === void 0 ? void 0 : message.botForwardedMessage));
974
- }
975
- };
976
- exports.normalizeMessageContent = normalizeMessageContent;
977
- /**
978
- * Extract the true message content from a message
979
- * Eg. extracts the inner message from a disappearing message/view once message
980
- */
981
- const extractMessageContent = (content) => {
982
- var _a, _b, _c, _d, _e, _f;
983
- const extractFromTemplateMessage = (msg) => {
984
- if (msg.imageMessage) {
985
- return { imageMessage: msg.imageMessage };
986
- }
987
- else if (msg.documentMessage) {
988
- return { documentMessage: msg.documentMessage };
989
- }
990
- else if (msg.videoMessage) {
991
- return { videoMessage: msg.videoMessage };
992
- }
993
- else if (msg.locationMessage) {
994
- return { locationMessage: msg.locationMessage };
995
- }
996
- else {
997
- return {
998
- conversation: 'contentText' in msg
999
- ? msg.contentText
1000
- : ('hydratedContentText' in msg ? msg.hydratedContentText : '')
1001
- };
1002
- }
1003
- };
1004
- content = (0, exports.normalizeMessageContent)(content);
1005
- if (content === null || content === void 0 ? void 0 : content.buttonsMessage) {
1006
- return extractFromTemplateMessage(content.buttonsMessage);
1007
- }
1008
- if ((_a = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _a === void 0 ? void 0 : _a.hydratedFourRowTemplate) {
1009
- return extractFromTemplateMessage((_b = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _b === void 0 ? void 0 : _b.hydratedFourRowTemplate);
1010
- }
1011
- if ((_c = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _c === void 0 ? void 0 : _c.hydratedTemplate) {
1012
- return extractFromTemplateMessage((_d = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _d === void 0 ? void 0 : _d.hydratedTemplate);
1013
- }
1014
- if ((_e = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _e === void 0 ? void 0 : _e.fourRowTemplate) {
1015
- return extractFromTemplateMessage((_f = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _f === void 0 ? void 0 : _f.fourRowTemplate);
1016
- }
1017
- return content;
1018
- };
1019
- exports.extractMessageContent = extractMessageContent;
1020
- /**
1021
- * Returns the device predicted by message ID
1022
- */
1023
- const getDevice = (id) => /^3A.{18}$/.test(id) ? 'ios' :
1024
- /^3E.{20}$/.test(id) ? 'web' :
1025
- /^(.{21}|.{32})$/.test(id) ? 'android' :
1026
- /^(3F|.{18}$)/.test(id) ? 'desktop' :
1027
- 'unknown';
1028
- exports.getDevice = getDevice;
1029
- /** Upserts a receipt in the message */
1030
- const updateMessageWithReceipt = (msg, receipt) => {
1031
- msg.userReceipt = msg.userReceipt || [];
1032
- const recp = msg.userReceipt.find(m => m.userJid === receipt.userJid);
1033
- if (recp) {
1034
- Object.assign(recp, receipt);
1035
- }
1036
- else {
1037
- msg.userReceipt.push(receipt);
1038
- }
1039
- };
1040
- exports.updateMessageWithReceipt = updateMessageWithReceipt;
1041
- /** Update the message with a new reaction */
1042
- const updateMessageWithReaction = (msg, reaction) => {
1043
- const authorID = (0, generics_1.getKeyAuthor)(reaction.key);
1044
- const reactions = (msg.reactions || [])
1045
- .filter(r => (0, generics_1.getKeyAuthor)(r.key) !== authorID);
1046
- reaction.text = reaction.text || '';
1047
- reactions.push(reaction);
1048
- msg.reactions = reactions;
1049
- };
1050
- exports.updateMessageWithReaction = updateMessageWithReaction;
1051
- /** Update the message with a new poll update */
1052
- const updateMessageWithPollUpdate = (msg, update) => {
1053
- var _a, _b;
1054
- const authorID = (0, generics_1.getKeyAuthor)(update.pollUpdateMessageKey);
1055
- const reactions = (msg.pollUpdates || [])
1056
- .filter(r => (0, generics_1.getKeyAuthor)(r.pollUpdateMessageKey) !== authorID);
1057
- if ((_b = (_a = update.vote) === null || _a === void 0 ? void 0 : _a.selectedOptions) === null || _b === void 0 ? void 0 : _b.length) {
1058
- reactions.push(update);
1059
- }
1060
- msg.pollUpdates = reactions;
1061
- };
1062
- exports.updateMessageWithPollUpdate = updateMessageWithPollUpdate;
1063
- /**
1064
- * Aggregates all poll updates in a poll.
1065
- * @param msg the poll creation message
1066
- * @param meId your jid
1067
- * @returns A list of options & their voters
1068
- */
1069
- function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
1070
- var _a, _b, _c;
1071
- const opts = ((_a = message === null || message === void 0 ? void 0 : message.pollCreationMessage) === null || _a === void 0 ? void 0 : _a.options) || ((_b = message === null || message === void 0 ? void 0 : message.pollCreationMessageV2) === null || _b === void 0 ? void 0 : _b.options) || ((_c = message === null || message === void 0 ? void 0 : message.pollCreationMessageV3) === null || _c === void 0 ? void 0 : _c.options) || [];
1072
- const voteHashMap = opts.reduce((acc, opt) => {
1073
- const hash = (0, crypto_2.sha256)(Buffer.from(opt.optionName || '')).toString();
1074
- acc[hash] = {
1075
- name: opt.optionName || '',
1076
- voters: []
1077
- };
1078
- return acc;
1079
- }, {});
1080
- for (const update of pollUpdates || []) {
1081
- const { vote } = update;
1082
- if (!vote) {
1083
- continue;
1084
- }
1085
- for (const option of vote.selectedOptions || []) {
1086
- const hash = option.toString();
1087
- let data = voteHashMap[hash];
1088
- if (!data) {
1089
- voteHashMap[hash] = {
1090
- name: 'Unknown',
1091
- voters: []
1092
- };
1093
- data = voteHashMap[hash];
1094
- }
1095
- voteHashMap[hash].voters.push((0, generics_1.getKeyAuthor)(update.pollUpdateMessageKey, meId));
1096
- }
1097
- }
1098
- return Object.values(voteHashMap);
1099
- }
1100
- /** Given a list of message keys, aggregates them by chat & sender. Useful for sending read receipts in bulk */
1101
- const aggregateMessageKeysNotFromMe = (keys) => {
1102
- const keyMap = {};
1103
- for (const { remoteJid, id, participant, fromMe } of keys) {
1104
- if (!fromMe) {
1105
- const uqKey = `${remoteJid}:${participant || ''}`;
1106
- if (!keyMap[uqKey]) {
1107
- keyMap[uqKey] = {
1108
- jid: remoteJid,
1109
- participant: participant,
1110
- messageIds: []
1111
- };
1112
- }
1113
- keyMap[uqKey].messageIds.push(id);
1114
- }
1115
- }
1116
- return Object.values(keyMap);
1117
- };
1118
- exports.aggregateMessageKeysNotFromMe = aggregateMessageKeysNotFromMe;
1119
- const REUPLOAD_REQUIRED_STATUS = [410, 404];
1120
- /**
1121
- * Downloads the given message. Throws an error if it's not a media message
1122
- */
1123
- const downloadMediaMessage = async (message, type, options, ctx) => {
1124
- const result = await downloadMsg()
1125
- .catch(async (error) => {
1126
- var _a;
1127
- if (ctx) {
1128
- if (axios_1.default.isAxiosError(error)) {
1129
- // check if the message requires a reupload
1130
- if (REUPLOAD_REQUIRED_STATUS.includes((_a = error.response) === null || _a === void 0 ? void 0 : _a.status)) {
1131
- ctx.logger.info({ key: message.key }, 'sending reupload media request...');
1132
- // request reupload
1133
- message = await ctx.reuploadRequest(message);
1134
- const result = await downloadMsg();
1135
- return result;
1136
- }
1137
- }
1138
- }
1139
- throw error;
1140
- });
1141
- return result;
1142
- async function downloadMsg() {
1143
- const mContent = (0, exports.extractMessageContent)(message.message);
1144
- if (!mContent) {
1145
- throw new boom_1.Boom('No message present', { statusCode: 400, data: message });
1146
- }
1147
- const contentType = (0, exports.getContentType)(mContent);
1148
- let mediaType = contentType === null || contentType === void 0 ? void 0 : contentType.replace('Message', '');
1149
- const media = mContent[contentType];
1150
- if (!media || typeof media !== 'object' || (!('url' in media) && !('thumbnailDirectPath' in media))) {
1151
- throw new boom_1.Boom(`"${contentType}" message is not a media message`);
1152
- }
1153
- let download;
1154
- if ('thumbnailDirectPath' in media && !('url' in media)) {
1155
- download = {
1156
- directPath: media.thumbnailDirectPath,
1157
- mediaKey: media.mediaKey
1158
- };
1159
- mediaType = 'thumbnail-link';
1160
- }
1161
- else {
1162
- download = media;
1163
- }
1164
- const stream = await (0, messages_media_1.downloadContentFromMessage)(download, mediaType, options);
1165
- if (type === 'buffer') {
1166
- const bufferArray = [];
1167
- for await (const chunk of stream) {
1168
- bufferArray.push(chunk);
1169
- }
1170
- return Buffer.concat(bufferArray);
1171
- }
1172
- return stream;
1173
- }
1174
- };
1175
- exports.downloadMediaMessage = downloadMediaMessage;
1176
- /** Checks whether the given message is a media message; if it is returns the inner content */
1177
- const assertMediaContent = (content) => {
1178
- content = (0, exports.extractMessageContent)(content);
1179
- const mediaContent = (content === null || content === void 0 ? void 0 : content.documentMessage)
1180
- || (content === null || content === void 0 ? void 0 : content.imageMessage)
1181
- || (content === null || content === void 0 ? void 0 : content.videoMessage)
1182
- || (content === null || content === void 0 ? void 0 : content.audioMessage)
1183
- || (content === null || content === void 0 ? void 0 : content.stickerMessage);
1184
- if (!mediaContent) {
1185
- throw new boom_1.Boom('given message is not a media message', { statusCode: 400, data: content });
1186
- }
1187
- return mediaContent;
1188
- };
1189
- exports.assertMediaContent = assertMediaContent;
1190
-
1191
- // Cache per ottimizzare le conversioni LID/JID
1192
- const lidCache = new Map();
1193
- const jidCache = new Map();
1194
- const CACHE_TTL = 5 * 60 * 1000; // 5 minuti
1195
- const MAX_CACHE_SIZE = 10000; // Massimo 10k entries
1196
-
1197
- // Funzione per pulire cache scadute
1198
- const cleanExpiredCache = (cache) => {
1199
- const now = Date.now();
1200
- for (const [key, value] of cache.entries()) {
1201
- if (now - value.timestamp > CACHE_TTL) {
1202
- cache.delete(key);
1203
- }
1204
- }
1205
-
1206
- // Se la cache è troppo grande, rimuovi le entry più vecchie
1207
- if (cache.size > MAX_CACHE_SIZE) {
1208
- const entries = Array.from(cache.entries());
1209
- entries.sort((a, b) => a[1].timestamp - b[1].timestamp);
1210
- const toRemove = entries.slice(0, Math.floor(MAX_CACHE_SIZE * 0.2));
1211
- toRemove.forEach(([key]) => cache.delete(key));
1212
- }
1213
- };
1214
-
1215
- // Pulisci cache ogni 2 minuti
1216
- setInterval(() => {
1217
- cleanExpiredCache(lidCache);
1218
- cleanExpiredCache(jidCache);
1219
- }, 2 * 60 * 1000);
1220
-
1221
- const toJid = (id) => {
1222
- try {
1223
- if (!id || typeof id !== 'string') {
1224
- return '';
1225
- }
1226
-
1227
- // Controlla cache
1228
- if (jidCache.has(id)) {
1229
- const cached = jidCache.get(id);
1230
- if (Date.now() - cached.timestamp < CACHE_TTL) {
1231
- return cached.result;
1232
- } else {
1233
- jidCache.delete(id);
1234
- }
1235
- }
1236
-
1237
- let result = '';
1238
-
1239
- if (id.endsWith('@lid')) {
1240
- result = id.replace('@lid', '@s.whatsapp.net');
1241
- } else if (id.includes('@')) {
1242
- result = id;
1243
- } else {
1244
- result = `${id}@s.whatsapp.net`;
1245
- }
1246
-
1247
- // Salva in cache
1248
- jidCache.set(id, {
1249
- result,
1250
- timestamp: Date.now()
1251
- });
1252
-
1253
- return result;
1254
- } catch (error) {
1255
- console.error('Error in toJid:', error.message, 'Input:', id);
1256
- return id || '';
1257
- }
1258
- };
1259
- exports.toJid = toJid;
1260
-
1261
- const getSenderLid = (message) => {
1262
- try {
1263
- if (!message?.key) {
1264
- throw new Error('Invalid message: missing key property');
1265
- }
1266
-
1267
- const sender = message.key.participant || message.key.remoteJid;
1268
- if (!sender) {
1269
- throw new Error('Invalid message: missing sender information');
1270
- }
1271
-
1272
- // Controlla cache
1273
- if (lidCache.has(sender)) {
1274
- const cached = lidCache.get(sender);
1275
- if (Date.now() - cached.timestamp < CACHE_TTL) {
1276
- return cached.result;
1277
- } else {
1278
- lidCache.delete(sender);
1279
- }
1280
- }
1281
-
1282
- const decoded = (0, WABinary_1.jidDecode)(sender);
1283
- if (!decoded?.user) {
1284
- throw new Error(`Invalid JID format: ${sender}`);
1285
- }
1286
-
1287
- const user = decoded.user;
1288
- const lid = (0, WABinary_1.jidEncode)(user, 'lid');
1289
-
1290
- const result = {
1291
- jid: sender,
1292
- lid,
1293
- isValid: true,
1294
- user,
1295
- timestamp: Date.now()
1296
- };
1297
-
1298
- // Log solo in debug mode
1299
- if (process.env.DEBUG_LID === 'true') {
1300
- console.log('sender lid:', lid, 'user:', user);
1301
- }
1302
-
1303
- // Salva in cache
1304
- lidCache.set(sender, {
1305
- result,
1306
- timestamp: Date.now()
1307
- });
1308
-
1309
- return result;
1310
-
1311
- } catch (error) {
1312
- console.error('Error in getSenderLid:', error.message, 'Message:', message?.key);
1313
-
1314
- // Ritorna un oggetto di fallback valido
1315
- return {
1316
- jid: message?.key?.remoteJid || 'unknown',
1317
- lid: 'unknown@lid',
1318
- isValid: false,
1319
- user: 'unknown',
1320
- error: error.message,
1321
- timestamp: Date.now()
1322
- };
1323
- }
1324
- };
1325
- exports.getSenderLid = getSenderLid;
1326
-
1327
- // Utility functions per performance e debugging
1328
- const getCacheStats = () => {
1329
- return {
1330
- lidCache: {
1331
- size: lidCache.size,
1332
- maxSize: MAX_CACHE_SIZE,
1333
- ttl: CACHE_TTL
1334
- },
1335
- jidCache: {
1336
- size: jidCache.size,
1337
- maxSize: MAX_CACHE_SIZE,
1338
- ttl: CACHE_TTL
1339
- }
1340
- };
1341
- };
1342
- exports.getCacheStats = getCacheStats;
1343
-
1344
- const clearCache = () => {
1345
- lidCache.clear();
1346
- jidCache.clear();
1347
- console.log(chalk.bold.magenta('- 🎐 | Cache cancellata correttamente'));
1348
- };
1349
- exports.clearCache = clearCache;
1350
-
1351
- const validateJid = (jid) => {
1352
- if (!jid || typeof jid !== 'string') {
1353
- return { isValid: false, error: 'Invalid JID: must be a non-empty string' };
1354
- }
1355
-
1356
- const parts = jid.split('@');
1357
- if (parts.length !== 2) {
1358
- return { isValid: false, error: 'Invalid JID format: must contain exactly one @' };
1359
- }
1360
-
1361
- const [user, server] = parts;
1362
- if (!user || !server) {
1363
- return { isValid: false, error: 'Invalid JID: user and server parts cannot be empty' };
1364
- }
1365
-
1366
- const validServers = ['s.whatsapp.net', 'g.us', 'broadcast', 'newsletter', 'lid', 'c.us'];
1367
- if (!validServers.includes(server)) {
1368
- return { isValid: false, error: `Invalid server: ${server}. Must be one of: ${validServers.join(', ')}` };
1369
- }
1370
-
1371
- return { isValid: true };
1372
- };
1373
- exports.validateJid = validateJid;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.assertMediaContent = exports.downloadMediaMessage = exports.aggregateMessageKeysNotFromMe = exports.updateMessageWithPollUpdate = exports.updateMessageWithReaction = exports.updateMessageWithReceipt = exports.getDevice = exports.extractMessageContent = exports.normalizeMessageContent = exports.getContentType = exports.generateWAMessage = exports.generateWAMessageFromContent = exports.generateWAMessageContent = exports.generateForwardMessageContent = exports.prepareDisappearingMessageSettingContent = exports.prepareWAMessageMedia = exports.generateLinkPreviewIfRequired = exports.extractUrlFromText = void 0;
7
+ exports.getAggregateVotesInPollMessage = getAggregateVotesInPollMessage;
8
+ const boom_1 = require("@hapi/boom");
9
+ const axios_1 = __importDefault(require("axios"));
10
+ const crypto_1 = require("crypto");
11
+ const fs_1 = require("fs");
12
+ const WAProto_1 = require("../../WAProto");
13
+ const Defaults_1 = require("../Defaults");
14
+ const Types_1 = require("../Types");
15
+ const WABinary_1 = require("../WABinary");
16
+ const crypto_2 = require("./crypto");
17
+ const generics_1 = require("./generics");
18
+ const messages_media_1 = require("./messages-media");
19
+ const MIMETYPE_MAP = {
20
+ image: 'image/jpeg',
21
+ video: 'video/mp4',
22
+ document: 'application/pdf',
23
+ audio: 'audio/ogg; codecs=opus',
24
+ sticker: 'image/webp',
25
+ 'product-catalog-image': 'image/jpeg',
26
+ };
27
+ const MessageTypeProto = {
28
+ 'image': Types_1.WAProto.Message.ImageMessage,
29
+ 'video': Types_1.WAProto.Message.VideoMessage,
30
+ 'audio': Types_1.WAProto.Message.AudioMessage,
31
+ 'sticker': Types_1.WAProto.Message.StickerMessage,
32
+ 'document': Types_1.WAProto.Message.DocumentMessage,
33
+ };
34
+ const ButtonType = WAProto_1.proto.Message.ButtonsMessage.HeaderType;
35
+ /**
36
+ * Uses a regex to test whether the string contains a URL, and returns the URL if it does.
37
+ * @param text eg. hello https://google.com
38
+ * @returns the URL, eg. https://google.com
39
+ */
40
+ const extractUrlFromText = (text) => { var _a; return (_a = text.match(Defaults_1.URL_REGEX)) === null || _a === void 0 ? void 0 : _a[0]; };
41
+ exports.extractUrlFromText = extractUrlFromText;
42
+ const generateLinkPreviewIfRequired = async (text, getUrlInfo, logger) => {
43
+ const url = (0, exports.extractUrlFromText)(text);
44
+ if (!!getUrlInfo && url) {
45
+ try {
46
+ const urlInfo = await getUrlInfo(url);
47
+ return urlInfo;
48
+ }
49
+ catch (error) { // ignore if fails
50
+ logger === null || logger === void 0 ? void 0 : logger.warn({ trace: error.stack }, 'url generation failed');
51
+ }
52
+ }
53
+ };
54
+ exports.generateLinkPreviewIfRequired = generateLinkPreviewIfRequired;
55
+ const assertColor = async (color) => {
56
+ let assertedColor;
57
+ if (typeof color === 'number') {
58
+ assertedColor = color > 0 ? color : 0xffffffff + Number(color) + 1;
59
+ }
60
+ else {
61
+ let hex = color.trim().replace('#', '');
62
+ if (hex.length <= 6) {
63
+ hex = 'FF' + hex.padStart(6, '0');
64
+ }
65
+ assertedColor = parseInt(hex, 16);
66
+ return assertedColor;
67
+ }
68
+ };
69
+ const prepareWAMessageMedia = async (message, options) => {
70
+ const logger = options.logger;
71
+ let mediaType;
72
+ for (const key of Defaults_1.MEDIA_KEYS) {
73
+ if (key in message) {
74
+ mediaType = key;
75
+ }
76
+ }
77
+ if (!mediaType) {
78
+ throw new boom_1.Boom('Invalid media type', { statusCode: 400 });
79
+ }
80
+ const uploadData = {
81
+ ...message,
82
+ media: message[mediaType]
83
+ };
84
+ delete uploadData[mediaType];
85
+ // check if cacheable + generate cache key
86
+ const cacheableKey = typeof uploadData.media === 'object' &&
87
+ ('url' in uploadData.media) &&
88
+ !!uploadData.media.url &&
89
+ !!options.mediaCache && (
90
+ // generate the key
91
+ mediaType + ':' + uploadData.media.url.toString());
92
+ if (mediaType === 'document' && !uploadData.fileName) {
93
+ uploadData.fileName = 'file';
94
+ }
95
+ if (!uploadData.mimetype) {
96
+ uploadData.mimetype = MIMETYPE_MAP[mediaType];
97
+ }
98
+ // check for cache hit
99
+ if (cacheableKey) {
100
+ const mediaBuff = options.mediaCache.get(cacheableKey);
101
+ if (mediaBuff) {
102
+ logger === null || logger === void 0 ? void 0 : logger.debug({ cacheableKey }, 'got media cache hit');
103
+ const obj = Types_1.WAProto.Message.decode(mediaBuff);
104
+ const key = `${mediaType}Message`;
105
+ Object.assign(obj[key], { ...uploadData, media: undefined });
106
+ return obj;
107
+ }
108
+ }
109
+ const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined';
110
+ const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') &&
111
+ (typeof uploadData['jpegThumbnail'] === 'undefined');
112
+ const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === true;
113
+ const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === true;
114
+ const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation;
115
+ const { mediaKey, encWriteStream, bodyPath, fileEncSha256, fileSha256, fileLength, didSaveToTmpPath, } = await (options.newsletter ? messages_media_1.prepareStream : messages_media_1.encryptedStream)(uploadData.media, options.mediaTypeOverride || mediaType, {
116
+ logger,
117
+ saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
118
+ opts: options.options
119
+ });
120
+ // url safe Base64 encode the SHA256 hash of the body
121
+ const fileEncSha256B64 = (options.newsletter ? fileSha256 : fileEncSha256 !== null && fileEncSha256 !== void 0 ? fileEncSha256 : fileSha256).toString('base64');
122
+ const [{ mediaUrl, directPath, handle }] = await Promise.all([
123
+ (async () => {
124
+ const result = await options.upload(encWriteStream, { fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs });
125
+ logger === null || logger === void 0 ? void 0 : logger.debug({ mediaType, cacheableKey }, 'uploaded media');
126
+ return result;
127
+ })(),
128
+ (async () => {
129
+ try {
130
+ if (requiresThumbnailComputation) {
131
+ const { thumbnail, originalImageDimensions } = await (0, messages_media_1.generateThumbnail)(bodyPath, mediaType, options);
132
+ uploadData.jpegThumbnail = thumbnail;
133
+ if (!uploadData.width && originalImageDimensions) {
134
+ uploadData.width = originalImageDimensions.width;
135
+ uploadData.height = originalImageDimensions.height;
136
+ logger === null || logger === void 0 ? void 0 : logger.debug('set dimensions');
137
+ }
138
+ logger === null || logger === void 0 ? void 0 : logger.debug('generated thumbnail');
139
+ }
140
+ if (requiresDurationComputation) {
141
+ uploadData.seconds = await (0, messages_media_1.getAudioDuration)(bodyPath);
142
+ logger === null || logger === void 0 ? void 0 : logger.debug('computed audio duration');
143
+ }
144
+ if (requiresWaveformProcessing) {
145
+ uploadData.waveform = await (0, messages_media_1.getAudioWaveform)(bodyPath, logger);
146
+ logger === null || logger === void 0 ? void 0 : logger.debug('processed waveform');
147
+ }
148
+ if (requiresAudioBackground) {
149
+ uploadData.backgroundArgb = await assertColor(options.backgroundColor);
150
+ logger === null || logger === void 0 ? void 0 : logger.debug('computed backgroundColor audio status');
151
+ }
152
+ }
153
+ catch (error) {
154
+ logger === null || logger === void 0 ? void 0 : logger.warn({ trace: error.stack }, 'failed to obtain extra info');
155
+ }
156
+ })(),
157
+ ])
158
+ .finally(async () => {
159
+ if (!Buffer.isBuffer(encWriteStream)) {
160
+ encWriteStream.destroy();
161
+ }
162
+ // remove tmp files
163
+ if (didSaveToTmpPath && bodyPath) {
164
+ try {
165
+ await fs_1.promises.access(bodyPath);
166
+ await fs_1.promises.unlink(bodyPath);
167
+ logger === null || logger === void 0 ? void 0 : logger.debug('removed tmp file');
168
+ }
169
+ catch (error) {
170
+ logger === null || logger === void 0 ? void 0 : logger.warn('failed to remove tmp file');
171
+ }
172
+ }
173
+ });
174
+ const obj = Types_1.WAProto.Message.fromObject({
175
+ [`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
176
+ url: handle ? undefined : mediaUrl,
177
+ directPath,
178
+ mediaKey: mediaKey,
179
+ fileEncSha256: fileEncSha256,
180
+ fileSha256,
181
+ fileLength,
182
+ mediaKeyTimestamp: handle ? undefined : (0, generics_1.unixTimestampSeconds)(),
183
+ ...uploadData,
184
+ media: undefined
185
+ })
186
+ });
187
+ if (uploadData.ptv) {
188
+ obj.ptvMessage = obj.videoMessage;
189
+ delete obj.videoMessage;
190
+ }
191
+ if (cacheableKey) {
192
+ logger === null || logger === void 0 ? void 0 : logger.debug({ cacheableKey }, 'set cache');
193
+ options.mediaCache.set(cacheableKey, Types_1.WAProto.Message.encode(obj).finish());
194
+ }
195
+ return obj;
196
+ };
197
+ exports.prepareWAMessageMedia = prepareWAMessageMedia;
198
+ const prepareDisappearingMessageSettingContent = (ephemeralExpiration) => {
199
+ ephemeralExpiration = ephemeralExpiration || 0;
200
+ const content = {
201
+ ephemeralMessage: {
202
+ message: {
203
+ protocolMessage: {
204
+ type: Types_1.WAProto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
205
+ ephemeralExpiration
206
+ }
207
+ }
208
+ }
209
+ };
210
+ return Types_1.WAProto.Message.fromObject(content);
211
+ };
212
+ exports.prepareDisappearingMessageSettingContent = prepareDisappearingMessageSettingContent;
213
+ /**
214
+ * Generate forwarded message content like WA does
215
+ * @param message the message to forward
216
+ * @param options.forceForward will show the message as forwarded even if it is from you
217
+ */
218
+ const generateForwardMessageContent = (message, forceForward) => {
219
+ var _a;
220
+ let content = message.message;
221
+ if (!content) {
222
+ throw new boom_1.Boom('no content in message', { statusCode: 400 });
223
+ }
224
+ // hacky copy
225
+ content = (0, exports.normalizeMessageContent)(content);
226
+ content = WAProto_1.proto.Message.decode(WAProto_1.proto.Message.encode(content).finish());
227
+ let key = Object.keys(content)[0];
228
+ let score = ((_a = content[key].contextInfo) === null || _a === void 0 ? void 0 : _a.forwardingScore) || 0;
229
+ score += message.key.fromMe && !forceForward ? 0 : 1;
230
+ if (key === 'conversation') {
231
+ content.extendedTextMessage = { text: content[key] };
232
+ delete content.conversation;
233
+ key = 'extendedTextMessage';
234
+ }
235
+ if (score > 0) {
236
+ content[key].contextInfo = { forwardingScore: score, isForwarded: true };
237
+ }
238
+ else {
239
+ content[key].contextInfo = {};
240
+ }
241
+ return content;
242
+ };
243
+ exports.generateForwardMessageContent = generateForwardMessageContent;
244
+ const generateWAMessageContent = async (message, options) => {
245
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
246
+ var _p, _q;
247
+
248
+ // iOS-specific externalAdReply handling
249
+ if (message.contextInfo?.externalAdReply) {
250
+ const externalAdReply = message.contextInfo.externalAdReply;
251
+
252
+ // If thumbnailUrl is provided but no thumbnail binary data, try to fetch and convert for iOS
253
+ if (externalAdReply.thumbnailUrl && !externalAdReply.thumbnail) {
254
+ try {
255
+ // Attempt to download the thumbnail image for iOS compatibility
256
+ const axiosResponse = await axios_1.default.get(externalAdReply.thumbnailUrl, {
257
+ responseType: 'arraybuffer',
258
+ timeout: 5000,
259
+ headers: {
260
+ 'User-Agent': 'WhatsApp/2.23.20.15 iOS/16.0 Device/iPhone'
261
+ }
262
+ });
263
+
264
+ if (axiosResponse.status === 200) {
265
+ externalAdReply.thumbnail = Buffer.from(axiosResponse.data);
266
+ // Clear thumbnailUrl since we now have binary data
267
+ delete externalAdReply.thumbnailUrl;
268
+ }
269
+ } catch (error) {
270
+ // If thumbnail download fails, keep thumbnailUrl as is
271
+ options.logger?.warn('Failed to download externalAdReply thumbnail for iOS:', error.message);
272
+ }
273
+ }
274
+
275
+ // Update the contextInfo with modified externalAdReply
276
+ message.contextInfo.externalAdReply = externalAdReply;
277
+ }
278
+
279
+ let m = {};
280
+ if ('text' in message) {
281
+ const extContent = { text: message.text };
282
+ let urlInfo = message.linkPreview;
283
+ if (urlInfo === true) {
284
+ urlInfo = await (0, exports.generateLinkPreviewIfRequired)(message.text, options.getUrlInfo, options.logger);
285
+ }
286
+ if (urlInfo && typeof urlInfo === 'object') {
287
+ extContent.matchedText = urlInfo['matched-text'];
288
+ extContent.jpegThumbnail = urlInfo.jpegThumbnail;
289
+ extContent.description = urlInfo.description;
290
+ extContent.title = urlInfo.title;
291
+ extContent.previewType = 0;
292
+ const img = urlInfo.highQualityThumbnail;
293
+ if (img) {
294
+ extContent.thumbnailDirectPath = img.directPath;
295
+ extContent.mediaKey = img.mediaKey;
296
+ extContent.mediaKeyTimestamp = img.mediaKeyTimestamp;
297
+ extContent.thumbnailWidth = img.width;
298
+ extContent.thumbnailHeight = img.height;
299
+ extContent.thumbnailSha256 = img.fileSha256;
300
+ extContent.thumbnailEncSha256 = img.fileEncSha256;
301
+ }
302
+ }
303
+ if (options.backgroundColor) {
304
+ extContent.backgroundArgb = await assertColor(options.backgroundColor);
305
+ }
306
+ if (options.font) {
307
+ extContent.font = options.font;
308
+ }
309
+ m.extendedTextMessage = extContent;
310
+ }
311
+ else if ('contacts' in message) {
312
+ const contactLen = message.contacts.contacts.length;
313
+ if (!contactLen) {
314
+ throw new boom_1.Boom('require atleast 1 contact', { statusCode: 400 });
315
+ }
316
+ if (contactLen === 1) {
317
+ m.contactMessage = Types_1.WAProto.Message.ContactMessage.fromObject(message.contacts.contacts[0]);
318
+ }
319
+ else {
320
+ m.contactsArrayMessage = Types_1.WAProto.Message.ContactsArrayMessage.fromObject(message.contacts);
321
+ }
322
+ }
323
+ else if ('location' in message) {
324
+ m.locationMessage = Types_1.WAProto.Message.LocationMessage.fromObject(message.location);
325
+ }
326
+ else if ('liveLocation' in message) {
327
+ m.liveLocationMessage = Types_1.WAProto.Message.LiveLocationMessage.fromObject(message.liveLocation);
328
+ }
329
+ else if ('react' in message) {
330
+ if (!message.react.senderTimestampMs) {
331
+ message.react.senderTimestampMs = Date.now();
332
+ }
333
+ m.reactionMessage = Types_1.WAProto.Message.ReactionMessage.fromObject(message.react);
334
+ }
335
+ else if ('delete' in message) {
336
+ m.protocolMessage = {
337
+ key: message.delete,
338
+ type: Types_1.WAProto.Message.ProtocolMessage.Type.REVOKE
339
+ };
340
+ }
341
+ else if ('forward' in message) {
342
+ m = (0, exports.generateForwardMessageContent)(message.forward, message.force);
343
+ }
344
+ else if ('disappearingMessagesInChat' in message) {
345
+ const exp = typeof message.disappearingMessagesInChat === 'boolean' ?
346
+ (message.disappearingMessagesInChat ? Defaults_1.WA_DEFAULT_EPHEMERAL : 0) :
347
+ message.disappearingMessagesInChat;
348
+ m = (0, exports.prepareDisappearingMessageSettingContent)(exp);
349
+ }
350
+ else if ('groupInvite' in message) {
351
+ m.groupInviteMessage = {};
352
+ m.groupInviteMessage.inviteCode = message.groupInvite.inviteCode;
353
+ m.groupInviteMessage.inviteExpiration = message.groupInvite.inviteExpiration;
354
+ m.groupInviteMessage.caption = message.groupInvite.text;
355
+ m.groupInviteMessage.groupJid = message.groupInvite.jid;
356
+ m.groupInviteMessage.groupName = message.groupInvite.subject;
357
+ //TODO: use built-in interface and get disappearing mode info etc.
358
+ //TODO: cache / use store!?
359
+ if (options.getProfilePicUrl) {
360
+ const pfpUrl = await options.getProfilePicUrl(message.groupInvite.jid, 'preview');
361
+ if (pfpUrl) {
362
+ const resp = await axios_1.default.get(pfpUrl, { responseType: 'arraybuffer' });
363
+ if (resp.status === 200) {
364
+ m.groupInviteMessage.jpegThumbnail = resp.data;
365
+ }
366
+ }
367
+ }
368
+ }
369
+ else if ('pin' in message) {
370
+ m.pinInChatMessage = {};
371
+ m.messageContextInfo = {};
372
+ m.pinInChatMessage.key = message.pin;
373
+ m.pinInChatMessage.type = message.type;
374
+ m.pinInChatMessage.senderTimestampMs = Date.now();
375
+ m.messageContextInfo.messageAddOnDurationInSecs = message.type === 1 ? message.time || 86400 : 0;
376
+ }
377
+ else if ('keep' in message) {
378
+ m.keepInChatMessage = {};
379
+ m.keepInChatMessage.key = message.keep;
380
+ m.keepInChatMessage.keepType = message.type;
381
+ m.keepInChatMessage.timestampMs = Date.now();
382
+ }
383
+ else if ('call' in message) {
384
+ m = {
385
+ scheduledCallCreationMessage: {
386
+ scheduledTimestampMs: (_a = message.call.time) !== null && _a !== void 0 ? _a : Date.now(),
387
+ callType: (_b = message.call.type) !== null && _b !== void 0 ? _b : 1,
388
+ title: message.call.title
389
+ }
390
+ };
391
+ }
392
+ else if ('paymentInvite' in message) {
393
+ m.paymentInviteMessage = {
394
+ serviceType: message.paymentInvite.type,
395
+ expiryTimestamp: message.paymentInvite.expiry
396
+ };
397
+ }
398
+ else if ('buttonReply' in message) {
399
+ switch (message.type) {
400
+ case 'template':
401
+ m.templateButtonReplyMessage = {
402
+ selectedDisplayText: message.buttonReply.displayText,
403
+ selectedId: message.buttonReply.id,
404
+ selectedIndex: message.buttonReply.index,
405
+ };
406
+ break;
407
+ case 'plain':
408
+ m.buttonsResponseMessage = {
409
+ selectedButtonId: message.buttonReply.id,
410
+ selectedDisplayText: message.buttonReply.displayText,
411
+ type: WAProto_1.proto.Message.ButtonsResponseMessage.Type.DISPLAY_TEXT,
412
+ };
413
+ break;
414
+ }
415
+ }
416
+ else if ('ptv' in message && message.ptv) {
417
+ const { videoMessage } = await (0, exports.prepareWAMessageMedia)({ video: message.video }, options);
418
+ m.ptvMessage = videoMessage;
419
+ }
420
+ else if ('product' in message) {
421
+ const { imageMessage } = await (0, exports.prepareWAMessageMedia)({ image: message.product.productImage }, options);
422
+ m.productMessage = Types_1.WAProto.Message.ProductMessage.fromObject({
423
+ ...message,
424
+ product: {
425
+ ...message.product,
426
+ productImage: imageMessage,
427
+ }
428
+ });
429
+ }
430
+ else if ('order' in message) {
431
+ m.orderMessage = Types_1.WAProto.Message.OrderMessage.fromObject({
432
+ orderId: message.order.id,
433
+ thumbnail: message.order.thumbnail,
434
+ itemCount: message.order.itemCount,
435
+ status: message.order.status,
436
+ surface: message.order.surface,
437
+ orderTitle: message.order.title,
438
+ message: message.order.text,
439
+ sellerJid: message.order.seller,
440
+ token: message.order.token,
441
+ totalAmount1000: message.order.amount,
442
+ totalCurrencyCode: message.order.currency
443
+ });
444
+ }
445
+ else if ('listReply' in message) {
446
+ m.listResponseMessage = { ...message.listReply };
447
+ }
448
+ else if ('poll' in message) {
449
+ (_p = message.poll).selectableCount || (_p.selectableCount = 0);
450
+ (_q = message.poll).toAnnouncementGroup || (_q.toAnnouncementGroup = false);
451
+ if (!Array.isArray(message.poll.values)) {
452
+ throw new boom_1.Boom('Invalid poll values', { statusCode: 400 });
453
+ }
454
+ if (message.poll.selectableCount < 0
455
+ || message.poll.selectableCount > message.poll.values.length) {
456
+ throw new boom_1.Boom(`poll.selectableCount in poll should be >= 0 and <= ${message.poll.values.length}`, { statusCode: 400 });
457
+ }
458
+ m.messageContextInfo = {
459
+ // encKey
460
+ messageSecret: message.poll.messageSecret || (0, crypto_1.randomBytes)(32),
461
+ };
462
+ const pollCreationMessage = {
463
+ name: message.poll.name,
464
+ selectableOptionsCount: message.poll.selectableCount,
465
+ options: message.poll.values.map(optionName => ({ optionName })),
466
+ };
467
+ if (message.poll.toAnnouncementGroup) {
468
+ // poll v2 is for community announcement groups (single select and multiple)
469
+ m.pollCreationMessageV2 = pollCreationMessage;
470
+ }
471
+ else {
472
+ if (message.poll.selectableCount === 1) {
473
+ // poll v3 is for single select polls
474
+ m.pollCreationMessageV3 = pollCreationMessage;
475
+ }
476
+ else {
477
+ // poll for multiple choice polls
478
+ m.pollCreationMessage = pollCreationMessage;
479
+ }
480
+ }
481
+ }
482
+ else if ('event' in message) {
483
+ m.messageContextInfo = {
484
+ messageSecret: message.event.messageSecret || (0, crypto_1.randomBytes)(32),
485
+ };
486
+ m.eventMessage = { ...message.event };
487
+ }
488
+ else if ('inviteAdmin' in message) {
489
+ m.newsletterAdminInviteMessage = {};
490
+ m.newsletterAdminInviteMessage.inviteExpiration = message.inviteAdmin.inviteExpiration;
491
+ m.newsletterAdminInviteMessage.caption = message.inviteAdmin.text;
492
+ m.newsletterAdminInviteMessage.newsletterJid = message.inviteAdmin.jid;
493
+ m.newsletterAdminInviteMessage.newsletterName = message.inviteAdmin.subject;
494
+ m.newsletterAdminInviteMessage.jpegThumbnail = message.inviteAdmin.thumbnail;
495
+ }
496
+ else if ('requestPayment' in message) {
497
+ const reqPayment = message.requestPayment;
498
+ const sticker = reqPayment.sticker ?
499
+ await (0, exports.prepareWAMessageMedia)({ sticker: reqPayment.sticker, ...options }, options)
500
+ : null;
501
+ let notes = {};
502
+ if (reqPayment.sticker) {
503
+ notes = {
504
+ stickerMessage: {
505
+ ...sticker.stickerMessage,
506
+ contextInfo: reqPayment.contextInfo
507
+ }
508
+ };
509
+ }
510
+ else if (reqPayment.note) {
511
+ notes = {
512
+ extendedTextMessage: {
513
+ text: reqPayment.note,
514
+ contextInfo: reqPayment.contextInfo,
515
+ }
516
+ };
517
+ }
518
+ else {
519
+ throw new boom_1.Boom('Invalid media type', { statusCode: 400 });
520
+ }
521
+ m.requestPaymentMessage = Types_1.WAProto.Message.RequestPaymentMessage.fromObject({
522
+ expiryTimestamp: reqPayment.expiryTimestamp || reqPayment.expiry,
523
+ amount1000: reqPayment.amount1000 || reqPayment.amount,
524
+ currencyCodeIso4217: reqPayment.currencyCodeIso4217 || reqPayment.currency,
525
+ requestFrom: reqPayment.requestFrom || reqPayment.from,
526
+ noteMessage: { ...notes },
527
+ background: reqPayment.background,
528
+ // Aggiungi altri parametri se disponibili
529
+ ...reqPayment
530
+ });
531
+ }
532
+ else if ('sharePhoneNumber' in message) {
533
+ m.protocolMessage = {
534
+ type: WAProto_1.proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER
535
+ };
536
+ }
537
+ else if ('requestPhoneNumber' in message) {
538
+ m.requestPhoneNumberMessage = {};
539
+ }
540
+ else if ('call' in message) {
541
+ m.callMessage = Types_1.WAProto.Message.CallMessage.fromObject(message.call);
542
+ }
543
+ else if ('newsletterMessage' in message) {
544
+ m.newsletterMessage = Types_1.WAProto.Message.NewsletterMessage.fromObject(message.newsletterMessage);
545
+ }
546
+ else if ('album' in message) {
547
+ const imageMessages = message.album.filter(item => 'image' in item);
548
+ const videoMessages = message.album.filter(item => 'video' in item);
549
+ m.albumMessage = WAProto_1.proto.Message.AlbumMessage.fromObject({
550
+ expectedImageCount: imageMessages.length,
551
+ expectedVideoCount: videoMessages.length,
552
+ });
553
+ }
554
+ else {
555
+ m = await (0, exports.prepareWAMessageMedia)(message, options);
556
+ }
557
+ if ('buttons' in message && !!message.buttons) {
558
+ const buttonsMessage = {
559
+ buttons: message.buttons.map(b => ({ ...b, type: WAProto_1.proto.Message.ButtonsMessage.Button.Type.RESPONSE }))
560
+ };
561
+ if ('text' in message) {
562
+ buttonsMessage.contentText = message.text;
563
+ buttonsMessage.headerType = ButtonType.EMPTY;
564
+ }
565
+ else {
566
+ if ('caption' in message) {
567
+ buttonsMessage.contentText = message.caption;
568
+ }
569
+ const type = Object.keys(m)[0].replace('Message', '').toUpperCase();
570
+ buttonsMessage.headerType = ButtonType[type];
571
+ Object.assign(buttonsMessage, m);
572
+ }
573
+ if ('title' in message && !!message.title) {
574
+ buttonsMessage.text = message.title,
575
+ buttonsMessage.headerType = ButtonType.TEXT;
576
+ }
577
+ if ('footer' in message && !!message.footer) {
578
+ buttonsMessage.footerText = message.footer;
579
+ }
580
+ if ('contextInfo' in message && !!message.contextInfo) {
581
+ buttonsMessage.contextInfo = message.contextInfo;
582
+ }
583
+ if ('mentions' in message && !!message.mentions) {
584
+ buttonsMessage.contextInfo = { mentionedJid: message.mentions };
585
+ }
586
+ m = { buttonsMessage };
587
+ }
588
+ else if ('templateButtons' in message && !!message.templateButtons) {
589
+ const templateMsg = {
590
+ hydratedButtons: message.templateButtons
591
+ };
592
+ if ('text' in message) {
593
+ templateMsg.hydratedContentText = message.text;
594
+ }
595
+ else if ('caption' in message) {
596
+ templateMsg.hydratedContentText = message.caption;
597
+ }
598
+ if ('footer' in message && !!message.footer) {
599
+ templateMsg.hydratedFooterText = message.footer;
600
+ }
601
+ // Add media to template if present
602
+ if (m && Object.keys(m).length > 0) {
603
+ Object.assign(templateMsg, m);
604
+ }
605
+ m = {
606
+ templateMessage: {
607
+ fourRowTemplate: templateMsg,
608
+ hydratedTemplate: templateMsg
609
+ }
610
+ };
611
+ }
612
+ if ('sections' in message && !!message.sections) {
613
+ const listMessage = {
614
+ sections: message.sections,
615
+ buttonText: message.buttonText,
616
+ title: message.title,
617
+ footerText: message.footer,
618
+ description: message.text,
619
+ listType: WAProto_1.proto.Message.ListMessage.ListType.SINGLE_SELECT
620
+ };
621
+ m = { listMessage };
622
+ }
623
+ if ('interactiveButtons' in message && !!message.interactiveButtons) {
624
+ const interactiveMessage = {
625
+ nativeFlowMessage: Types_1.WAProto.Message.InteractiveMessage.NativeFlowMessage.fromObject({
626
+ buttons: message.interactiveButtons,
627
+ })
628
+ };
629
+ if ('text' in message) {
630
+ interactiveMessage.body = {
631
+ text: message.text
632
+ };
633
+ }
634
+ else if ('caption' in message) {
635
+ interactiveMessage.body = {
636
+ text: message.caption
637
+ };
638
+ interactiveMessage.header = {
639
+ title: message.title,
640
+ subtitle: message.subtitle,
641
+ hasMediaAttachment: (_j = message === null || message === void 0 ? void 0 : message.media) !== null && _j !== void 0 ? _j : false,
642
+ };
643
+ Object.assign(interactiveMessage.header, m);
644
+ }
645
+ if ('footer' in message && !!message.footer) {
646
+ interactiveMessage.footer = {
647
+ text: message.footer
648
+ };
649
+ }
650
+ if ('title' in message && !!message.title) {
651
+ interactiveMessage.header = {
652
+ title: message.title,
653
+ subtitle: message.subtitle,
654
+ hasMediaAttachment: (_k = message === null || message === void 0 ? void 0 : message.media) !== null && _k !== void 0 ? _k : false,
655
+ };
656
+ Object.assign(interactiveMessage.header, m);
657
+ }
658
+ if ('contextInfo' in message && !!message.contextInfo) {
659
+ interactiveMessage.contextInfo = message.contextInfo;
660
+ }
661
+ if ('mentions' in message && !!message.mentions) {
662
+ interactiveMessage.contextInfo = { mentionedJid: message.mentions };
663
+ }
664
+ m = { interactiveMessage };
665
+ }
666
+ if ('shop' in message && !!message.shop) {
667
+ const interactiveMessage = {
668
+ shopStorefrontMessage: Types_1.WAProto.Message.InteractiveMessage.ShopMessage.fromObject({
669
+ surface: message.shop,
670
+ id: message.id
671
+ })
672
+ };
673
+ if ('text' in message) {
674
+ interactiveMessage.body = {
675
+ text: message.text
676
+ };
677
+ }
678
+ else if ('caption' in message) {
679
+ interactiveMessage.body = {
680
+ text: message.caption
681
+ };
682
+ interactiveMessage.header = {
683
+ title: message.title,
684
+ subtitle: message.subtitle,
685
+ hasMediaAttachment: (_l = message === null || message === void 0 ? void 0 : message.media) !== null && _l !== void 0 ? _l : false,
686
+ };
687
+ Object.assign(interactiveMessage.header, m);
688
+ }
689
+ if ('footer' in message && !!message.footer) {
690
+ interactiveMessage.footer = {
691
+ text: message.footer
692
+ };
693
+ }
694
+ if ('title' in message && !!message.title) {
695
+ interactiveMessage.header = {
696
+ title: message.title,
697
+ subtitle: message.subtitle,
698
+ hasMediaAttachment: (_m = message === null || message === void 0 ? void 0 : message.media) !== null && _m !== void 0 ? _m : false,
699
+ };
700
+ Object.assign(interactiveMessage.header, m);
701
+ }
702
+ if ('contextInfo' in message && !!message.contextInfo) {
703
+ interactiveMessage.contextInfo = message.contextInfo;
704
+ }
705
+ if ('mentions' in message && !!message.mentions) {
706
+ interactiveMessage.contextInfo = { mentionedJid: message.mentions };
707
+ }
708
+ m = { interactiveMessage };
709
+ }
710
+ if ('cards' in message && !!message.cards && message.cards.length > 0) {
711
+ const carouselCards = await Promise.all(message.cards.map(async (card) => {
712
+ const cardMessage = {
713
+ header: {
714
+ title: card.title || '',
715
+ hasMediaAttachment: !!(card.image || card.video)
716
+ }
717
+ };
718
+
719
+ // Add body as separate field if present
720
+ if (card.body) {
721
+ cardMessage.body = {
722
+ text: card.body
723
+ };
724
+ }
725
+ // Handle media attachments
726
+ if (card.image) {
727
+ const mediaMessage = await prepareWAMessageMedia({ image: card.image }, options);
728
+ if (mediaMessage.imageMessage) {
729
+ cardMessage.header.imageMessage = mediaMessage.imageMessage;
730
+ }
731
+ }
732
+ else if (card.video) {
733
+ const mediaMessage = await prepareWAMessageMedia({ video: card.video }, options);
734
+ if (mediaMessage.videoMessage) {
735
+ cardMessage.header.videoMessage = mediaMessage.videoMessage;
736
+ }
737
+ }
738
+ // Handle buttons
739
+ if (card.buttons && card.buttons.length > 0) {
740
+ cardMessage.nativeFlowMessage = {
741
+ buttons: card.buttons.map(button => ({
742
+ name: button.name,
743
+ buttonParamsJson: button.buttonParamsJson
744
+ }))
745
+ };
746
+ }
747
+ // Add footer if present
748
+ if (card.footer) {
749
+ cardMessage.footer = {
750
+ text: card.footer
751
+ };
752
+ }
753
+ return cardMessage;
754
+ }));
755
+ const interactiveMessage = {
756
+ carouselMessage: Types_1.WAProto.Message.InteractiveMessage.CarouselMessage.fromObject({
757
+ cards: carouselCards,
758
+ messageVersion: 1
759
+ })
760
+ };
761
+ if ('text' in message) {
762
+ interactiveMessage.body = {
763
+ text: message.text
764
+ };
765
+ }
766
+ if ('footer' in message && !!message.footer) {
767
+ interactiveMessage.footer = {
768
+ text: message.footer
769
+ };
770
+ }
771
+ if ('title' in message && !!message.title) {
772
+ interactiveMessage.header = {
773
+ title: message.title,
774
+ subtitle: message.subtitle,
775
+ hasMediaAttachment: false
776
+ };
777
+ }
778
+ if ('contextInfo' in message && !!message.contextInfo) {
779
+ interactiveMessage.contextInfo = message.contextInfo;
780
+ }
781
+ if ('mentions' in message && !!message.mentions) {
782
+ interactiveMessage.contextInfo = { mentionedJid: message.mentions };
783
+ }
784
+ m = { interactiveMessage };
785
+ }
786
+ if ('viewOnce' in message && !!message.viewOnce) {
787
+ m = { viewOnceMessage: { message: m } };
788
+ }
789
+ if ('mentions' in message && ((_o = message.mentions) === null || _o === void 0 ? void 0 : _o.length)) {
790
+ const [messageType] = Object.keys(m);
791
+ m[messageType].contextInfo = m[messageType] || {};
792
+ m[messageType].contextInfo.mentionedJid = message.mentions;
793
+ }
794
+ if ('edit' in message) {
795
+ m = {
796
+ protocolMessage: {
797
+ key: message.edit,
798
+ editedMessage: m,
799
+ timestampMs: Date.now(),
800
+ type: Types_1.WAProto.Message.ProtocolMessage.Type.MESSAGE_EDIT
801
+ }
802
+ };
803
+ }
804
+ if ('contextInfo' in message && !!message.contextInfo) {
805
+ const [messageType] = Object.keys(m);
806
+ m[messageType] = m[messageType] || {};
807
+ m[messageType].contextInfo = {
808
+ ...m[messageType].contextInfo,
809
+ ...message.contextInfo
810
+ };
811
+ }
812
+ return Types_1.WAProto.Message.fromObject(m);
813
+ };
814
+ exports.generateWAMessageContent = generateWAMessageContent;
815
+ const generateWAMessageFromContent = (jid, message, options) => {
816
+ // set timestamp to now
817
+ // if not specified
818
+ if (!options.timestamp) {
819
+ options.timestamp = new Date();
820
+ }
821
+ const innerMessage = (0, exports.normalizeMessageContent)(message);
822
+ const key = (0, exports.getContentType)(innerMessage);
823
+ const timestamp = (0, generics_1.unixTimestampSeconds)(options.timestamp);
824
+ const { quoted, userJid } = options;
825
+ // only set quoted if isn't a newsletter message
826
+ if (quoted && !(0, WABinary_1.isJidNewsletter)(jid)) {
827
+ const participant = quoted.key.fromMe ? userJid : (quoted.participant || quoted.key.participant || quoted.key.remoteJid);
828
+ let quotedMsg = (0, exports.normalizeMessageContent)(quoted.message);
829
+ const msgType = (0, exports.getContentType)(quotedMsg);
830
+ // strip any redundant properties
831
+ if (quotedMsg) {
832
+ quotedMsg = WAProto_1.proto.Message.fromObject({ [msgType]: quotedMsg[msgType] });
833
+ const quotedContent = quotedMsg[msgType];
834
+ if (typeof quotedContent === 'object' && quotedContent && 'contextInfo' in quotedContent) {
835
+ delete quotedContent.contextInfo;
836
+ }
837
+ const contextInfo = innerMessage[key].contextInfo || {};
838
+ contextInfo.participant = (0, WABinary_1.jidNormalizedUser)(participant);
839
+ contextInfo.stanzaId = quoted.key.id;
840
+ contextInfo.quotedMessage = quotedMsg;
841
+ // if a participant is quoted, then it must be a group
842
+ // hence, remoteJid of group must also be entered
843
+ if (jid !== quoted.key.remoteJid) {
844
+ contextInfo.remoteJid = quoted.key.remoteJid;
845
+ }
846
+ innerMessage[key].contextInfo = contextInfo;
847
+ }
848
+ }
849
+ if (
850
+ // if we want to send a disappearing message
851
+ !!(options === null || options === void 0 ? void 0 : options.ephemeralExpiration) &&
852
+ // and it's not a protocol message -- delete, toggle disappear message
853
+ key !== 'protocolMessage' &&
854
+ // already not converted to disappearing message
855
+ key !== 'ephemeralMessage' &&
856
+ // newsletter not accept disappearing messages
857
+ !(0, WABinary_1.isJidNewsletter)(jid)) {
858
+ innerMessage[key].contextInfo = {
859
+ ...(innerMessage[key].contextInfo || {}),
860
+ expiration: options.ephemeralExpiration || Defaults_1.WA_DEFAULT_EPHEMERAL,
861
+ //ephemeralSettingTimestamp: options.ephemeralOptions.eph_setting_ts?.toString()
862
+ };
863
+ }
864
+ message = Types_1.WAProto.Message.fromObject(message);
865
+ const messageJSON = {
866
+ key: {
867
+ remoteJid: jid,
868
+ fromMe: true,
869
+ id: (options === null || options === void 0 ? void 0 : options.messageId) || (0, generics_1.generateMessageIDV2)(),
870
+ },
871
+ message: message,
872
+ messageTimestamp: timestamp,
873
+ messageStubParameters: [],
874
+ participant: (0, WABinary_1.isJidGroup)(jid) || (0, WABinary_1.isJidStatusBroadcast)(jid) ? userJid : undefined,
875
+ status: Types_1.WAMessageStatus.PENDING
876
+ };
877
+ return Types_1.WAProto.WebMessageInfo.fromObject(messageJSON);
878
+ };
879
+ exports.generateWAMessageFromContent = generateWAMessageFromContent;
880
+ const generateWAMessage = async (jid, content, options) => {
881
+ var _a;
882
+ // ensure msg ID is with every log
883
+ options.logger = (_a = options === null || options === void 0 ? void 0 : options.logger) === null || _a === void 0 ? void 0 : _a.child({ msgId: options.messageId });
884
+ return (0, exports.generateWAMessageFromContent)(jid, await (0, exports.generateWAMessageContent)(content, { newsletter: (0, WABinary_1.isJidNewsletter)(jid), ...options }), options);
885
+ };
886
+ exports.generateWAMessage = generateWAMessage;
887
+ /** Get the key to access the true type of content */
888
+ const getContentType = (content) => {
889
+ if (content) {
890
+ const keys = Object.keys(content);
891
+ const key = keys.find(k => (k === 'conversation' || k.includes('Message')) && k !== 'senderKeyDistributionMessage');
892
+ return key;
893
+ }
894
+ };
895
+ exports.getContentType = getContentType;
896
+ /**
897
+ * Normalizes ephemeral, view once messages to regular message content
898
+ * Eg. image messages in ephemeral messages, in view once messages etc.
899
+ * @param content
900
+ * @returns
901
+ */
902
+ const normalizeMessageContent = (content) => {
903
+ if (!content) {
904
+ return undefined;
905
+ }
906
+ // set max iterations to prevent an infinite loop
907
+ for (let i = 0; i < 5; i++) {
908
+ const inner = getFutureProofMessage(content);
909
+ if (!inner) {
910
+ break;
911
+ }
912
+ content = inner.message;
913
+ }
914
+ return content;
915
+ function getFutureProofMessage(message) {
916
+ return ((message === null || message === void 0 ? void 0 : message.ephemeralMessage)
917
+ || (message === null || message === void 0 ? void 0 : message.viewOnceMessage)
918
+ || (message === null || message === void 0 ? void 0 : message.documentWithCaptionMessage)
919
+ || (message === null || message === void 0 ? void 0 : message.viewOnceMessageV2)
920
+ || (message === null || message === void 0 ? void 0 : message.viewOnceMessageV2Extension)
921
+ || (message === null || message === void 0 ? void 0 : message.editedMessage)
922
+ || (message === null || message === void 0 ? void 0 : message.groupMentionedMessage)
923
+ || (message === null || message === void 0 ? void 0 : message.botInvokeMessage)
924
+ || (message === null || message === void 0 ? void 0 : message.lottieStickerMessage)
925
+ || (message === null || message === void 0 ? void 0 : message.eventCoverImage)
926
+ || (message === null || message === void 0 ? void 0 : message.statusMentionMessage)
927
+ || (message === null || message === void 0 ? void 0 : message.pollCreationOptionImageMessage)
928
+ || (message === null || message === void 0 ? void 0 : message.associatedChildMessage)
929
+ || (message === null || message === void 0 ? void 0 : message.groupStatusMentionMessage)
930
+ || (message === null || message === void 0 ? void 0 : message.pollCreationMessageV4)
931
+ || (message === null || message === void 0 ? void 0 : message.pollCreationMessageV5)
932
+ || (message === null || message === void 0 ? void 0 : message.statusAddYours)
933
+ || (message === null || message === void 0 ? void 0 : message.groupStatusMessage)
934
+ || (message === null || message === void 0 ? void 0 : message.limitSharingMessage)
935
+ || (message === null || message === void 0 ? void 0 : message.botTaskMessage)
936
+ || (message === null || message === void 0 ? void 0 : message.questionMessage)
937
+ || (message === null || message === void 0 ? void 0 : message.groupStatusMessageV2)
938
+ || (message === null || message === void 0 ? void 0 : message.botForwardedMessage));
939
+ }
940
+ };
941
+ exports.normalizeMessageContent = normalizeMessageContent;
942
+ /**
943
+ * Extract the true message content from a message
944
+ * Eg. extracts the inner message from a disappearing message/view once message
945
+ */
946
+ const extractMessageContent = (content) => {
947
+ var _a, _b, _c, _d, _e, _f;
948
+ const extractFromTemplateMessage = (msg) => {
949
+ if (msg.imageMessage) {
950
+ return { imageMessage: msg.imageMessage };
951
+ }
952
+ else if (msg.documentMessage) {
953
+ return { documentMessage: msg.documentMessage };
954
+ }
955
+ else if (msg.videoMessage) {
956
+ return { videoMessage: msg.videoMessage };
957
+ }
958
+ else if (msg.locationMessage) {
959
+ return { locationMessage: msg.locationMessage };
960
+ }
961
+ else {
962
+ return {
963
+ conversation: 'contentText' in msg
964
+ ? msg.contentText
965
+ : ('hydratedContentText' in msg ? msg.hydratedContentText : '')
966
+ };
967
+ }
968
+ };
969
+ content = (0, exports.normalizeMessageContent)(content);
970
+ if (content === null || content === void 0 ? void 0 : content.buttonsMessage) {
971
+ return extractFromTemplateMessage(content.buttonsMessage);
972
+ }
973
+ if ((_a = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _a === void 0 ? void 0 : _a.hydratedFourRowTemplate) {
974
+ return extractFromTemplateMessage((_b = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _b === void 0 ? void 0 : _b.hydratedFourRowTemplate);
975
+ }
976
+ if ((_c = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _c === void 0 ? void 0 : _c.hydratedTemplate) {
977
+ return extractFromTemplateMessage((_d = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _d === void 0 ? void 0 : _d.hydratedTemplate);
978
+ }
979
+ if ((_e = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _e === void 0 ? void 0 : _e.fourRowTemplate) {
980
+ return extractFromTemplateMessage((_f = content === null || content === void 0 ? void 0 : content.templateMessage) === null || _f === void 0 ? void 0 : _f.fourRowTemplate);
981
+ }
982
+ return content;
983
+ };
984
+ exports.extractMessageContent = extractMessageContent;
985
+ /**
986
+ * Returns the device predicted by message ID
987
+ */
988
+ const getDevice = (id) => /^3A.{18}$/.test(id) ? 'ios' :
989
+ /^3E.{20}$/.test(id) ? 'web' :
990
+ /^(.{21}|.{32})$/.test(id) ? 'android' :
991
+ /^(3F|.{18}$)/.test(id) ? 'desktop' :
992
+ 'unknown';
993
+ exports.getDevice = getDevice;
994
+ /** Upserts a receipt in the message */
995
+ const updateMessageWithReceipt = (msg, receipt) => {
996
+ msg.userReceipt = msg.userReceipt || [];
997
+ const recp = msg.userReceipt.find(m => m.userJid === receipt.userJid);
998
+ if (recp) {
999
+ Object.assign(recp, receipt);
1000
+ }
1001
+ else {
1002
+ msg.userReceipt.push(receipt);
1003
+ }
1004
+ };
1005
+ exports.updateMessageWithReceipt = updateMessageWithReceipt;
1006
+ /** Update the message with a new reaction */
1007
+ const updateMessageWithReaction = (msg, reaction) => {
1008
+ const authorID = (0, generics_1.getKeyAuthor)(reaction.key);
1009
+ const reactions = (msg.reactions || [])
1010
+ .filter(r => (0, generics_1.getKeyAuthor)(r.key) !== authorID);
1011
+ reaction.text = reaction.text || '';
1012
+ reactions.push(reaction);
1013
+ msg.reactions = reactions;
1014
+ };
1015
+ exports.updateMessageWithReaction = updateMessageWithReaction;
1016
+ /** Update the message with a new poll update */
1017
+ const updateMessageWithPollUpdate = (msg, update) => {
1018
+ var _a, _b;
1019
+ const authorID = (0, generics_1.getKeyAuthor)(update.pollUpdateMessageKey);
1020
+ const reactions = (msg.pollUpdates || [])
1021
+ .filter(r => (0, generics_1.getKeyAuthor)(r.pollUpdateMessageKey) !== authorID);
1022
+ if ((_b = (_a = update.vote) === null || _a === void 0 ? void 0 : _a.selectedOptions) === null || _b === void 0 ? void 0 : _b.length) {
1023
+ reactions.push(update);
1024
+ }
1025
+ msg.pollUpdates = reactions;
1026
+ };
1027
+ exports.updateMessageWithPollUpdate = updateMessageWithPollUpdate;
1028
+ /**
1029
+ * Aggregates all poll updates in a poll.
1030
+ * @param msg the poll creation message
1031
+ * @param meId your jid
1032
+ * @returns A list of options & their voters
1033
+ */
1034
+ function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
1035
+ var _a, _b, _c;
1036
+ const opts = ((_a = message === null || message === void 0 ? void 0 : message.pollCreationMessage) === null || _a === void 0 ? void 0 : _a.options) || ((_b = message === null || message === void 0 ? void 0 : message.pollCreationMessageV2) === null || _b === void 0 ? void 0 : _b.options) || ((_c = message === null || message === void 0 ? void 0 : message.pollCreationMessageV3) === null || _c === void 0 ? void 0 : _c.options) || [];
1037
+ const voteHashMap = opts.reduce((acc, opt) => {
1038
+ const hash = (0, crypto_2.sha256)(Buffer.from(opt.optionName || '')).toString();
1039
+ acc[hash] = {
1040
+ name: opt.optionName || '',
1041
+ voters: []
1042
+ };
1043
+ return acc;
1044
+ }, {});
1045
+ for (const update of pollUpdates || []) {
1046
+ const { vote } = update;
1047
+ if (!vote) {
1048
+ continue;
1049
+ }
1050
+ for (const option of vote.selectedOptions || []) {
1051
+ const hash = option.toString();
1052
+ let data = voteHashMap[hash];
1053
+ if (!data) {
1054
+ voteHashMap[hash] = {
1055
+ name: 'Unknown',
1056
+ voters: []
1057
+ };
1058
+ data = voteHashMap[hash];
1059
+ }
1060
+ voteHashMap[hash].voters.push((0, generics_1.getKeyAuthor)(update.pollUpdateMessageKey, meId));
1061
+ }
1062
+ }
1063
+ return Object.values(voteHashMap);
1064
+ }
1065
+ /** Given a list of message keys, aggregates them by chat & sender. Useful for sending read receipts in bulk */
1066
+ const aggregateMessageKeysNotFromMe = (keys) => {
1067
+ const keyMap = {};
1068
+ for (const { remoteJid, id, participant, fromMe } of keys) {
1069
+ if (!fromMe) {
1070
+ const uqKey = `${remoteJid}:${participant || ''}`;
1071
+ if (!keyMap[uqKey]) {
1072
+ keyMap[uqKey] = {
1073
+ jid: remoteJid,
1074
+ participant: participant,
1075
+ messageIds: []
1076
+ };
1077
+ }
1078
+ keyMap[uqKey].messageIds.push(id);
1079
+ }
1080
+ }
1081
+ return Object.values(keyMap);
1082
+ };
1083
+ exports.aggregateMessageKeysNotFromMe = aggregateMessageKeysNotFromMe;
1084
+ const REUPLOAD_REQUIRED_STATUS = [410, 404];
1085
+ /**
1086
+ * Downloads the given message. Throws an error if it's not a media message
1087
+ */
1088
+ const downloadMediaMessage = async (message, type, options, ctx) => {
1089
+ const result = await downloadMsg()
1090
+ .catch(async (error) => {
1091
+ var _a;
1092
+ if (ctx) {
1093
+ if (axios_1.default.isAxiosError(error)) {
1094
+ // check if the message requires a reupload
1095
+ if (REUPLOAD_REQUIRED_STATUS.includes((_a = error.response) === null || _a === void 0 ? void 0 : _a.status)) {
1096
+ ctx.logger.info({ key: message.key }, 'sending reupload media request...');
1097
+ // request reupload
1098
+ message = await ctx.reuploadRequest(message);
1099
+ const result = await downloadMsg();
1100
+ return result;
1101
+ }
1102
+ }
1103
+ }
1104
+ throw error;
1105
+ });
1106
+ return result;
1107
+ async function downloadMsg() {
1108
+ const mContent = (0, exports.extractMessageContent)(message.message);
1109
+ if (!mContent) {
1110
+ throw new boom_1.Boom('No message present', { statusCode: 400, data: message });
1111
+ }
1112
+ const contentType = (0, exports.getContentType)(mContent);
1113
+ let mediaType = contentType === null || contentType === void 0 ? void 0 : contentType.replace('Message', '');
1114
+ const media = mContent[contentType];
1115
+ if (!media || typeof media !== 'object' || (!('url' in media) && !('thumbnailDirectPath' in media))) {
1116
+ throw new boom_1.Boom(`"${contentType}" message is not a media message`);
1117
+ }
1118
+ let download;
1119
+ if ('thumbnailDirectPath' in media && !('url' in media)) {
1120
+ download = {
1121
+ directPath: media.thumbnailDirectPath,
1122
+ mediaKey: media.mediaKey
1123
+ };
1124
+ mediaType = 'thumbnail-link';
1125
+ }
1126
+ else {
1127
+ download = media;
1128
+ }
1129
+ const stream = await (0, messages_media_1.downloadContentFromMessage)(download, mediaType, options);
1130
+ if (type === 'buffer') {
1131
+ const bufferArray = [];
1132
+ for await (const chunk of stream) {
1133
+ bufferArray.push(chunk);
1134
+ }
1135
+ return Buffer.concat(bufferArray);
1136
+ }
1137
+ return stream;
1138
+ }
1139
+ };
1140
+ exports.downloadMediaMessage = downloadMediaMessage;
1141
+ /** Checks whether the given message is a media message; if it is returns the inner content */
1142
+ const assertMediaContent = (content) => {
1143
+ content = (0, exports.extractMessageContent)(content);
1144
+ const mediaContent = (content === null || content === void 0 ? void 0 : content.documentMessage)
1145
+ || (content === null || content === void 0 ? void 0 : content.imageMessage)
1146
+ || (content === null || content === void 0 ? void 0 : content.videoMessage)
1147
+ || (content === null || content === void 0 ? void 0 : content.audioMessage)
1148
+ || (content === null || content === void 0 ? void 0 : content.stickerMessage);
1149
+ if (!mediaContent) {
1150
+ throw new boom_1.Boom('given message is not a media message', { statusCode: 400, data: content });
1151
+ }
1152
+ return mediaContent;
1153
+ };
1154
+ exports.assertMediaContent = assertMediaContent;
1155
+
1156
+ // Cache per ottimizzare le conversioni LID/JID
1157
+ const lidCache = new Map();
1158
+ const jidCache = new Map();
1159
+ const CACHE_TTL = 5 * 60 * 1000; // 5 minuti
1160
+ const MAX_CACHE_SIZE = 10000; // Massimo 10k entries
1161
+
1162
+ // Funzione per pulire cache scadute
1163
+ const cleanExpiredCache = (cache) => {
1164
+ const now = Date.now();
1165
+ for (const [key, value] of cache.entries()) {
1166
+ if (now - value.timestamp > CACHE_TTL) {
1167
+ cache.delete(key);
1168
+ }
1169
+ }
1170
+
1171
+ // Se la cache è troppo grande, rimuovi le entry più vecchie
1172
+ if (cache.size > MAX_CACHE_SIZE) {
1173
+ const entries = Array.from(cache.entries());
1174
+ entries.sort((a, b) => a[1].timestamp - b[1].timestamp);
1175
+ const toRemove = entries.slice(0, Math.floor(MAX_CACHE_SIZE * 0.2));
1176
+ toRemove.forEach(([key]) => cache.delete(key));
1177
+ }
1178
+ };
1179
+
1180
+ // Pulisci cache ogni 2 minuti
1181
+ setInterval(() => {
1182
+ cleanExpiredCache(lidCache);
1183
+ cleanExpiredCache(jidCache);
1184
+ }, 2 * 60 * 1000);
1185
+
1186
+ const toJid = (id) => {
1187
+ try {
1188
+ if (!id || typeof id !== 'string') {
1189
+ return '';
1190
+ }
1191
+
1192
+ // Controlla cache
1193
+ if (jidCache.has(id)) {
1194
+ const cached = jidCache.get(id);
1195
+ if (Date.now() - cached.timestamp < CACHE_TTL) {
1196
+ return cached.result;
1197
+ } else {
1198
+ jidCache.delete(id);
1199
+ }
1200
+ }
1201
+
1202
+ let result = '';
1203
+
1204
+ if (id.endsWith('@lid')) {
1205
+ result = id.replace('@lid', '@s.whatsapp.net');
1206
+ } else if (id.includes('@')) {
1207
+ result = id;
1208
+ } else {
1209
+ result = `${id}@s.whatsapp.net`;
1210
+ }
1211
+
1212
+ // Salva in cache
1213
+ jidCache.set(id, {
1214
+ result,
1215
+ timestamp: Date.now()
1216
+ });
1217
+
1218
+ return result;
1219
+ } catch (error) {
1220
+ console.error('Error in toJid:', error.message, 'Input:', id);
1221
+ return id || '';
1222
+ }
1223
+ };
1224
+ exports.toJid = toJid;
1225
+
1226
+ const getSenderLid = (message) => {
1227
+ try {
1228
+ if (!message?.key) {
1229
+ throw new Error('Invalid message: missing key property');
1230
+ }
1231
+
1232
+ const sender = message.key.participant || message.key.remoteJid;
1233
+ if (!sender) {
1234
+ throw new Error('Invalid message: missing sender information');
1235
+ }
1236
+
1237
+ // Controlla cache
1238
+ if (lidCache.has(sender)) {
1239
+ const cached = lidCache.get(sender);
1240
+ if (Date.now() - cached.timestamp < CACHE_TTL) {
1241
+ return cached.result;
1242
+ } else {
1243
+ lidCache.delete(sender);
1244
+ }
1245
+ }
1246
+
1247
+ const decoded = (0, WABinary_1.jidDecode)(sender);
1248
+ if (!decoded?.user) {
1249
+ throw new Error(`Invalid JID format: ${sender}`);
1250
+ }
1251
+
1252
+ const user = decoded.user;
1253
+ const lid = (0, WABinary_1.jidEncode)(user, 'lid');
1254
+
1255
+ const result = {
1256
+ jid: sender,
1257
+ lid,
1258
+ isValid: true,
1259
+ user,
1260
+ timestamp: Date.now()
1261
+ };
1262
+
1263
+ // Log solo in debug mode
1264
+ if (process.env.DEBUG_LID === 'true') {
1265
+ console.log('sender lid:', lid, 'user:', user);
1266
+ }
1267
+
1268
+ // Salva in cache
1269
+ lidCache.set(sender, {
1270
+ result,
1271
+ timestamp: Date.now()
1272
+ });
1273
+
1274
+ return result;
1275
+
1276
+ } catch (error) {
1277
+ console.error('Error in getSenderLid:', error.message, 'Message:', message?.key);
1278
+
1279
+ // Ritorna un oggetto di fallback valido
1280
+ return {
1281
+ jid: message?.key?.remoteJid || 'unknown',
1282
+ lid: 'unknown@lid',
1283
+ isValid: false,
1284
+ user: 'unknown',
1285
+ error: error.message,
1286
+ timestamp: Date.now()
1287
+ };
1288
+ }
1289
+ };
1290
+ exports.getSenderLid = getSenderLid;
1291
+
1292
+ // Utility functions per performance e debugging
1293
+ const getCacheStats = () => {
1294
+ return {
1295
+ lidCache: {
1296
+ size: lidCache.size,
1297
+ maxSize: MAX_CACHE_SIZE,
1298
+ ttl: CACHE_TTL
1299
+ },
1300
+ jidCache: {
1301
+ size: jidCache.size,
1302
+ maxSize: MAX_CACHE_SIZE,
1303
+ ttl: CACHE_TTL
1304
+ }
1305
+ };
1306
+ };
1307
+ exports.getCacheStats = getCacheStats;
1308
+
1309
+ const clearCache = () => {
1310
+ lidCache.clear();
1311
+ jidCache.clear();
1312
+ console.log('Cache cleared successfully');
1313
+ };
1314
+ exports.clearCache = clearCache;
1315
+
1316
+ const validateJid = (jid) => {
1317
+ if (!jid || typeof jid !== 'string') {
1318
+ return { isValid: false, error: 'Invalid JID: must be a non-empty string' };
1319
+ }
1320
+
1321
+ const parts = jid.split('@');
1322
+ if (parts.length !== 2) {
1323
+ return { isValid: false, error: 'Invalid JID format: must contain exactly one @' };
1324
+ }
1325
+
1326
+ const [user, server] = parts;
1327
+ if (!user || !server) {
1328
+ return { isValid: false, error: 'Invalid JID: user and server parts cannot be empty' };
1329
+ }
1330
+
1331
+ const validServers = ['s.whatsapp.net', 'g.us', 'broadcast', 'newsletter', 'lid', 'c.us'];
1332
+ if (!validServers.includes(server)) {
1333
+ return { isValid: false, error: `Invalid server: ${server}. Must be one of: ${validServers.join(', ')}` };
1334
+ }
1335
+
1336
+ return { isValid: true };
1337
+ };
1338
+ exports.validateJid = validateJid;