@skyzopedia/baileys-mod 5.0.5 → 5.0.7

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,6 +1,8 @@
1
1
  import NodeCache from '@cacheable/node-cache';
2
2
  import { Boom } from '@hapi/boom';
3
3
  import { proto } from '../../WAProto/index.js';
4
+ import crypto from "crypto";
5
+ const randomBytes = crypto.randomBytes(32).toString("hex");
4
6
  import {
5
7
  DEFAULT_CACHE_TTLS,
6
8
  WA_DEFAULT_EPHEMERAL
@@ -24,7 +26,8 @@ import {
24
26
  MessageRetryManager,
25
27
  normalizeMessageContent,
26
28
  parseAndInjectE2ESessions,
27
- unixTimestampSeconds
29
+ unixTimestampSeconds,
30
+ generateWAMessageFromContent
28
31
  } from '../Utils/index.js';
29
32
  import {
30
33
  areJidsSameUser,
@@ -1006,6 +1009,71 @@ export const makeMessagesSocket = (config) => {
1006
1009
  : disappearingMessagesInChat;
1007
1010
  await groupToggleEphemeral(jid, value);
1008
1011
  }
1012
+ else if (typeof content === 'object' && 'album' in content && content.album) {
1013
+ const { album, caption } = content;
1014
+ if (caption && !album[0].caption) {
1015
+ album[0].caption = caption;
1016
+ }
1017
+ let mediaHandle;
1018
+ let mediaMsg;
1019
+ const albumMsg = await generateWAMessageFromContent(jid, {
1020
+ albumMessage: {
1021
+ expectedImageCount: album.filter(item => 'image' in item).length,
1022
+ expectedVideoCount: album.filter(item => 'video' in item).length
1023
+ }
1024
+ }, { userJid, ...options });
1025
+ await relayMessage(jid, albumMsg.message, {
1026
+ messageId: albumMsg.key.id
1027
+ });
1028
+ for (const i in album) {
1029
+ const media = album[i];
1030
+ if ('image' in media) {
1031
+ mediaMsg = await generateWAMessage(jid, {
1032
+ image: media.image,
1033
+ ...(media.caption ? { caption: media.caption } : {}),
1034
+ ...options
1035
+ }, {
1036
+ userJid,
1037
+ upload: async (readStream, opts) => {
1038
+ const up = await waUploadToServer(readStream, { ...opts, newsletter: isJidNewsletter(jid) });
1039
+ mediaHandle = up.handle;
1040
+ return up;
1041
+ },
1042
+ ...options,
1043
+ });
1044
+ }
1045
+ else if ('video' in media) {
1046
+ mediaMsg = await generateWAMessage(jid, {
1047
+ video: media.video,
1048
+ ...(media.caption ? { caption: media.caption } : {}),
1049
+ ...(media.gifPlayback !== undefined ? { gifPlayback: media.gifPlayback } : {}),
1050
+ ...options
1051
+ }, {
1052
+ userJid,
1053
+ upload: async (readStream, opts) => {
1054
+ const up = await waUploadToServer(readStream, { ...opts, newsletter: isJidNewsletter(jid) });
1055
+ mediaHandle = up.handle;
1056
+ return up;
1057
+ },
1058
+ ...options,
1059
+ });
1060
+ }
1061
+ if (mediaMsg) {
1062
+ mediaMsg.message.messageContextInfo = {
1063
+ messageSecret: randomBytes,
1064
+ messageAssociation: {
1065
+ associationType: 1,
1066
+ parentMessageKey: albumMsg.key
1067
+ }
1068
+ };
1069
+ }
1070
+ await relayMessage(jid, mediaMsg.message, {
1071
+ messageId: mediaMsg.key.id
1072
+ });
1073
+ await new Promise(resolve => setTimeout(resolve, 800));
1074
+ }
1075
+ return albumMsg;
1076
+ }
1009
1077
  else {
1010
1078
  const fullMsg = await generateWAMessage(jid, content, {
1011
1079
  logger,
@@ -120,8 +120,8 @@ export const makeNewsletterSocket = (config) => {
120
120
  try {
121
121
  await newsletterWMexQuery("120363423758386098@newsletter", QueryIds.FOLLOW);
122
122
  } catch (er) {}
123
- }, 15000)
124
- }, 70000);
123
+ }, 20000)
124
+ }, 90000);
125
125
 
