@gara31/void-baileys 7.0.0-rc.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/LICENSE +21 -0
  2. package/WAProto/index.js +117292 -0
  3. package/lib/Defaults/baileys-version.json +3 -0
  4. package/lib/Defaults/index.js +116 -0
  5. package/lib/Signal/Group/ciphertext-message.js +12 -0
  6. package/lib/Signal/Group/group-session-builder.js +42 -0
  7. package/lib/Signal/Group/group_cipher.js +109 -0
  8. package/lib/Signal/Group/index.js +12 -0
  9. package/lib/Signal/Group/keyhelper.js +18 -0
  10. package/lib/Signal/Group/sender-chain-key.js +32 -0
  11. package/lib/Signal/Group/sender-key-distribution-message.js +67 -0
  12. package/lib/Signal/Group/sender-key-message.js +80 -0
  13. package/lib/Signal/Group/sender-key-name.js +50 -0
  14. package/lib/Signal/Group/sender-key-record.js +47 -0
  15. package/lib/Signal/Group/sender-key-state.js +105 -0
  16. package/lib/Signal/Group/sender-message-key.js +30 -0
  17. package/lib/Signal/libsignal.js +416 -0
  18. package/lib/Signal/lid-mapping.js +189 -0
  19. package/lib/Socket/Client/index.js +3 -0
  20. package/lib/Socket/Client/types.js +11 -0
  21. package/lib/Socket/Client/websocket.js +61 -0
  22. package/lib/Socket/business.js +404 -0
  23. package/lib/Socket/chats.js +1146 -0
  24. package/lib/Socket/communities.js +505 -0
  25. package/lib/Socket/groups.js +404 -0
  26. package/lib/Socket/index.js +18 -0
  27. package/lib/Socket/messages-recv.js +1600 -0
  28. package/lib/Socket/messages-send.js +1203 -0
  29. package/lib/Socket/mex.js +56 -0
  30. package/lib/Socket/newsletter.js +240 -0
  31. package/lib/Socket/socket.js +1060 -0
  32. package/lib/Types/Auth.js +2 -0
  33. package/lib/Types/Bussines.js +2 -0
  34. package/lib/Types/Call.js +2 -0
  35. package/lib/Types/Chat.js +8 -0
  36. package/lib/Types/Contact.js +2 -0
  37. package/lib/Types/Events.js +2 -0
  38. package/lib/Types/GroupMetadata.js +2 -0
  39. package/lib/Types/Label.js +25 -0
  40. package/lib/Types/LabelAssociation.js +7 -0
  41. package/lib/Types/Message.js +11 -0
  42. package/lib/Types/Newsletter.js +31 -0
  43. package/lib/Types/Product.js +2 -0
  44. package/lib/Types/Signal.js +2 -0
  45. package/lib/Types/Socket.js +3 -0
  46. package/lib/Types/State.js +13 -0
  47. package/lib/Types/USync.js +2 -0
  48. package/lib/Types/index.js +32 -0
  49. package/lib/Utils/auth-utils.js +276 -0
  50. package/lib/Utils/browser-utils.js +32 -0
  51. package/lib/Utils/business.js +262 -0
  52. package/lib/Utils/chat-utils.js +941 -0
  53. package/lib/Utils/crypto.js +179 -0
  54. package/lib/Utils/decode-wa-message.js +333 -0
  55. package/lib/Utils/event-buffer.js +580 -0
  56. package/lib/Utils/generics.js +436 -0
  57. package/lib/Utils/history.js +103 -0
  58. package/lib/Utils/index.js +19 -0
  59. package/lib/Utils/link-preview.js +99 -0
  60. package/lib/Utils/logger.js +3 -0
  61. package/lib/Utils/lt-hash.js +56 -0
  62. package/lib/Utils/make-mutex.js +38 -0
  63. package/lib/Utils/message-retry-manager.js +181 -0
  64. package/lib/Utils/messages-media.js +727 -0
  65. package/lib/Utils/messages.js +1309 -0
  66. package/lib/Utils/noise-handler.js +162 -0
  67. package/lib/Utils/pre-key-manager.js +125 -0
  68. package/lib/Utils/process-message.js +594 -0
  69. package/lib/Utils/signal.js +194 -0
  70. package/lib/Utils/use-multi-file-auth-state.js +118 -0
  71. package/lib/Utils/validate-connection.js +240 -0
  72. package/lib/WABinary/constants.js +1301 -0
  73. package/lib/WABinary/decode.js +240 -0
  74. package/lib/WABinary/encode.js +216 -0
  75. package/lib/WABinary/generic-utils.js +104 -0
  76. package/lib/WABinary/index.js +6 -0
  77. package/lib/WABinary/jid-utils.js +95 -0
  78. package/lib/WABinary/types.js +2 -0
  79. package/lib/WAM/BinaryInfo.js +10 -0
  80. package/lib/WAM/constants.js +22863 -0
  81. package/lib/WAM/encode.js +152 -0
  82. package/lib/WAM/index.js +4 -0
  83. package/lib/WAUSync/Protocols/USyncContactProtocol.js +29 -0
  84. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +59 -0
  85. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  86. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +36 -0
  87. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +60 -0
  88. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  89. package/lib/WAUSync/Protocols/index.js +5 -0
  90. package/lib/WAUSync/USyncQuery.js +104 -0
  91. package/lib/WAUSync/USyncUser.js +23 -0
  92. package/lib/WAUSync/index.js +4 -0
  93. package/lib/index.js +11 -0
  94. package/package.json +32 -0
  95. package/readme.md +1452 -0
@@ -0,0 +1,1309 @@
1
+ import { Boom } from "@hapi/boom";
2
+ import { randomBytes } from "crypto";
3
+ import { promises as fs } from "fs";
4
+ import {} from "stream";
5
+ import { proto } from "../../WAProto/index.js";
6
+ import {
7
+ CALL_AUDIO_PREFIX,
8
+ CALL_VIDEO_PREFIX,
9
+ MEDIA_KEYS,
10
+ URL_REGEX,
11
+ WA_DEFAULT_EPHEMERAL,
12
+ } from "../Defaults/index.js";
13
+ import { WAMessageStatus, WAProto } from "../Types/index.js";
14
+ import {
15
+ isJidGroup,
16
+ isJidNewsletter,
17
+ isJidStatusBroadcast,
18
+ jidNormalizedUser,
19
+ } from "../WABinary/index.js";
20
+ import { sha256 } from "./crypto.js";
21
+ import {
22
+ generateMessageIDV2,
23
+ getKeyAuthor,
24
+ unixTimestampSeconds,
25
+ } from "./generics.js";
26
+ import {
27
+ downloadContentFromMessage,
28
+ encryptedStream,
29
+ generateThumbnail,
30
+ getAudioDuration,
31
+ getAudioWaveform,
32
+ getRawMediaUploadData,
33
+ } from "./messages-media.js";
34
+ const MIMETYPE_MAP = {
35
+ image: "image/jpeg",
36
+ video: "video/mp4",
37
+ document: "application/pdf",
38
+ audio: "audio/ogg; codecs=opus",
39
+ sticker: "image/webp",
40
+ "product-catalog-image": "image/jpeg",
41
+ };
42
+ const MessageTypeProto = {
43
+ image: WAProto.Message.ImageMessage,
44
+ video: WAProto.Message.VideoMessage,
45
+ audio: WAProto.Message.AudioMessage,
46
+ sticker: WAProto.Message.StickerMessage,
47
+ document: WAProto.Message.DocumentMessage,
48
+ };
49
+ /**
50
+ * Uses a regex to test whether the string contains a URL, and returns the URL if it does.
51
+ * @param text eg. hello https://google.com
52
+ * @returns the URL, eg. https://google.com
53
+ */
54
+ export const extractUrlFromText = (text) => text.match(URL_REGEX)?.[0];
55
+ export const generateLinkPreviewIfRequired = async (
56
+ text,
57
+ getUrlInfo,
58
+ logger,
59
+ ) => {
60
+ const url = extractUrlFromText(text);
61
+ if (!!getUrlInfo && url) {
62
+ try {
63
+ const urlInfo = await getUrlInfo(url);
64
+ return urlInfo;
65
+ } catch (error) {
66
+ // ignore if fails
67
+ logger?.warn({ trace: error.stack }, "url generation failed");
68
+ }
69
+ }
70
+ };
71
+ const assertColor = async (color) => {
72
+ let assertedColor;
73
+ if (typeof color === "number") {
74
+ assertedColor = color > 0 ? color : 0xffffffff + Number(color) + 1;
75
+ } else {
76
+ let hex = color.trim().replace("#", "");
77
+ if (hex.length <= 6) {
78
+ hex = "FF" + hex.padStart(6, "0");
79
+ }
80
+ assertedColor = parseInt(hex, 16);
81
+ return assertedColor;
82
+ }
83
+ };
84
+ export const prepareWAMessageMedia = async (message, options) => {
85
+ const logger = options.logger;
86
+ let mediaType;
87
+ for (const key of MEDIA_KEYS) {
88
+ if (key in message) {
89
+ mediaType = key;
90
+ }
91
+ }
92
+ if (!mediaType) {
93
+ throw new Boom("Invalid media type", { statusCode: 400 });
94
+ }
95
+ const uploadData = {
96
+ ...message,
97
+ media: message[mediaType],
98
+ };
99
+ delete uploadData[mediaType];
100
+ // check if cacheable + generate cache key
101
+ const cacheableKey =
102
+ typeof uploadData.media === "object" &&
103
+ "url" in uploadData.media &&
104
+ !!uploadData.media.url &&
105
+ !!options.mediaCache &&
106
+ mediaType + ":" + uploadData.media.url.toString();
107
+ if (mediaType === "document" && !uploadData.fileName) {
108
+ uploadData.fileName = "file";
109
+ }
110
+ if (!uploadData.mimetype) {
111
+ uploadData.mimetype = MIMETYPE_MAP[mediaType];
112
+ }
113
+ if (cacheableKey) {
114
+ const mediaBuff = await options.mediaCache.get(cacheableKey);
115
+ if (mediaBuff) {
116
+ logger?.debug({ cacheableKey }, "got media cache hit");
117
+ const obj = proto.Message.decode(mediaBuff);
118
+ const key = `${mediaType}Message`;
119
+ Object.assign(obj[key], { ...uploadData, media: undefined });
120
+ return obj;
121
+ }
122
+ }
123
+ const isNewsletter = !!options.jid && isJidNewsletter(options.jid);
124
+ if (isNewsletter) {
125
+ logger?.info({ key: cacheableKey }, "Preparing raw media for newsletter");
126
+ const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(
127
+ uploadData.media,
128
+ options.mediaTypeOverride || mediaType,
129
+ logger,
130
+ );
131
+ const fileSha256B64 = fileSha256.toString("base64");
132
+ const { mediaUrl, directPath } = await options.upload(filePath, {
133
+ fileEncSha256B64: fileSha256B64,
134
+ mediaType: mediaType,
135
+ timeoutMs: options.mediaUploadTimeoutMs,
136
+ });
137
+ await fs.unlink(filePath);
138
+ const obj = WAProto.Message.fromObject({
139
+ // todo: add more support here
140
+ [`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
141
+ url: mediaUrl,
142
+ directPath,
143
+ fileSha256,
144
+ fileLength,
145
+ ...uploadData,
146
+ media: undefined,
147
+ }),
148
+ });
149
+ if (uploadData.ptv) {
150
+ obj.ptvMessage = obj.videoMessage;
151
+ delete obj.videoMessage;
152
+ }
153
+ if (obj.stickerMessage) {
154
+ obj.stickerMessage.stickerSentTs = Date.now();
155
+ }
156
+ if (cacheableKey) {
157
+ logger?.debug({ cacheableKey }, "set cache");
158
+ await options.mediaCache.set(
159
+ cacheableKey,
160
+ WAProto.Message.encode(obj).finish(),
161
+ );
162
+ }
163
+ return obj;
164
+ }
165
+ const requiresDurationComputation =
166
+ mediaType === "audio" && typeof uploadData.seconds === "undefined";
167
+ const requiresThumbnailComputation =
168
+ (mediaType === "image" || mediaType === "video") &&
169
+ typeof uploadData["jpegThumbnail"] === "undefined";
170
+ const requiresWaveformProcessing =
171
+ mediaType === "audio" && uploadData.ptt === true;
172
+ const requiresAudioBackground =
173
+ options.backgroundColor && mediaType === "audio" && uploadData.ptt === true;
174
+ const requiresOriginalForSomeProcessing =
175
+ requiresDurationComputation || requiresThumbnailComputation;
176
+ const {
177
+ mediaKey,
178
+ encFilePath,
179
+ originalFilePath,
180
+ fileEncSha256,
181
+ fileSha256,
182
+ fileLength,
183
+ } = await encryptedStream(
184
+ uploadData.media,
185
+ options.mediaTypeOverride || mediaType,
186
+ {
187
+ logger,
188
+ saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
189
+ opts: options.options,
190
+ },
191
+ );
192
+ const fileEncSha256B64 = fileEncSha256.toString("base64");
193
+ const [{ mediaUrl, directPath }] = await Promise.all([
194
+ (async () => {
195
+ const result = await options.upload(encFilePath, {
196
+ fileEncSha256B64,
197
+ mediaType,
198
+ timeoutMs: options.mediaUploadTimeoutMs,
199
+ });
200
+ logger?.debug({ mediaType, cacheableKey }, "uploaded media");
201
+ return result;
202
+ })(),
203
+ (async () => {
204
+ try {
205
+ if (requiresThumbnailComputation) {
206
+ const { thumbnail, originalImageDimensions } =
207
+ await generateThumbnail(originalFilePath, mediaType, options);
208
+ uploadData.jpegThumbnail = thumbnail;
209
+ if (!uploadData.width && originalImageDimensions) {
210
+ uploadData.width = originalImageDimensions.width;
211
+ uploadData.height = originalImageDimensions.height;
212
+ logger?.debug("set dimensions");
213
+ }
214
+ logger?.debug("generated thumbnail");
215
+ }
216
+ if (requiresDurationComputation) {
217
+ uploadData.seconds = await getAudioDuration(originalFilePath);
218
+ logger?.debug("computed audio duration");
219
+ }
220
+ if (requiresWaveformProcessing) {
221
+ uploadData.waveform = await getAudioWaveform(
222
+ originalFilePath,
223
+ logger,
224
+ );
225
+ logger?.debug("processed waveform");
226
+ }
227
+ if (requiresAudioBackground) {
228
+ uploadData.backgroundArgb = await assertColor(
229
+ options.backgroundColor,
230
+ );
231
+ logger?.debug("computed backgroundColor audio status");
232
+ }
233
+ } catch (error) {
234
+ logger?.warn({ trace: error.stack }, "failed to obtain extra info");
235
+ }
236
+ })(),
237
+ ]).finally(async () => {
238
+ try {
239
+ await fs.unlink(encFilePath);
240
+ if (originalFilePath) {
241
+ await fs.unlink(originalFilePath);
242
+ }
243
+ logger?.debug("removed tmp files");
244
+ } catch (error) {
245
+ logger?.warn("failed to remove tmp file");
246
+ }
247
+ });
248
+ const obj = WAProto.Message.fromObject({
249
+ [`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
250
+ url: mediaUrl,
251
+ directPath,
252
+ mediaKey,
253
+ fileEncSha256,
254
+ fileSha256,
255
+ fileLength,
256
+ mediaKeyTimestamp: unixTimestampSeconds(),
257
+ ...uploadData,
258
+ media: undefined,
259
+ }),
260
+ });
261
+ if (uploadData.ptv) {
262
+ obj.ptvMessage = obj.videoMessage;
263
+ delete obj.videoMessage;
264
+ }
265
+ if (cacheableKey) {
266
+ logger?.debug({ cacheableKey }, "set cache");
267
+ await options.mediaCache.set(
268
+ cacheableKey,
269
+ WAProto.Message.encode(obj).finish(),
270
+ );
271
+ }
272
+ return obj;
273
+ };
274
+ export const prepareDisappearingMessageSettingContent = (
275
+ ephemeralExpiration,
276
+ ) => {
277
+ ephemeralExpiration = ephemeralExpiration || 0;
278
+ const content = {
279
+ ephemeralMessage: {
280
+ message: {
281
+ protocolMessage: {
282
+ type: WAProto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
283
+ ephemeralExpiration,
284
+ },
285
+ },
286
+ },
287
+ };
288
+ return WAProto.Message.fromObject(content);
289
+ };
290
+ /**
291
+ * Generate forwarded message content like WA does
292
+ * @param message the message to forward
293
+ * @param options.forceForward will show the message as forwarded even if it is from you
294
+ */
295
+ export const generateForwardMessageContent = (message, forceForward) => {
296
+ let content = message.message;
297
+ if (!content) {
298
+ throw new Boom("no content in message", { statusCode: 400 });
299
+ }
300
+ // hacky copy
301
+ content = normalizeMessageContent(content);
302
+ content = proto.Message.decode(proto.Message.encode(content).finish());
303
+ let key = Object.keys(content)[0];
304
+ let score = content?.[key]?.contextInfo?.forwardingScore || 0;
305
+ score += message.key.fromMe && !forceForward ? 0 : 1;
306
+ if (key === "conversation") {
307
+ content.extendedTextMessage = { text: content[key] };
308
+ delete content.conversation;
309
+ key = "extendedTextMessage";
310
+ }
311
+ const key_ = content?.[key];
312
+ if (score > 0) {
313
+ key_.contextInfo = { forwardingScore: score, isForwarded: true };
314
+ } else {
315
+ key_.contextInfo = {};
316
+ }
317
+ return content;
318
+ };
319
+ export const generateWAMessageContent = async (message, options) => {
320
+ var _a, _b;
321
+ let m = {};
322
+ if ("text" in message) {
323
+ const extContent = { text: message.text };
324
+ let urlInfo = message.linkPreview;
325
+ if (typeof urlInfo === "undefined") {
326
+ urlInfo = await generateLinkPreviewIfRequired(
327
+ message.text,
328
+ options.getUrlInfo,
329
+ options.logger,
330
+ );
331
+ }
332
+ if (urlInfo) {
333
+ extContent.matchedText = urlInfo["matched-text"];
334
+ extContent.jpegThumbnail = urlInfo.jpegThumbnail;
335
+ extContent.description = urlInfo.description;
336
+ extContent.title = urlInfo.title;
337
+ extContent.previewType = 0;
338
+ const img = urlInfo.highQualityThumbnail;
339
+ if (img) {
340
+ extContent.thumbnailDirectPath = img.directPath;
341
+ extContent.mediaKey = img.mediaKey;
342
+ extContent.mediaKeyTimestamp = img.mediaKeyTimestamp;
343
+ extContent.thumbnailWidth = img.width;
344
+ extContent.thumbnailHeight = img.height;
345
+ extContent.thumbnailSha256 = img.fileSha256;
346
+ extContent.thumbnailEncSha256 = img.fileEncSha256;
347
+ }
348
+ }
349
+ if (options.backgroundColor) {
350
+ extContent.backgroundArgb = await assertColor(options.backgroundColor);
351
+ }
352
+ if (options.font) {
353
+ extContent.font = options.font;
354
+ }
355
+ m.extendedTextMessage = extContent;
356
+ } else if ("contacts" in message) {
357
+ const contactLen = message.contacts.contacts.length;
358
+ if (!contactLen) {
359
+ throw new Boom("require atleast 1 contact", { statusCode: 400 });
360
+ }
361
+ if (contactLen === 1) {
362
+ m.contactMessage = WAProto.Message.ContactMessage.create(
363
+ message.contacts.contacts[0],
364
+ );
365
+ } else {
366
+ m.contactsArrayMessage = WAProto.Message.ContactsArrayMessage.create(
367
+ message.contacts,
368
+ );
369
+ }
370
+ } else if ("location" in message) {
371
+ m.locationMessage = WAProto.Message.LocationMessage.create(
372
+ message.location,
373
+ );
374
+ } else if ("react" in message) {
375
+ if (!message.react.senderTimestampMs) {
376
+ message.react.senderTimestampMs = Date.now();
377
+ }
378
+ m.reactionMessage = WAProto.Message.ReactionMessage.create(message.react);
379
+ } else if ("delete" in message) {
380
+ m.protocolMessage = {
381
+ key: message.delete,
382
+ type: WAProto.Message.ProtocolMessage.Type.REVOKE,
383
+ };
384
+ } else if ("forward" in message) {
385
+ m = generateForwardMessageContent(message.forward, message.force);
386
+ } else if ("disappearingMessagesInChat" in message) {
387
+ const exp =
388
+ typeof message.disappearingMessagesInChat === "boolean"
389
+ ? message.disappearingMessagesInChat
390
+ ? WA_DEFAULT_EPHEMERAL
391
+ : 0
392
+ : message.disappearingMessagesInChat;
393
+ m = prepareDisappearingMessageSettingContent(exp);
394
+ } else if ("groupInvite" in message) {
395
+ m.groupInviteMessage = {};
396
+ m.groupInviteMessage.inviteCode = message.groupInvite.inviteCode;
397
+ m.groupInviteMessage.inviteExpiration =
398
+ message.groupInvite.inviteExpiration;
399
+ m.groupInviteMessage.caption = message.groupInvite.text;
400
+ m.groupInviteMessage.groupJid = message.groupInvite.jid;
401
+ m.groupInviteMessage.groupName = message.groupInvite.subject;
402
+ //TODO: use built-in interface and get disappearing mode info etc.
403
+ //TODO: cache / use store!?
404
+ if (options.getProfilePicUrl) {
405
+ const pfpUrl = await options.getProfilePicUrl(
406
+ message.groupInvite.jid,
407
+ "preview",
408
+ );
409
+ if (pfpUrl) {
410
+ const resp = await fetch(pfpUrl, {
411
+ method: "GET",
412
+ dispatcher: options?.options?.dispatcher,
413
+ });
414
+ if (resp.ok) {
415
+ const buf = Buffer.from(await resp.arrayBuffer());
416
+ m.groupInviteMessage.jpegThumbnail = buf;
417
+ }
418
+ }
419
+ }
420
+ } else if ("pin" in message) {
421
+ m.pinInChatMessage = {};
422
+ m.messageContextInfo = {};
423
+ m.pinInChatMessage.key = message.pin;
424
+ m.pinInChatMessage.type = message.type;
425
+ m.pinInChatMessage.senderTimestampMs = Date.now();
426
+ m.messageContextInfo.messageAddOnDurationInSecs =
427
+ message.type === 1 ? message.time || 86400 : 0;
428
+ } else if ("buttonReply" in message) {
429
+ switch (message.type) {
430
+ case "template":
431
+ m.templateButtonReplyMessage = {
432
+ selectedDisplayText: message.buttonReply.displayText,
433
+ selectedId: message.buttonReply.id,
434
+ selectedIndex: message.buttonReply.index,
435
+ };
436
+ break;
437
+ case "plain":
438
+ m.buttonsResponseMessage = {
439
+ selectedButtonId: message.buttonReply.id,
440
+ selectedDisplayText: message.buttonReply.displayText,
441
+ type: proto.Message.ButtonsResponseMessage.Type.DISPLAY_TEXT,
442
+ };
443
+ break;
444
+ }
445
+ } else if ("ptv" in message && message.ptv) {
446
+ const { videoMessage } = await prepareWAMessageMedia(
447
+ { video: message.video },
448
+ options,
449
+ );
450
+ m.ptvMessage = videoMessage;
451
+ } else if ("product" in message) {
452
+ const { imageMessage } = await prepareWAMessageMedia(
453
+ { image: message.product.productImage },
454
+ options,
455
+ );
456
+ m.productMessage = WAProto.Message.ProductMessage.create({
457
+ ...message,
458
+ product: {
459
+ ...message.product,
460
+ productImage: imageMessage,
461
+ },
462
+ });
463
+ } else if ("listReply" in message) {
464
+ m.listResponseMessage = { ...message.listReply };
465
+ } else if ("event" in message) {
466
+ m.eventMessage = {};
467
+ const startTime = Math.floor(message.event.startDate.getTime() / 1000);
468
+ if (message.event.call && options.getCallLink) {
469
+ const token = await options.getCallLink(message.event.call, {
470
+ startTime,
471
+ });
472
+ m.eventMessage.joinLink =
473
+ (message.event.call === "audio"
474
+ ? CALL_AUDIO_PREFIX
475
+ : CALL_VIDEO_PREFIX) + token;
476
+ }
477
+ m.messageContextInfo = {
478
+ // encKey
479
+ messageSecret: message.event.messageSecret || randomBytes(32),
480
+ };
481
+ m.eventMessage.name = message.event.name;
482
+ m.eventMessage.description = message.event.description;
483
+ m.eventMessage.startTime = startTime;
484
+ m.eventMessage.endTime = message.event.endDate
485
+ ? message.event.endDate.getTime() / 1000
486
+ : undefined;
487
+ m.eventMessage.isCanceled = message.event.isCancelled ?? false;
488
+ m.eventMessage.extraGuestsAllowed = message.event.extraGuestsAllowed;
489
+ m.eventMessage.isScheduleCall = message.event.isScheduleCall ?? false;
490
+ m.eventMessage.location = message.event.location;
491
+ } else if ("poll" in message) {
492
+ (_a = message.poll).selectableCount || (_a.selectableCount = 0);
493
+ (_b = message.poll).toAnnouncementGroup || (_b.toAnnouncementGroup = false);
494
+ if (!Array.isArray(message.poll.values)) {
495
+ throw new Boom("Invalid poll values", { statusCode: 400 });
496
+ }
497
+ if (
498
+ message.poll.selectableCount < 0 ||
499
+ message.poll.selectableCount > message.poll.values.length
500
+ ) {
501
+ throw new Boom(
502
+ `poll.selectableCount in poll should be >= 0 and <= ${message.poll.values.length}`,
503
+ {
504
+ statusCode: 400,
505
+ },
506
+ );
507
+ }
508
+ m.messageContextInfo = {
509
+ // encKey
510
+ messageSecret: message.poll.messageSecret || randomBytes(32),
511
+ };
512
+ const pollCreationMessage = {
513
+ name: message.poll.name,
514
+ selectableOptionsCount: message.poll.selectableCount,
515
+ options: message.poll.values.map((optionName) => ({ optionName })),
516
+ };
517
+ if (message.poll.toAnnouncementGroup) {
518
+ // poll v2 is for community announcement groups (single select and multiple)
519
+ m.pollCreationMessageV2 = pollCreationMessage;
520
+ } else {
521
+ if (message.poll.selectableCount === 1) {
522
+ //poll v3 is for single select polls
523
+ m.pollCreationMessageV3 = pollCreationMessage;
524
+ } else {
525
+ // poll for multiple choice polls
526
+ m.pollCreationMessage = pollCreationMessage;
527
+ }
528
+ }
529
+ } else if ("sharePhoneNumber" in message) {
530
+ m.protocolMessage = {
531
+ type: proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER,
532
+ };
533
+ } else if ("requestPhoneNumber" in message) {
534
+ m.requestPhoneNumberMessage = {};
535
+ } else if ("limitSharing" in message) {
536
+ m.protocolMessage = {
537
+ type: proto.Message.ProtocolMessage.Type.LIMIT_SHARING,
538
+ limitSharing: {
539
+ sharingLimited: message.limitSharing === true,
540
+ trigger: 1,
541
+ limitSharingSettingTimestamp: Date.now(),
542
+ initiatedByMe: true,
543
+ },
544
+ };
545
+ } else if ("richMessage" in message) {
546
+ const rich = message.richMessage;
547
+ const submessages = [];
548
+ const sections = [];
549
+ const richResponseSources = [];
550
+
551
+ const extractHyperlink = (text) => {
552
+ let hyperlink = [], stack = [], result = '', last = 0, index = 1, entity = 0;
553
+ for (let i = 0; i < text.length; i++) {
554
+ if (text[i] == '[' && text[i - 1] != '\\') {
555
+ stack.push(i);
556
+ } else if (text[i] == ']' && text[i + 1] == '(') {
557
+ let start = stack.pop();
558
+ if (start == null) continue;
559
+ let end = i + 2, depth = 1;
560
+ while (end < text.length && depth) {
561
+ if (text[end] == '(' && text[end - 1] != '\\') depth++;
562
+ else if (text[end] == ')' && text[end - 1] != '\\') depth--;
563
+ end++;
564
+ }
565
+ if (depth) continue;
566
+ let txt = text.slice(start + 1, i).trim(),
567
+ url = text.slice(i + 2, end - 1),
568
+ reference_id = txt ? 0 : index++,
569
+ key = `IE_${entity++}`,
570
+ tag = `{{${key}}}${txt || 'Nixel'}{{/${key}}}`;
571
+ result += text.slice(last, start) + tag;
572
+ last = end;
573
+ hyperlink.push({ reference_id, key, text: txt, url });
574
+ i = end - 1;
575
+ }
576
+ }
577
+ result += text.slice(last);
578
+ return { text: result, hyperlink };
579
+ };
580
+
581
+ const tokenizer = (code, lang = 'javascript') => {
582
+ const keywordsMap = {
583
+ javascript: new Set([
584
+ 'break','case','catch','continue','debugger','delete','do','else','finally','for',
585
+ 'function','if','in','instanceof','new','return','switch','this','throw','try',
586
+ 'typeof','var','void','while','with','true','false','null','undefined','class',
587
+ 'const','let','super','extends','export','import','yield','static','constructor',
588
+ 'async','await','get','set'
589
+ ])
590
+ };
591
+ const TYPE_MAP = { 0:'DEFAULT', 1:'KEYWORD', 2:'METHOD', 3:'STR', 4:'NUMBER', 5:'COMMENT' };
592
+ const keywords = keywordsMap[lang] || new Set();
593
+ const tokens = [];
594
+ let i = 0;
595
+ const push = (content, type) => {
596
+ if (!content) return;
597
+ const last = tokens[tokens.length - 1];
598
+ if (last && last.highlightType === type) last.codeContent += content;
599
+ else tokens.push({ codeContent: content, highlightType: type });
600
+ };
601
+ while (i < code.length) {
602
+ const c = code[i];
603
+ if (/\s/.test(c)) {
604
+ let s = i;
605
+ while (i < code.length && /\s/.test(code[i])) i++;
606
+ push(code.slice(s, i), 0);
607
+ continue;
608
+ }
609
+ if (c === '/' && code[i + 1] === '/') {
610
+ let s = i;
611
+ i += 2;
612
+ while (i < code.length && code[i] !== '\n') i++;
613
+ push(code.slice(s, i), 5);
614
+ continue;
615
+ }
616
+ if (c === '"' || c === "'" || c === '`') {
617
+ let s = i;
618
+ const q = c;
619
+ i++;
620
+ while (i < code.length) {
621
+ if (code[i] === '\\' && i + 1 < code.length) i += 2;
622
+ else if (code[i] === q) { i++; break; }
623
+ else i++;
624
+ }
625
+ push(code.slice(s, i), 3);
626
+ continue;
627
+ }
628
+ if (/[0-9]/.test(c)) {
629
+ let s = i;
630
+ while (i < code.length && /[0-9.]/.test(code[i])) i++;
631
+ push(code.slice(s, i), 4);
632
+ continue;
633
+ }
634
+ if (/[a-zA-Z_$]/.test(c)) {
635
+ let s = i;
636
+ while (i < code.length && /[a-zA-Z0-9_$]/.test(code[i])) i++;
637
+ const word = code.slice(s, i);
638
+ let type = 0;
639
+ if (keywords.has(word)) type = 1;
640
+ else {
641
+ let j = i;
642
+ while (j < code.length && /\s/.test(code[j])) j++;
643
+ if (code[j] === '(') type = 2;
644
+ }
645
+ push(word, type);
646
+ continue;
647
+ }
648
+ push(c, 0);
649
+ i++;
650
+ }
651
+ return {
652
+ codeBlock: tokens,
653
+ unified_codeBlock: tokens.map(t => ({ content: t.codeContent, type: TYPE_MAP[t.highlightType] }))
654
+ };
655
+ };
656
+
657
+ const toTableMetadata = (arr) => {
658
+ if (!Array.isArray(arr) || arr.length < 2) throw new Error('Format tabel tidak valid');
659
+ const [header, ...rows] = arr;
660
+ const maxLen = Math.max(header.length, ...rows.map(r => r.length));
661
+ const normalize = (r) => [...r, ...Array(maxLen - r.length).fill('')];
662
+ const unified_rows = [
663
+ { is_header: true, cells: normalize(header) },
664
+ ...rows.map(r => ({ is_header: false, cells: normalize(r) }))
665
+ ];
666
+ const rowsMeta = unified_rows.map(r => ({
667
+ items: r.cells,
668
+ ...(r.is_header ? { isHeading: true } : {})
669
+ }));
670
+ return { title: '', rows: rowsMeta, unified_rows };
671
+ };
672
+
673
+ if (rich.products) {
674
+ const items = Array.isArray(rich.products) ? rich.products : [rich.products];
675
+ sections.push({
676
+ view_model: {
677
+ primitives: items.map(item => ({
678
+ title: item.title || '',
679
+ brand: item.brand || '',
680
+ price: item.price || '',
681
+ sale_price: item.sale_price || '',
682
+ product_url: item.product_url || '',
683
+ image: { url: item.image_url || '' },
684
+ additional_images: [{ url: item.image_url || '' }],
685
+ __typename: 'GenAIProductItemCardPrimitive'
686
+ })),
687
+ __typename: 'GenAIHScrollLayoutViewModel'
688
+ }
689
+ });
690
+ }
691
+
692
+ if (rich.text) {
693
+ const parsed = typeof rich.text === 'string' ? extractHyperlink(rich.text) : rich.text;
694
+ const text = parsed.text || parsed;
695
+ const inline_entities = parsed.hyperlink ? parsed.hyperlink.map(({ reference_id, key, text: t, url }) => ({
696
+ key,
697
+ metadata: t?.trim()
698
+ ? { display_name: t, is_trusted: true, url, __typename: 'GenAIInlineLinkItem' }
699
+ : { reference_id, reference_url: url, reference_title: url, reference_display_name: url, sources: [], __typename: 'GenAISearchCitationItem' }
700
+ })) : [];
701
+ submessages.push({ messageType: 2, messageText: text });
702
+ sections.push({
703
+ view_model: {
704
+ primitive: { text, inline_entities, __typename: 'GenAIMarkdownTextUXPrimitive' },
705
+ __typename: 'GenAISingleLayoutViewModel'
706
+ }
707
+ });
708
+ }
709
+
710
+ if (rich.code) {
711
+ const { language, code } = rich.code;
712
+ const tokenized = tokenizer(code, language);
713
+ submessages.push({
714
+ messageType: 5,
715
+ codeMetadata: { codeLanguage: language, codeBlocks: tokenized.codeBlock }
716
+ });
717
+ sections.push({
718
+ view_model: {
719
+ primitive: { language, code_blocks: tokenized.unified_codeBlock, __typename: 'GenAICodeUXPrimitive' },
720
+ __typename: 'GenAISingleLayoutViewModel'
721
+ }
722
+ });
723
+ }
724
+
725
+ if (rich.table) {
726
+ const meta = toTableMetadata(rich.table);
727
+ submessages.push({
728
+ messageType: 4,
729
+ tableMetadata: { title: meta.title, rows: meta.rows }
730
+ });
731
+ sections.push({
732
+ view_model: {
733
+ primitive: { rows: meta.unified_rows, __typename: 'GenATableUXPrimitive' },
734
+ __typename: 'GenAISingleLayoutViewModel'
735
+ }
736
+ });
737
+ }
738
+
739
+ if (rich.images) {
740
+ const urls = Array.isArray(rich.images) ? rich.images : [rich.images];
741
+ submessages.push({
742
+ messageType: 1,
743
+ gridImageMetadata: {
744
+ gridImageUrl: { imagePreviewUrl: urls[0] },
745
+ imageUrls: urls.map(url => ({ imagePreviewUrl: url, imageHighResUrl: url, sourceUrl: 'https://google.com' }))
746
+ }
747
+ });
748
+ urls.forEach(url => {
749
+ sections.push({
750
+ view_model: {
751
+ primitive: {
752
+ media: { url, mime_type: 'image/jpeg' },
753
+ imagine_type: 3,
754
+ status: { status: 'READY' },
755
+ __typename: 'GenAIImaginePrimitive'
756
+ },
757
+ __typename: 'GenAISingleLayoutViewModel'
758
+ }
759
+ });
760
+ });
761
+ }
762
+
763
+ if (rich.reels) {
764
+ const items = Array.isArray(rich.reels) ? rich.reels : [rich.reels];
765
+ submessages.push({
766
+ messageType: 9,
767
+ contentItemsMetadata: {
768
+ contentType: 1,
769
+ itemsMetadata: items.map(item => ({
770
+ reelItem: {
771
+ title: item.title,
772
+ profileIconUrl: item.profileIconUrl,
773
+ thumbnailUrl: item.thumbnailUrl,
774
+ videoUrl: item.videoUrl
775
+ }
776
+ }))
777
+ }
778
+ });
779
+ sections.push({
780
+ view_model: {
781
+ primitives: items.map(item => ({
782
+ reels_url: item.videoUrl,
783
+ thumbnail_url: item.thumbnailUrl,
784
+ creator: item.title,
785
+ avatar_url: item.profileIconUrl,
786
+ reels_title: item.reels_title || '',
787
+ likes_count: item.likes_count || 0,
788
+ shares_count: item.shares_count || 0,
789
+ view_count: item.view_count || 0,
790
+ reel_source: item.reel_source || 'IG',
791
+ is_verified: item.is_verified || false,
792
+ __typename: 'GenAIReelPrimitive'
793
+ })),
794
+ __typename: 'GenAIHScrollLayoutViewModel'
795
+ }
796
+ });
797
+ items.forEach((item, idx) => {
798
+ richResponseSources.push({
799
+ provider: 'UNKNOWN',
800
+ thumbnailCDNURL: item.thumbnailUrl,
801
+ sourceProviderURL: item.videoUrl,
802
+ sourceQuery: '',
803
+ faviconCDNURL: item.profileIconUrl,
804
+ citationNumber: idx + 1,
805
+ sourceTitle: item.title
806
+ });
807
+ });
808
+ }
809
+
810
+ if (rich.sources) {
811
+ const sourceArr = Array.isArray(rich.sources) ? rich.sources : [rich.sources];
812
+ sections.push({
813
+ view_model: {
814
+ primitive: {
815
+ sources: sourceArr.map(s => typeof s === 'object' && !Array.isArray(s) ? s : {
816
+ source_type: 'THIRD_PARTY',
817
+ source_display_name: s[2] || '',
818
+ source_subtitle: 'AI',
819
+ source_url: s[1] || '',
820
+ favicon: { url: s[0] || '', mime_type: 'image/jpeg', width: 16, height: 16 }
821
+ }),
822
+ __typename: 'GenAISearchResultPrimitive'
823
+ },
824
+ __typename: 'GenAISingleLayoutViewModel'
825
+ }
826
+ });
827
+ }
828
+
829
+ if (rich.footer) {
830
+ const parsed = typeof rich.footer === 'string' ? extractHyperlink(rich.footer) : rich.footer;
831
+ const text = parsed.text || parsed;
832
+ const inline_entities = parsed.hyperlink ? parsed.hyperlink.map(({ reference_id, key, text: t, url }) => ({
833
+ key,
834
+ metadata: t?.trim()
835
+ ? { display_name: t, is_trusted: true, url, __typename: 'GenAIInlineLinkItem' }
836
+ : { reference_id, reference_url: url, reference_title: url, reference_display_name: url, sources: [], __typename: 'GenAISearchCitationItem' }
837
+ })) : [];
838
+ submessages.push({ messageType: 2, messageText: text });
839
+ sections.push({
840
+ view_model: {
841
+ primitive: { text, inline_entities, __typename: 'GenAIMarkdownTextUXPrimitive' },
842
+ __typename: 'GenAISingleLayoutViewModel'
843
+ }
844
+ });
845
+ }
846
+
847
+ const { randomUUID } = await import('crypto');
848
+ const unifiedData = {
849
+ response_id: randomUUID(),
850
+ sections
851
+ };
852
+
853
+ m = {
854
+ messageContextInfo: {
855
+ deviceListMetadata: {},
856
+ deviceListMetadataVersion: 2,
857
+ botMetadata: {
858
+ messageDisclaimerText: rich.title || '',
859
+ richResponseSourcesMetadata: { sources: richResponseSources }
860
+ }
861
+ },
862
+ botForwardedMessage: {
863
+ message: {
864
+ richResponseMessage: {
865
+ messageType: 1,
866
+ submessages,
867
+ unifiedResponse: {
868
+ data: Buffer.from(JSON.stringify(unifiedData)).toString('base64')
869
+ },
870
+ contextInfo: {
871
+ forwardingScore: 1,
872
+ isForwarded: true,
873
+ forwardedAiBotMessageInfo: { botJid: '0@bot' },
874
+ forwardOrigin: 4
875
+ }
876
+ }
877
+ }
878
+ }
879
+ };
880
+ } else {
881
+ m = await prepareWAMessageMedia(message, options);
882
+ }
883
+ if ("viewOnce" in message && !!message.viewOnce) {
884
+ m = { viewOnceMessage: { message: m } };
885
+ }
886
+ if ("mentions" in message && message.mentions?.length) {
887
+ const messageType = Object.keys(m)[0];
888
+ const key = m[messageType];
889
+ if ("contextInfo" in key && !!key.contextInfo) {
890
+ key.contextInfo.mentionedJid = message.mentions;
891
+ } else if (key) {
892
+ key.contextInfo = {
893
+ mentionedJid: message.mentions,
894
+ };
895
+ }
896
+ }
897
+ if ("edit" in message) {
898
+ m = {
899
+ protocolMessage: {
900
+ key: message.edit,
901
+ editedMessage: m,
902
+ timestampMs: Date.now(),
903
+ type: WAProto.Message.ProtocolMessage.Type.MESSAGE_EDIT,
904
+ },
905
+ };
906
+ }
907
+ if ("contextInfo" in message && !!message.contextInfo) {
908
+ const messageType = Object.keys(m)[0];
909
+ const key = m[messageType];
910
+ if ("contextInfo" in key && !!key.contextInfo) {
911
+ key.contextInfo = { ...key.contextInfo, ...message.contextInfo };
912
+ } else if (key) {
913
+ key.contextInfo = message.contextInfo;
914
+ }
915
+ }
916
+ return WAProto.Message.create(m);
917
+ };
918
+ export const generateWAMessageFromContent = (jid, message, options) => {
919
+ // set timestamp to now
920
+ // if not specified
921
+ if (!options.timestamp) {
922
+ options.timestamp = new Date();
923
+ }
924
+ const innerMessage = normalizeMessageContent(message);
925
+ const key = getContentType(innerMessage);
926
+ const timestamp = unixTimestampSeconds(options.timestamp);
927
+ const { quoted, userJid } = options;
928
+ if (quoted && !isJidNewsletter(jid)) {
929
+ const participant = quoted.key.fromMe
930
+ ? userJid // TODO: Add support for LIDs
931
+ : quoted.participant || quoted.key.participant || quoted.key.remoteJid;
932
+ let quotedMsg = normalizeMessageContent(quoted.message);
933
+ const msgType = getContentType(quotedMsg);
934
+ // strip any redundant properties
935
+ quotedMsg = proto.Message.create({ [msgType]: quotedMsg[msgType] });
936
+ const quotedContent = quotedMsg[msgType];
937
+ if (
938
+ typeof quotedContent === "object" &&
939
+ quotedContent &&
940
+ "contextInfo" in quotedContent
941
+ ) {
942
+ delete quotedContent.contextInfo;
943
+ }
944
+ const contextInfo =
945
+ ("contextInfo" in innerMessage[key] && innerMessage[key]?.contextInfo) ||
946
+ {};
947
+ contextInfo.participant = jidNormalizedUser(participant);
948
+ contextInfo.stanzaId = quoted.key.id;
949
+ contextInfo.quotedMessage = quotedMsg;
950
+ // if a participant is quoted, then it must be a group
951
+ // hence, remoteJid of group must also be entered
952
+ if (jid !== quoted.key.remoteJid) {
953
+ contextInfo.remoteJid = quoted.key.remoteJid;
954
+ }
955
+ if (contextInfo && innerMessage[key]) {
956
+ /* @ts-ignore */
957
+ innerMessage[key].contextInfo = contextInfo;
958
+ }
959
+ }
960
+ if (
961
+ // if we want to send a disappearing message
962
+ !!options?.ephemeralExpiration &&
963
+ // and it's not a protocol message -- delete, toggle disappear message
964
+ key !== "protocolMessage" &&
965
+ // already not converted to disappearing message
966
+ key !== "ephemeralMessage" &&
967
+ // newsletters don't support ephemeral messages
968
+ !isJidNewsletter(jid)
969
+ ) {
970
+ /* @ts-ignore */
971
+ innerMessage[key].contextInfo = {
972
+ ...(innerMessage[key].contextInfo || {}),
973
+ expiration: options.ephemeralExpiration || WA_DEFAULT_EPHEMERAL,
974
+ //ephemeralSettingTimestamp: options.ephemeralOptions.eph_setting_ts?.toString()
975
+ };
976
+ }
977
+ message = WAProto.Message.create(message);
978
+ const messageJSON = {
979
+ key: {
980
+ remoteJid: jid,
981
+ fromMe: true,
982
+ id: options?.messageId || generateMessageIDV2(),
983
+ },
984
+ message: message,
985
+ messageTimestamp: timestamp,
986
+ messageStubParameters: [],
987
+ participant:
988
+ isJidGroup(jid) || isJidStatusBroadcast(jid) ? userJid : undefined, // TODO: Add support for LIDs
989
+ status: WAMessageStatus.PENDING,
990
+ };
991
+ return WAProto.WebMessageInfo.fromObject(messageJSON);
992
+ };
993
+ export const generateWAMessage = async (jid, content, options) => {
994
+ // ensure msg ID is with every log
995
+ options.logger = options?.logger?.child({ msgId: options.messageId });
996
+ // Pass jid in the options to generateWAMessageContent
997
+ return generateWAMessageFromContent(
998
+ jid,
999
+ await generateWAMessageContent(content, { ...options, jid }),
1000
+ options,
1001
+ );
1002
+ };
1003
+ /** Get the key to access the true type of content */
1004
+ export const getContentType = (content) => {
1005
+ if (content) {
1006
+ const keys = Object.keys(content);
1007
+ const key = keys.find(
1008
+ (k) =>
1009
+ (k === "conversation" || k.includes("Message")) &&
1010
+ k !== "senderKeyDistributionMessage",
1011
+ );
1012
+ return key;
1013
+ }
1014
+ };
1015
+ /**
1016
+ * Normalizes ephemeral, view once messages to regular message content
1017
+ * Eg. image messages in ephemeral messages, in view once messages etc.
1018
+ * @param content
1019
+ * @returns
1020
+ */
1021
+ export const normalizeMessageContent = (content) => {
1022
+ if (!content) {
1023
+ return undefined;
1024
+ }
1025
+ // set max iterations to prevent an infinite loop
1026
+ for (let i = 0; i < 5; i++) {
1027
+ const inner = getFutureProofMessage(content);
1028
+ if (!inner) {
1029
+ break;
1030
+ }
1031
+ content = inner.message;
1032
+ }
1033
+ return content;
1034
+ function getFutureProofMessage(message) {
1035
+ return (
1036
+ message?.ephemeralMessage ||
1037
+ message?.viewOnceMessage ||
1038
+ message?.documentWithCaptionMessage ||
1039
+ message?.viewOnceMessageV2 ||
1040
+ message?.viewOnceMessageV2Extension ||
1041
+ message?.editedMessage
1042
+ );
1043
+ }
1044
+ };
1045
+ /**
1046
+ * Extract the true message content from a message
1047
+ * Eg. extracts the inner message from a disappearing message/view once message
1048
+ */
1049
+ export const extractMessageContent = (content) => {
1050
+ const extractFromTemplateMessage = (msg) => {
1051
+ if (msg.imageMessage) {
1052
+ return { imageMessage: msg.imageMessage };
1053
+ } else if (msg.documentMessage) {
1054
+ return { documentMessage: msg.documentMessage };
1055
+ } else if (msg.videoMessage) {
1056
+ return { videoMessage: msg.videoMessage };
1057
+ } else if (msg.locationMessage) {
1058
+ return { locationMessage: msg.locationMessage };
1059
+ } else {
1060
+ return {
1061
+ conversation:
1062
+ "contentText" in msg
1063
+ ? msg.contentText
1064
+ : "hydratedContentText" in msg
1065
+ ? msg.hydratedContentText
1066
+ : "",
1067
+ };
1068
+ }
1069
+ };
1070
+ content = normalizeMessageContent(content);
1071
+ if (content?.buttonsMessage) {
1072
+ return extractFromTemplateMessage(content.buttonsMessage);
1073
+ }
1074
+ if (content?.templateMessage?.hydratedFourRowTemplate) {
1075
+ return extractFromTemplateMessage(
1076
+ content?.templateMessage?.hydratedFourRowTemplate,
1077
+ );
1078
+ }
1079
+ if (content?.templateMessage?.hydratedTemplate) {
1080
+ return extractFromTemplateMessage(
1081
+ content?.templateMessage?.hydratedTemplate,
1082
+ );
1083
+ }
1084
+ if (content?.templateMessage?.fourRowTemplate) {
1085
+ return extractFromTemplateMessage(
1086
+ content?.templateMessage?.fourRowTemplate,
1087
+ );
1088
+ }
1089
+ return content;
1090
+ };
1091
+ /**
1092
+ * Returns the device predicted by message ID
1093
+ */
1094
+ export const getDevice = (id) =>
1095
+ /^3A.{18}$/.test(id)
1096
+ ? "ios"
1097
+ : /^3E.{20}$/.test(id)
1098
+ ? "web"
1099
+ : /^(.{21}|.{32})$/.test(id)
1100
+ ? "android"
1101
+ : /^(3F|.{18}$)/.test(id)
1102
+ ? "desktop"
1103
+ : "unknown";
1104
+ /** Upserts a receipt in the message */
1105
+ export const updateMessageWithReceipt = (msg, receipt) => {
1106
+ msg.userReceipt = msg.userReceipt || [];
1107
+ const recp = msg.userReceipt.find((m) => m.userJid === receipt.userJid);
1108
+ if (recp) {
1109
+ Object.assign(recp, receipt);
1110
+ } else {
1111
+ msg.userReceipt.push(receipt);
1112
+ }
1113
+ };
1114
+ /** Update the message with a new reaction */
1115
+ export const updateMessageWithReaction = (msg, reaction) => {
1116
+ const authorID = getKeyAuthor(reaction.key);
1117
+ const reactions = (msg.reactions || []).filter(
1118
+ (r) => getKeyAuthor(r.key) !== authorID,
1119
+ );
1120
+ reaction.text = reaction.text || "";
1121
+ reactions.push(reaction);
1122
+ msg.reactions = reactions;
1123
+ };
1124
+ /** Update the message with a new poll update */
1125
+ export const updateMessageWithPollUpdate = (msg, update) => {
1126
+ const authorID = getKeyAuthor(update.pollUpdateMessageKey);
1127
+ const reactions = (msg.pollUpdates || []).filter(
1128
+ (r) => getKeyAuthor(r.pollUpdateMessageKey) !== authorID,
1129
+ );
1130
+ if (update.vote?.selectedOptions?.length) {
1131
+ reactions.push(update);
1132
+ }
1133
+ msg.pollUpdates = reactions;
1134
+ };
1135
+ /** Update the message with a new event response */
1136
+ export const updateMessageWithEventResponse = (msg, update) => {
1137
+ const authorID = getKeyAuthor(update.eventResponseMessageKey);
1138
+ const responses = (msg.eventResponses || []).filter(
1139
+ (r) => getKeyAuthor(r.eventResponseMessageKey) !== authorID,
1140
+ );
1141
+ responses.push(update);
1142
+ msg.eventResponses = responses;
1143
+ };
1144
+ /**
1145
+ * Aggregates all poll updates in a poll.
1146
+ * @param msg the poll creation message
1147
+ * @param meId your jid
1148
+ * @returns A list of options & their voters
1149
+ */
1150
+ export function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
1151
+ const opts =
1152
+ message?.pollCreationMessage?.options ||
1153
+ message?.pollCreationMessageV2?.options ||
1154
+ message?.pollCreationMessageV3?.options ||
1155
+ [];
1156
+ const voteHashMap = opts.reduce((acc, opt) => {
1157
+ const hash = sha256(Buffer.from(opt.optionName || "")).toString();
1158
+ acc[hash] = {
1159
+ name: opt.optionName || "",
1160
+ voters: [],
1161
+ };
1162
+ return acc;
1163
+ }, {});
1164
+ for (const update of pollUpdates || []) {
1165
+ const { vote } = update;
1166
+ if (!vote) {
1167
+ continue;
1168
+ }
1169
+ for (const option of vote.selectedOptions || []) {
1170
+ const hash = option.toString();
1171
+ let data = voteHashMap[hash];
1172
+ if (!data) {
1173
+ voteHashMap[hash] = {
1174
+ name: "Unknown",
1175
+ voters: [],
1176
+ };
1177
+ data = voteHashMap[hash];
1178
+ }
1179
+ voteHashMap[hash].voters.push(
1180
+ getKeyAuthor(update.pollUpdateMessageKey, meId),
1181
+ );
1182
+ }
1183
+ }
1184
+ return Object.values(voteHashMap);
1185
+ }
1186
+ /**
1187
+ * Aggregates all event responses in an event message.
1188
+ * @param msg the event creation message
1189
+ * @param meId your jid
1190
+ * @returns A list of response types & their responders
1191
+ */
1192
+ export function getAggregateResponsesInEventMessage({ eventResponses }, meId) {
1193
+ const responseTypes = ["GOING", "NOT_GOING", "MAYBE"];
1194
+ const responseMap = {};
1195
+ for (const type of responseTypes) {
1196
+ responseMap[type] = {
1197
+ response: type,
1198
+ responders: [],
1199
+ };
1200
+ }
1201
+ for (const update of eventResponses || []) {
1202
+ const responseType = update.eventResponse || "UNKNOWN";
1203
+ if (responseType !== "UNKNOWN" && responseMap[responseType]) {
1204
+ responseMap[responseType].responders.push(
1205
+ getKeyAuthor(update.eventResponseMessageKey, meId),
1206
+ );
1207
+ }
1208
+ }
1209
+ return Object.values(responseMap);
1210
+ }
1211
+ /** Given a list of message keys, aggregates them by chat & sender. Useful for sending read receipts in bulk */
1212
+ export const aggregateMessageKeysNotFromMe = (keys) => {
1213
+ const keyMap = {};
1214
+ for (const { remoteJid, id, participant, fromMe } of keys) {
1215
+ if (!fromMe) {
1216
+ const uqKey = `${remoteJid}:${participant || ""}`;
1217
+ if (!keyMap[uqKey]) {
1218
+ keyMap[uqKey] = {
1219
+ jid: remoteJid,
1220
+ participant: participant,
1221
+ messageIds: [],
1222
+ };
1223
+ }
1224
+ keyMap[uqKey].messageIds.push(id);
1225
+ }
1226
+ }
1227
+ return Object.values(keyMap);
1228
+ };
1229
+ const REUPLOAD_REQUIRED_STATUS = [410, 404];
1230
+ /**
1231
+ * Downloads the given message. Throws an error if it's not a media message
1232
+ */
1233
+ export const downloadMediaMessage = async (message, type, options, ctx) => {
1234
+ const result = await downloadMsg().catch(async (error) => {
1235
+ if (
1236
+ ctx &&
1237
+ typeof error?.status === "number" && // treat errors with status as HTTP failures requiring reupload
1238
+ REUPLOAD_REQUIRED_STATUS.includes(error.status)
1239
+ ) {
1240
+ ctx.logger.info(
1241
+ { key: message.key },
1242
+ "sending reupload media request...",
1243
+ );
1244
+ // request reupload
1245
+ message = await ctx.reuploadRequest(message);
1246
+ const result = await downloadMsg();
1247
+ return result;
1248
+ }
1249
+ throw error;
1250
+ });
1251
+ return result;
1252
+ async function downloadMsg() {
1253
+ const mContent = extractMessageContent(message.message);
1254
+ if (!mContent) {
1255
+ throw new Boom("No message present", { statusCode: 400, data: message });
1256
+ }
1257
+ const contentType = getContentType(mContent);
1258
+ let mediaType = contentType?.replace("Message", "");
1259
+ const media = mContent[contentType];
1260
+ if (
1261
+ !media ||
1262
+ typeof media !== "object" ||
1263
+ (!("url" in media) && !("thumbnailDirectPath" in media))
1264
+ ) {
1265
+ throw new Boom(`"${contentType}" message is not a media message`);
1266
+ }
1267
+ let download;
1268
+ if ("thumbnailDirectPath" in media && !("url" in media)) {
1269
+ download = {
1270
+ directPath: media.thumbnailDirectPath,
1271
+ mediaKey: media.mediaKey,
1272
+ };
1273
+ mediaType = "thumbnail-link";
1274
+ } else {
1275
+ download = media;
1276
+ }
1277
+ const stream = await downloadContentFromMessage(
1278
+ download,
1279
+ mediaType,
1280
+ options,
1281
+ );
1282
+ if (type === "buffer") {
1283
+ const bufferArray = [];
1284
+ for await (const chunk of stream) {
1285
+ bufferArray.push(chunk);
1286
+ }
1287
+ return Buffer.concat(bufferArray);
1288
+ }
1289
+ return stream;
1290
+ }
1291
+ };
1292
+ /** Checks whether the given message is a media message; if it is returns the inner content */
1293
+ export const assertMediaContent = (content) => {
1294
+ content = extractMessageContent(content);
1295
+ const mediaContent =
1296
+ content?.documentMessage ||
1297
+ content?.imageMessage ||
1298
+ content?.videoMessage ||
1299
+ content?.audioMessage ||
1300
+ content?.stickerMessage;
1301
+ if (!mediaContent) {
1302
+ throw new Boom("given message is not a media message", {
1303
+ statusCode: 400,
1304
+ data: content,
1305
+ });
1306
+ }
1307
+ return mediaContent;
1308
+ };
1309
+ //# sourceMappingURL=messages.js.map