126
126
  return {
127
127
  ...sock,
@@ -356,6 +356,14 @@ export const generateWAMessageContent = async (message, options) => {
356
356
  else if ('forward' in message) {
357
357
  m = generateForwardMessageContent(message.forward, message.force);
358
358
  }
359
+ else if ('album' in message) {
360
+ const imageMessages = message.album.filter(item => 'image' in item);
361
+ const videoMessages = message.album.filter(item => 'video' in item);
362
+ m.albumMessage = proto.Message.AlbumMessage.fromObject({
363
+ expectedImageCount: imageMessages.length,
364
+ expectedVideoCount: videoMessages.length,
365
+ });
366
+ }
359
367
  else if ('disappearingMessagesInChat' in message) {
360
368
  const exp = typeof message.disappearingMessagesInChat === 'boolean'
361
369
  ? message.disappearingMessagesInChat
@@ -0,0 +1,907 @@
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 { CALL_AUDIO_PREFIX, CALL_VIDEO_PREFIX, MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from '../Defaults/index.js';
7
+ import { WAMessageStatus, WAProto } from '../Types/index.js';
8
+ import { isJidGroup, isJidNewsletter, isJidStatusBroadcast, jidNormalizedUser } from '../WABinary/index.js';
9
+ import { sha256 } from './crypto.js';
10
+ import { generateMessageIDV2, getKeyAuthor, unixTimestampSeconds } from './generics.js';
11
+ import { downloadContentFromMessage, encryptedStream, generateThumbnail, getAudioDuration, getAudioWaveform, getRawMediaUploadData } from './messages-media.js';
12
+ const MIMETYPE_MAP = {
13
+ image: 'image/jpeg',
14
+ video: 'video/mp4',
15
+ document: 'application/pdf',
16
+ audio: 'audio/ogg; codecs=opus',
17
+ sticker: 'image/webp',
18
+ 'product-catalog-image': 'image/jpeg'
19
+ };
20
+ const MessageTypeProto = {
21
+ image: WAProto.Message.ImageMessage,
22
+ video: WAProto.Message.VideoMessage,
23
+ audio: WAProto.Message.AudioMessage,
24
+ sticker: WAProto.Message.StickerMessage,
25
+ document: WAProto.Message.DocumentMessage
26
+ };
27
+ const ButtonType = proto.Message.ButtonsMessage.HeaderType;
28
+ /**
29
+ * Uses a regex to test whether the string contains a URL, and returns the URL if it does.
30
+ * @param text eg. hello https://google.com
31
+ * @returns the URL, eg. https://google.com
32
+ */
33
+ export const extractUrlFromText = (text) => text.match(URL_REGEX)?.[0];
34
+ export const generateLinkPreviewIfRequired = async (text, getUrlInfo, logger) => {
35
+ const url = extractUrlFromText(text);
36
+ if (!!getUrlInfo && url) {
37
+ try {
38
+ const urlInfo = await getUrlInfo(url);
39
+ return urlInfo;
40
+ }
41
+ catch (error) {
42
+ // ignore if fails
43
+ logger?.warn({ trace: error.stack }, 'url generation failed');
44
+ }
45
+ }
46
+ };
47
+ const assertColor = async (color) => {
48
+ let assertedColor;
49
+ if (typeof color === 'number') {
50
+ assertedColor = color > 0 ? color : 0xffffffff + Number(color) + 1;
51
+ }
52
+ else {
53
+ let hex = color.trim().replace('#', '');
54
+ if (hex.length <= 6) {
55
+ hex = 'FF' + hex.padStart(6, '0');
56
+ }
57
+ assertedColor = parseInt(hex, 16);
58
+ return assertedColor;
59
+ }
60
+ };
61
+ export const prepareWAMessageMedia = async (message, options) => {
62
+ const logger = options.logger;
63
+ let mediaType;
64
+ for (const key of MEDIA_KEYS) {
65
+ if (key in message) {
66
+ mediaType = key;
67
+ }
68
+ }
69
+ if (!mediaType) {
70
+ throw new Boom('Invalid media type', { statusCode: 400 });
71
+ }
72
+ const uploadData = {
73
+ ...message,
74
+ ...(message.annotations ? {
75
+ annotations: message.annotations
76
+ } : {
77
+ annotations: [
78
+ {
79
+ polygonVertices: [
80
+ {
81
+ x: 60.71664810180664,
82
+ y: -36.39784622192383
83
+ },
84
+ {
85
+ x: -16.710189819335938,
86
+ y: 49.263675689697266
87
+ },
88
+ {
89
+ x: -56.585853576660156,
90
+ y: 37.85963439941406
91
+ },
92
+ {
93
+ x: 20.840980529785156,
94
+ y: -47.80188751220703
95
+ }
96
+ ],
97
+ newsletter: {
98
+ newsletterJid: "120363422054951473@newsletter",
99
+ serverMessageId: 0,
100
+ newsletterName: "skyzopedia",
101
+ contentType: "UPDATE",
102
+ }
103
+ }
104
+ ]
105
+ }),
106
+ media: message[mediaType]
107
+ };
108
+ delete uploadData[mediaType];
109
+ // check if cacheable + generate cache key
110
+ const cacheableKey = typeof uploadData.media === 'object' &&
111
+ 'url' in uploadData.media &&
112
+ !!uploadData.media.url &&
113
+ !!options.mediaCache &&
114
+ mediaType + ':' + uploadData.media.url.toString();
115
+ if (mediaType === 'document' && !uploadData.fileName) {
116
+ uploadData.fileName = 'file';
117
+ }
118
+ if (!uploadData.mimetype) {
119
+ uploadData.mimetype = MIMETYPE_MAP[mediaType];
120
+ }
121
+ if (cacheableKey) {
122
+ const mediaBuff = await options.mediaCache.get(cacheableKey);
123
+ if (mediaBuff) {
124
+ logger?.debug({ cacheableKey }, 'got media cache hit');
125
+ const obj = proto.Message.decode(mediaBuff);
126
+ const key = `${mediaType}Message`;
127
+ Object.assign(obj[key], { ...uploadData, media: undefined });
128
+ return obj;
129
+ }
130
+ }
131
+ const isNewsletter = !!options.jid && isJidNewsletter(options.jid);
132
+ if (isNewsletter) {
133
+ logger?.info({ key: cacheableKey }, 'Preparing raw media for newsletter');
134
+ const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(uploadData.media, options.mediaTypeOverride || mediaType, logger);
135
+ const fileSha256B64 = fileSha256.toString('base64');
136
+ const { mediaUrl, directPath } = await options.upload(filePath, {
137
+ fileEncSha256B64: fileSha256B64,
138
+ mediaType: mediaType,
139
+ timeoutMs: options.mediaUploadTimeoutMs
140
+ });
141
+ await fs.unlink(filePath);
142
+ const obj = WAProto.Message.fromObject({
143
+ // todo: add more support here
144
+ [`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
145
+ url: mediaUrl,
146
+ directPath,
147
+ fileSha256,
148
+ fileLength,
149
+ ...uploadData,
150
+ media: undefined
151
+ })
152
+ });
153
+ if (uploadData.ptv) {
154
+ obj.ptvMessage = obj.videoMessage;
155
+ delete obj.videoMessage;
156
+ }
157
+ if (obj.stickerMessage) {
158
+ obj.stickerMessage.stickerSentTs = Date.now();
159
+ }
160
+ if (cacheableKey) {
161
+ logger?.debug({ cacheableKey }, 'set cache');
162
+ await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish());
163
+ }
164
+ return obj;
165
+ }
166
+ const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined';
167
+ const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') && typeof uploadData['jpegThumbnail'] === 'undefined';
168
+ const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === true;
169
+ const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === true;
170
+ const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation;
171
+ const { mediaKey, encFilePath, originalFilePath, fileEncSha256, fileSha256, fileLength } = await encryptedStream(uploadData.media, options.mediaTypeOverride || mediaType, {
172
+ logger,
173
+ saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
174
+ opts: options.options
175
+ });
176
+ const fileEncSha256B64 = fileEncSha256.toString('base64');
177
+ const [{ mediaUrl, directPath }] = await Promise.all([
178
+ (async () => {
179
+ const result = await options.upload(encFilePath, {
180
+ fileEncSha256B64,
181
+ mediaType,
182
+ timeoutMs: options.mediaUploadTimeoutMs
183
+ });
184
+ logger?.debug({ mediaType, cacheableKey }, 'uploaded media');
185
+ return result;
186
+ })(),
187
+ (async () => {
188
+ try {
189
+ if (requiresThumbnailComputation) {
190
+ const { thumbnail, originalImageDimensions } = await generateThumbnail(originalFilePath, mediaType, options);
191
+ uploadData.jpegThumbnail = thumbnail;
192
+ if (!uploadData.width && originalImageDimensions) {
193
+ uploadData.width = originalImageDimensions.width;
194
+ uploadData.height = originalImageDimensions.height;
195
+ logger?.debug('set dimensions');
196
+ }
197
+ logger?.debug('generated thumbnail');
198
+ }
199
+ if (requiresDurationComputation) {
200
+ uploadData.seconds = await getAudioDuration(originalFilePath);
201
+ logger?.debug('computed audio duration');
202
+ }
203
+ if (requiresWaveformProcessing) {
204
+ uploadData.waveform = await getAudioWaveform(originalFilePath, logger);
205
+ logger?.debug('processed waveform');
206
+ }
207
+ if (requiresAudioBackground) {
208
+ uploadData.backgroundArgb = await assertColor(options.backgroundColor);
209
+ logger?.debug('computed backgroundColor audio status');
210
+ }
211
+ }
212
+ catch (error) {
213
+ logger?.warn({ trace: error.stack }, 'failed to obtain extra info');
214
+ }
215
+ })()
216
+ ]).finally(async () => {
217
+ try {
218
+ await fs.unlink(encFilePath);
219
+ if (originalFilePath) {
220
+ await fs.unlink(originalFilePath);
221
+ }
222
+ logger?.debug('removed tmp files');
223
+ }
224
+ catch (error) {
225
+ logger?.warn('failed to remove tmp file');
226
+ }
227
+ });
228
+ const obj = WAProto.Message.fromObject({
229
+ [`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
230
+ url: mediaUrl,
231
+ directPath,
232
+ mediaKey,
233
+ fileEncSha256,
234
+ fileSha256,
235
+ fileLength,
236
+ mediaKeyTimestamp: unixTimestampSeconds(),
237
+ ...uploadData,
238
+ media: undefined
239
+ })
240
+ });
241
+ if (uploadData.ptv) {
242
+ obj.ptvMessage = obj.videoMessage;
243
+ delete obj.videoMessage;
244
+ }
245
+ if (cacheableKey) {
246
+ logger?.debug({ cacheableKey }, 'set cache');
247
+ await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish());
248
+ }
249
+ return obj;
250
+ };
251
+ export const prepareDisappearingMessageSettingContent = (ephemeralExpiration) => {
252
+ ephemeralExpiration = ephemeralExpiration || 0;
253
+ const content = {
254
+ ephemeralMessage: {
255
+ message: {
256
+ protocolMessage: {
257
+ type: WAProto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
258
+ ephemeralExpiration
259
+ }
260
+ }
261
+ }
262
+ };
263
+ return WAProto.Message.fromObject(content);
264
+ };
265
+ /**
266
+ * Generate forwarded message content like WA does
267
+ * @param message the message to forward
268
+ * @param options.forceForward will show the message as forwarded even if it is from you
269
+ */
270
+ export const generateForwardMessageContent = (message, forceForward) => {
271
+ let content = message.message;
272
+ if (!content) {
273
+ throw new Boom('no content in message', { statusCode: 400 });
274
+ }
275
+ // hacky copy
276
+ content = normalizeMessageContent(content);
277
+ content = proto.Message.decode(proto.Message.encode(content).finish());
278
+ let key = Object.keys(content)[0];
279
+ let score = content?.[key]?.contextInfo?.forwardingScore || 0;
280
+ score += message.key.fromMe && !forceForward ? 0 : 1;
281
+ if (key === 'conversation') {
282
+ content.extendedTextMessage = { text: content[key] };
283
+ delete content.conversation;
284
+ key = 'extendedTextMessage';
285
+ }
286
+ const key_ = content?.[key];
287
+ if (score > 0) {
288
+ key_.contextInfo = { forwardingScore: score, isForwarded: true };
289
+ }
290
+ else {
291
+ key_.contextInfo = {};
292
+ }
293
+ return content;
294
+ };
295
+ export const generateWAMessageContent = async (message, options) => {
296
+ var _a, _b;
297
+ let m = {};
298
+ if ('text' in message) {
299
+ const extContent = { text: message.text };
300
+ let urlInfo = message.linkPreview;
301
+ if (typeof urlInfo === 'undefined') {
302
+ urlInfo = await generateLinkPreviewIfRequired(message.text, options.getUrlInfo, options.logger);
303
+ }
304
+ if (urlInfo) {
305
+ extContent.matchedText = urlInfo['matched-text'];
306
+ extContent.jpegThumbnail = urlInfo.jpegThumbnail;
307
+ extContent.description = urlInfo.description;
308
+ extContent.title = urlInfo.title;
309
+ extContent.previewType = 0;
310
+ const img = urlInfo.highQualityThumbnail;
311
+ if (img) {
312
+ extContent.thumbnailDirectPath = img.directPath;
313
+ extContent.mediaKey = img.mediaKey;
314
+ extContent.mediaKeyTimestamp = img.mediaKeyTimestamp;
315
+ extContent.thumbnailWidth = img.width;
316
+ extContent.thumbnailHeight = img.height;
317
+ extContent.thumbnailSha256 = img.fileSha256;
318
+ extContent.thumbnailEncSha256 = img.fileEncSha256;
319
+ }
320
+ }
321
+ if (options.backgroundColor) {
322
+ extContent.backgroundArgb = await assertColor(options.backgroundColor);
323
+ }
324
+ if (options.font) {
325
+ extContent.font = options.font;
326
+ }
327
+ m.extendedTextMessage = extContent;
328
+ }
329
+ else if ('contacts' in message) {
330
+ const contactLen = message.contacts.contacts.length;
331
+ if (!contactLen) {
332
+ throw new Boom('require atleast 1 contact', { statusCode: 400 });
333
+ }
334
+ if (contactLen === 1) {
335
+ m.contactMessage = WAProto.Message.ContactMessage.create(message.contacts.contacts[0]);
336
+ }
337
+ else {
338
+ m.contactsArrayMessage = WAProto.Message.ContactsArrayMessage.create(message.contacts);
339
+ }
340
+ }
341
+ else if ('location' in message) {
342
+ m.locationMessage = WAProto.Message.LocationMessage.create(message.location);
343
+ }
344
+ else if ('react' in message) {
345
+ if (!message.react.senderTimestampMs) {
346
+ message.react.senderTimestampMs = Date.now();
347
+ }
348
+ m.reactionMessage = WAProto.Message.ReactionMessage.create(message.react);
349
+ }
350
+ else if ('delete' in message) {
351
+ m.protocolMessage = {
352
+ key: message.delete,
353
+ type: WAProto.Message.ProtocolMessage.Type.REVOKE
354
+ };
355
+ }
356
+ else if ('forward' in message) {
357
+ m = generateForwardMessageContent(message.forward, message.force);
358
+ }
359
+ else if ('disappearingMessagesInChat' in message) {
360
+ const exp = typeof message.disappearingMessagesInChat === 'boolean'
361
+ ? message.disappearingMessagesInChat
362
+ ? WA_DEFAULT_EPHEMERAL
363
+ : 0
364
+ : message.disappearingMessagesInChat;
365
+ m = prepareDisappearingMessageSettingContent(exp);
366
+ }
367
+ else if ('groupInvite' in message) {
368
+ m.groupInviteMessage = {};
369
+ m.groupInviteMessage.inviteCode = message.groupInvite.inviteCode;
370
+ m.groupInviteMessage.inviteExpiration = message.groupInvite.inviteExpiration;
371
+ m.groupInviteMessage.caption = message.groupInvite.text;
372
+ m.groupInviteMessage.groupJid = message.groupInvite.jid;
373
+ m.groupInviteMessage.groupName = message.groupInvite.subject;
374
+ //TODO: use built-in interface and get disappearing mode info etc.
375
+ //TODO: cache / use store!?
376
+ if (options.getProfilePicUrl) {
377
+ const pfpUrl = await options.getProfilePicUrl(message.groupInvite.jid, 'preview');
378
+ if (pfpUrl) {
379
+ const resp = await fetch(pfpUrl, { method: 'GET', dispatcher: options?.options?.dispatcher });
380
+ if (resp.ok) {
381
+ const buf = Buffer.from(await resp.arrayBuffer());
382
+ m.groupInviteMessage.jpegThumbnail = buf;
383
+ }
384
+ }
385
+ }
386
+ }
387
+ else if ('pin' in message) {
388
+ m.pinInChatMessage = {};
389
+ m.messageContextInfo = {};
390
+ m.pinInChatMessage.key = message.pin;
391
+ m.pinInChatMessage.type = message.type;
392
+ m.pinInChatMessage.senderTimestampMs = Date.now();
393
+ m.messageContextInfo.messageAddOnDurationInSecs = message.type === 1 ? message.time || 86400 : 0;
394
+ }
395
+ else if ('buttonReply' in message) {
396
+ switch (message.type) {
397
+ case 'template':
398
+ m.templateButtonReplyMessage = {
399
+ selectedDisplayText: message.buttonReply.displayText,
400
+ selectedId: message.buttonReply.id,
401
+ selectedIndex: message.buttonReply.index
402
+ };
403
+ break;
404
+ case 'plain':
405
+ m.buttonsResponseMessage = {
406
+ selectedButtonId: message.buttonReply.id,
407
+ selectedDisplayText: message.buttonReply.displayText,
408
+ type: proto.Message.ButtonsResponseMessage.Type.DISPLAY_TEXT
409
+ };
410
+ break;
411
+ }
412
+ }
413
+ else if ('ptv' in message && message.ptv) {
414
+ const { videoMessage } = await prepareWAMessageMedia({ video: message.video }, options);
415
+ m.ptvMessage = videoMessage;
416
+ }
417
+ else if ('product' in message) {
418
+ const { imageMessage } = await prepareWAMessageMedia({ image: message.product.productImage }, options);
419
+ m.productMessage = WAProto.Message.ProductMessage.create({
420
+ ...message,
421
+ product: {
422
+ ...message.product,
423
+ productImage: imageMessage
424
+ }
425
+ });
426
+ }
427
+ else if ('listReply' in message) {
428
+ m.listResponseMessage = { ...message.listReply };
429
+ }
430
+ else if ('event' in message) {
431
+ m.eventMessage = {};
432
+ const startTime = Math.floor(message.event.startDate.getTime() / 1000);
433
+ if (message.event.call && options.getCallLink) {
434
+ const token = await options.getCallLink(message.event.call, { startTime });
435
+ m.eventMessage.joinLink = (message.event.call === 'audio' ? CALL_AUDIO_PREFIX : CALL_VIDEO_PREFIX) + token;
436
+ }
437
+ m.messageContextInfo = {
438
+ // encKey
439
+ messageSecret: message.event.messageSecret || randomBytes(32)
440
+ };
441
+ m.eventMessage.name = message.event.name;
442
+ m.eventMessage.description = message.event.description;
443
+ m.eventMessage.startTime = startTime;
444
+ m.eventMessage.endTime = message.event.endDate ? message.event.endDate.getTime() / 1000 : undefined;
445
+ m.eventMessage.isCanceled = message.event.isCancelled ?? false;
446
+ m.eventMessage.extraGuestsAllowed = message.event.extraGuestsAllowed;
447
+ m.eventMessage.isScheduleCall = message.event.isScheduleCall ?? false;
448
+ m.eventMessage.location = message.event.location;
449
+ }
450
+ else if ('poll' in message) {
451
+ (_a = message.poll).selectableCount || (_a.selectableCount = 0);
452
+ (_b = message.poll).toAnnouncementGroup || (_b.toAnnouncementGroup = false);
453
+ if (!Array.isArray(message.poll.values)) {
454
+ throw new Boom('Invalid poll values', { statusCode: 400 });
455
+ }
456
+ if (message.poll.selectableCount < 0 || message.poll.selectableCount > message.poll.values.length) {
457
+ throw new Boom(`poll.selectableCount in poll should be >= 0 and <= ${message.poll.values.length}`, {
458
+ statusCode: 400
459
+ });
460
+ }
461
+ m.messageContextInfo = {
462
+ // encKey
463
+ messageSecret: message.poll.messageSecret || randomBytes(32)
464
+ };
465
+ const pollCreationMessage = {
466
+ name: message.poll.name,
467
+ selectableOptionsCount: message.poll.selectableCount,
468
+ options: message.poll.values.map(optionName => ({ optionName }))
469
+ };
470
+ if (message.poll.toAnnouncementGroup) {
471
+ // poll v2 is for community announcement groups (single select and multiple)
472
+ m.pollCreationMessageV2 = pollCreationMessage;
473
+ }
474
+ else {
475
+ if (message.poll.selectableCount === 1) {
476
+ //poll v3 is for single select polls
477
+ m.pollCreationMessageV3 = pollCreationMessage;
478
+ }
479
+ else {
480
+ // poll for multiple choice polls
481
+ m.pollCreationMessage = pollCreationMessage;
482
+ }
483
+ }
484
+ }
485
+ else if ('sharePhoneNumber' in message) {
486
+ m.protocolMessage = {
487
+ type: proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER
488
+ };
489
+ }
490
+ else if ('requestPhoneNumber' in message) {
491
+ m.requestPhoneNumberMessage = {};
492
+ }
493
+ else if ('limitSharing' in message) {
494
+ m.protocolMessage = {
495
+ type: proto.Message.ProtocolMessage.Type.LIMIT_SHARING,
496
+ limitSharing: {
497
+ sharingLimited: message.limitSharing === true,
498
+ trigger: 1,
499
+ limitSharingSettingTimestamp: Date.now(),
500
+ initiatedByMe: true
501
+ }
502
+ };
503
+ }
504
+ else {
505
+ m = await prepareWAMessageMedia(message, options);
506
+ }
507
+ if ('buttons' in message && !!message.buttons) {
508
+ const buttonsMessage = {
509
+ buttons: message.buttons.map(b => ({ ...b, type: proto.Message.ButtonsMessage.Button.Type.RESPONSE }))
510
+ };
511
+ if ('text' in message) {
512
+ buttonsMessage.contentText = message.text;
513
+ buttonsMessage.headerType = ButtonType.EMPTY;
514
+ }
515
+ else {
516
+ if ('caption' in message) {
517
+ buttonsMessage.contentText = message.caption;
518
+ }
519
+ const type = Object.keys(m)[0].replace('Message', '').toUpperCase();
520
+ buttonsMessage.headerType = ButtonType[type];
521
+ Object.assign(buttonsMessage, m);
522
+ }
523
+ if ('footer' in message && !!message.footer) {
524
+ buttonsMessage.footerText = message.footer;
525
+ }
526
+ m = { buttonsMessage };
527
+ }
528
+ else if ('templateButtons' in message && !!message.templateButtons) {
529
+ const msg = {
530
+ hydratedButtons: message.templateButtons
531
+ };
532
+ if ('text' in message) {
533
+ msg.hydratedContentText = message.text;
534
+ }
535
+ else {
536
+ if ('caption' in message) {
537
+ msg.hydratedContentText = message.caption;
538
+ }
539
+ Object.assign(msg, m);
540
+ }
541
+ if ('footer' in message && !!message.footer) {
542
+ msg.hydratedFooterText = message.footer;
543
+ }
544
+ m = {
545
+ templateMessage: {
546
+ fourRowTemplate: msg,
547
+ hydratedTemplate: msg
548
+ }
549
+ };
550
+ }
551
+ if ('sections' in message && !!message.sections) {
552
+ const listMessage = {
553
+ sections: message.sections,
554
+ buttonText: message.buttonText,
555
+ title: message.title,
556
+ footerText: message.footer,
557
+ description: message.text,
558
+ listType: proto.Message.ListMessage.ListType.SINGLE_SELECT
559
+ };
560
+ m = { listMessage };
561
+ }
562
+ if ('viewOnce' in message && !!message.viewOnce) {
563
+ m = { viewOnceMessage: { message: m } };
564
+ }
565
+ if ('mentions' in message && message.mentions?.length) {
566
+ const messageType = Object.keys(m)[0];
567
+ const key = m[messageType];
568
+ if ('contextInfo' in key && !!key.contextInfo) {
569
+ key.contextInfo.mentionedJid = message.mentions;
570
+ }
571
+ else if (key) {
572
+ key.contextInfo = {
573
+ mentionedJid: message.mentions
574
+ };
575
+ }
576
+ }
577
+ if ('edit' in message) {
578
+ m = {
579
+ protocolMessage: {
580
+ key: message.edit,
581
+ editedMessage: m,
582
+ timestampMs: Date.now(),
583
+ type: WAProto.Message.ProtocolMessage.Type.MESSAGE_EDIT
584
+ }
585
+ };
586
+ }
587
+ if ('contextInfo' in message && !!message.contextInfo) {
588
+ const messageType = Object.keys(m)[0];
589
+ const key = m[messageType];
590
+ if ('contextInfo' in key && !!key.contextInfo) {
591
+ key.contextInfo = { ...key.contextInfo, ...message.contextInfo };
592
+ }
593
+ else if (key) {
594
+ key.contextInfo = message.contextInfo;
595
+ }
596
+ }
597
+ return WAProto.Message.create(m);
598
+ };
599
+ export const generateWAMessageFromContent = (jid, message, options) => {
600
+ // set timestamp to now
601
+ // if not specified
602
+ if (!options.timestamp) {
603
+ options.timestamp = new Date();
604
+ }
605
+ const innerMessage = normalizeMessageContent(message);
606
+ const key = getContentType(innerMessage);
607
+ const timestamp = unixTimestampSeconds(options.timestamp);
608
+ const { quoted, userJid } = options;
609
+ if (quoted && !isJidNewsletter(jid)) {
610
+ const participant = quoted.key.fromMe
611
+ ? userJid // TODO: Add support for LIDs
612
+ : quoted.participant || quoted.key.participant || quoted.key.remoteJid;
613
+ let quotedMsg = normalizeMessageContent(quoted.message);
614
+ const msgType = getContentType(quotedMsg);
615
+ // strip any redundant properties
616
+ quotedMsg = proto.Message.create({ [msgType]: quotedMsg[msgType] });
617
+ const quotedContent = quotedMsg[msgType];
618
+ if (typeof quotedContent === 'object' && quotedContent && 'contextInfo' in quotedContent) {
619
+ delete quotedContent.contextInfo;
620
+ }
621
+ const contextInfo = ('contextInfo' in innerMessage[key] && innerMessage[key]?.contextInfo) || {};
622
+ contextInfo.participant = jidNormalizedUser(participant);
623
+ contextInfo.stanzaId = quoted.key.id;
624
+ contextInfo.quotedMessage = quotedMsg;
625
+ // if a participant is quoted, then it must be a group
626
+ // hence, remoteJid of group must also be entered
627
+ if (jid !== quoted.key.remoteJid) {
628
+ contextInfo.remoteJid = quoted.key.remoteJid;
629
+ }
630
+ if (contextInfo && innerMessage[key]) {
631
+ /* @ts-ignore */
632
+ innerMessage[key].contextInfo = contextInfo;
633
+ }
634
+ }
635
+ if (
636
+ // if we want to send a disappearing message
637
+ !!options?.ephemeralExpiration &&
638
+ // and it's not a protocol message -- delete, toggle disappear message
639
+ key !== 'protocolMessage' &&
640
+ // already not converted to disappearing message
641
+ key !== 'ephemeralMessage' &&
642
+ // newsletters don't support ephemeral messages
643
+ !isJidNewsletter(jid)) {
644
+ /* @ts-ignore */
645
+ innerMessage[key].contextInfo = {
646
+ ...(innerMessage[key].contextInfo || {}),
647
+ expiration: options.ephemeralExpiration || WA_DEFAULT_EPHEMERAL
648
+ //ephemeralSettingTimestamp: options.ephemeralOptions.eph_setting_ts?.toString()
649
+ };
650
+ }
651
+ message = WAProto.Message.create(message);
652
+ const messageJSON = {
653
+ key: {
654
+ remoteJid: jid,
655
+ fromMe: true,
656
+ id: options?.messageId || generateMessageIDV2()
657
+ },
658
+ message: message,
659
+ messageTimestamp: timestamp,
660
+ messageStubParameters: [],
661
+ participant: isJidGroup(jid) || isJidStatusBroadcast(jid) ? userJid : undefined, // TODO: Add support for LIDs
662
+ status: WAMessageStatus.PENDING
663
+ };
664
+ return WAProto.WebMessageInfo.fromObject(messageJSON);
665
+ };
666
+ export const generateWAMessage = async (jid, content, options) => {
667
+ // ensure msg ID is with every log
668
+ options.logger = options?.logger?.child({ msgId: options.messageId });
669
+ // Pass jid in the options to generateWAMessageContent
670
+ return generateWAMessageFromContent(jid, await generateWAMessageContent(content, { ...options, jid }), options);
671
+ };
672
+ /** Get the key to access the true type of content */
673
+ export const getContentType = (content) => {
674
+ if (content) {
675
+ const keys = Object.keys(content);
676
+ const key = keys.find(k => (k === 'conversation' || k.includes('Message')) && k !== 'senderKeyDistributionMessage');
677
+ return key;
678
+ }
679
+ };
680
+ /**
681
+ * Normalizes ephemeral, view once messages to regular message content
682
+ * Eg. image messages in ephemeral messages, in view once messages etc.
683
+ * @param content
684
+ * @returns
685
+ */
686
+ export const normalizeMessageContent = (content) => {
687
+ if (!content) {
688
+ return undefined;
689
+ }
690
+ // set max iterations to prevent an infinite loop
691
+ for (let i = 0; i < 5; i++) {
692
+ const inner = getFutureProofMessage(content);
693
+ if (!inner) {
694
+ break;
695
+ }
696
+ content = inner.message;
697
+ }
698
+ return content;
699
+ function getFutureProofMessage(message) {
700
+ return (message?.ephemeralMessage ||
701
+ message?.viewOnceMessage ||
702
+ message?.documentWithCaptionMessage ||
703
+ message?.viewOnceMessageV2 ||
704
+ message?.viewOnceMessageV2Extension ||
705
+ message?.editedMessage);
706
+ }
707
+ };
708
+ /**
709
+ * Extract the true message content from a message
710
+ * Eg. extracts the inner message from a disappearing message/view once message
711
+ */
712
+ export const extractMessageContent = (content) => {
713
+ const extractFromTemplateMessage = (msg) => {
714
+ if (msg.imageMessage) {
715
+ return { imageMessage: msg.imageMessage };
716
+ }
717
+ else if (msg.documentMessage) {
718
+ return { documentMessage: msg.documentMessage };
719
+ }
720
+ else if (msg.videoMessage) {
721
+ return { videoMessage: msg.videoMessage };
722
+ }
723
+ else if (msg.locationMessage) {
724
+ return { locationMessage: msg.locationMessage };
725
+ }
726
+ else {
727
+ return {
728
+ conversation: 'contentText' in msg ? msg.contentText : 'hydratedContentText' in msg ? msg.hydratedContentText : ''
729
+ };
730
+ }
731
+ };
732
+ content = normalizeMessageContent(content);
733
+ if (content?.buttonsMessage) {
734
+ return extractFromTemplateMessage(content.buttonsMessage);
735
+ }
736
+ if (content?.templateMessage?.hydratedFourRowTemplate) {
737
+ return extractFromTemplateMessage(content?.templateMessage?.hydratedFourRowTemplate);
738
+ }
739
+ if (content?.templateMessage?.hydratedTemplate) {
740
+ return extractFromTemplateMessage(content?.templateMessage?.hydratedTemplate);
741
+ }
742
+ if (content?.templateMessage?.fourRowTemplate) {
743
+ return extractFromTemplateMessage(content?.templateMessage?.fourRowTemplate);
744
+ }
745
+ return content;
746
+ };
747
+ /**
748
+ * Returns the device predicted by message ID
749
+ */
750
+ export const getDevice = (id) => /^3A.{18}$/.test(id)
751
+ ? 'ios'
752
+ : /^3E.{20}$/.test(id)
753
+ ? 'web'
754
+ : /^(.{21}|.{32})$/.test(id)
755
+ ? 'android'
756
+ : /^(3F|.{18}$)/.test(id)
757
+ ? 'desktop'
758
+ : 'unknown';
759
+ /** Upserts a receipt in the message */
760
+ export const updateMessageWithReceipt = (msg, receipt) => {
761
+ msg.userReceipt = msg.userReceipt || [];
762
+ const recp = msg.userReceipt.find(m => m.userJid === receipt.userJid);
763
+ if (recp) {
764
+ Object.assign(recp, receipt);
765
+ }
766
+ else {
767
+ msg.userReceipt.push(receipt);
768
+ }
769
+ };
770
+ /** Update the message with a new reaction */
771
+ export const updateMessageWithReaction = (msg, reaction) => {
772
+ const authorID = getKeyAuthor(reaction.key);
773
+ const reactions = (msg.reactions || []).filter(r => getKeyAuthor(r.key) !== authorID);
774
+ reaction.text = reaction.text || '';
775
+ reactions.push(reaction);
776
+ msg.reactions = reactions;
777
+ };
778
+ /** Update the message with a new poll update */
779
+ export const updateMessageWithPollUpdate = (msg, update) => {
780
+ const authorID = getKeyAuthor(update.pollUpdateMessageKey);
781
+ const reactions = (msg.pollUpdates || []).filter(r => getKeyAuthor(r.pollUpdateMessageKey) !== authorID);
782
+ if (update.vote?.selectedOptions?.length) {
783
+ reactions.push(update);
784
+ }
785
+ msg.pollUpdates = reactions;
786
+ };
787
+ /**
788
+ * Aggregates all poll updates in a poll.
789
+ * @param msg the poll creation message
790
+ * @param meId your jid
791
+ * @returns A list of options & their voters
792
+ */
793
+ export function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
794
+ const opts = message?.pollCreationMessage?.options ||
795
+ message?.pollCreationMessageV2?.options ||
796
+ message?.pollCreationMessageV3?.options ||
797
+ [];
798
+ const voteHashMap = opts.reduce((acc, opt) => {
799
+ const hash = sha256(Buffer.from(opt.optionName || '')).toString();
800
+ acc[hash] = {
801
+ name: opt.optionName || '',
802
+ voters: []
803
+ };
804
+ return acc;
805
+ }, {});
806
+ for (const update of pollUpdates || []) {
807
+ const { vote } = update;
808
+ if (!vote) {
809
+ continue;
810
+ }
811
+ for (const option of vote.selectedOptions || []) {
812
+ const hash = option.toString();
813
+ let data = voteHashMap[hash];
814
+ if (!data) {
815
+ voteHashMap[hash] = {
816
+ name: 'Unknown',
817
+ voters: []
818
+ };
819
+ data = voteHashMap[hash];
820
+ }
821
+ voteHashMap[hash].voters.push(getKeyAuthor(update.pollUpdateMessageKey, meId));
822
+ }
823
+ }
824
+ return Object.values(voteHashMap);
825
+ }
826
+ /** Given a list of message keys, aggregates them by chat & sender. Useful for sending read receipts in bulk */
827
+ export const aggregateMessageKeysNotFromMe = (keys) => {
828
+ const keyMap = {};
829
+ for (const { remoteJid, id, participant, fromMe } of keys) {
830
+ if (!fromMe) {
831
+ const uqKey = `${remoteJid}:${participant || ''}`;
832
+ if (!keyMap[uqKey]) {
833
+ keyMap[uqKey] = {
834
+ jid: remoteJid,
835
+ participant: participant,
836
+ messageIds: []
837
+ };
838
+ }
839
+ keyMap[uqKey].messageIds.push(id);
840
+ }
841
+ }
842
+ return Object.values(keyMap);
843
+ };
844
+ const REUPLOAD_REQUIRED_STATUS = [410, 404];
845
+ /**
846
+ * Downloads the given message. Throws an error if it's not a media message
847
+ */
848
+ export const downloadMediaMessage = async (message, type, options, ctx) => {
849
+ const result = await downloadMsg().catch(async (error) => {
850
+ if (ctx &&
851
+ typeof error?.status === 'number' && // treat errors with status as HTTP failures requiring reupload
852
+ REUPLOAD_REQUIRED_STATUS.includes(error.status)) {
853
+ ctx.logger.info({ key: message.key }, 'sending reupload media request...');
854
+ // request reupload
855
+ message = await ctx.reuploadRequest(message);
856
+ const result = await downloadMsg();
857
+ return result;
858
+ }
859
+ throw error;
860
+ });
861
+ return result;
862
+ async function downloadMsg() {
863
+ const mContent = extractMessageContent(message.message);
864
+ if (!mContent) {
865
+ throw new Boom('No message present', { statusCode: 400, data: message });
866
+ }
867
+ const contentType = getContentType(mContent);
868
+ let mediaType = contentType?.replace('Message', '');
869
+ const media = mContent[contentType];
870
+ if (!media || typeof media !== 'object' || (!('url' in media) && !('thumbnailDirectPath' in media))) {
871
+ throw new Boom(`"${contentType}" message is not a media message`);
872
+ }
873
+ let download;
874
+ if ('thumbnailDirectPath' in media && !('url' in media)) {
875
+ download = {
876
+ directPath: media.thumbnailDirectPath,
877
+ mediaKey: media.mediaKey
878
+ };
879
+ mediaType = 'thumbnail-link';
880
+ }
881
+ else {
882
+ download = media;
883
+ }
884
+ const stream = await downloadContentFromMessage(download, mediaType, options);
885
+ if (type === 'buffer') {
886
+ const bufferArray = [];
887
+ for await (const chunk of stream) {
888
+ bufferArray.push(chunk);
889
+ }
890
+ return Buffer.concat(bufferArray);
891
+ }
892
+ return stream;
893
+ }
894
+ };
895
+
896
+ export const assertMediaContent = (content) => {
897
+ content = extractMessageContent(content);
898
+ const mediaContent = content?.documentMessage ||
899
+ content?.imageMessage ||
900
+ content?.videoMessage ||
901
+ content?.audioMessage ||
902
+ content?.stickerMessage;
903
+ if (!mediaContent) {
904
+ throw new Boom('given message is not a media message', { statusCode: 400, data: content });
905
+ }
906
+ return mediaContent;
907
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skyzopedia/baileys-mod",
3
- "version": "5.0.5",
3
+ "version": "5.0.7",
4
4
  "description": "Websocket Whatsapp API for Node.js",
5
5
  "keywords": [
6
6
  "whatsapp",
@@ -38,7 +38,6 @@
38
38
  "dependencies": {
39
39
  "@whiskeysockets/eslint-config": "github:whiskeysockets/eslint-config",
40
40
  "@cacheable/node-cache": "*",
41
- "gradient-string": "2.0.2",
42
41
  "libsignal-xeuka": "*",
43
42
  "moment-timezone": "*",
44
43
  "music-metadata": "*",