@skyzopedia/baileys-mod 3.0.2
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.
- package/LICENSE +21 -0
- package/WAProto/WAProto.proto +5311 -0
- package/WAProto/index.js +94091 -0
- package/lib/Defaults/index.js +123 -0
- package/lib/KeyDB/BinarySearch.js +20 -0
- package/lib/KeyDB/KeyedDB.js +167 -0
- package/lib/KeyDB/index.js +4 -0
- package/lib/Signal/Group/ciphertext-message.js +13 -0
- package/lib/Signal/Group/group-session-builder.js +32 -0
- package/lib/Signal/Group/group_cipher.js +84 -0
- package/lib/Signal/Group/index.js +13 -0
- package/lib/Signal/Group/keyhelper.js +20 -0
- package/lib/Signal/Group/sender-chain-key.js +28 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +65 -0
- package/lib/Signal/Group/sender-key-message.js +68 -0
- package/lib/Signal/Group/sender-key-name.js +52 -0
- package/lib/Signal/Group/sender-key-record.js +43 -0
- package/lib/Signal/Group/sender-key-state.js +86 -0
- package/lib/Signal/Group/sender-message-key.js +28 -0
- package/lib/Signal/libsignal.js +324 -0
- package/lib/Signal/lid-mapping.js +155 -0
- package/lib/Socket/Client/index.js +4 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.js +52 -0
- package/lib/Socket/business.js +377 -0
- package/lib/Socket/chats.js +881 -0
- package/lib/Socket/communities.js +413 -0
- package/lib/Socket/groups.js +312 -0
- package/lib/Socket/index.js +16 -0
- package/lib/Socket/messages-recv.js +1163 -0
- package/lib/Socket/messages-send.js +1082 -0
- package/lib/Socket/mex.js +45 -0
- package/lib/Socket/newsletter.js +259 -0
- package/lib/Socket/socket.js +781 -0
- package/lib/Store/index.js +6 -0
- package/lib/Store/make-cache-manager-store.js +75 -0
- package/lib/Store/make-in-memory-store.js +290 -0
- package/lib/Store/make-ordered-dictionary.js +79 -0
- package/lib/Store/object-repository.js +25 -0
- package/lib/Types/Auth.js +3 -0
- package/lib/Types/Bussines.js +3 -0
- package/lib/Types/Call.js +3 -0
- package/lib/Types/Chat.js +9 -0
- package/lib/Types/Contact.js +3 -0
- package/lib/Types/Events.js +3 -0
- package/lib/Types/GroupMetadata.js +3 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +12 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Newsletter.js.bak +33 -0
- package/lib/Types/Product.js +3 -0
- package/lib/Types/Signal.js +3 -0
- package/lib/Types/Socket.js +4 -0
- package/lib/Types/State.js +11 -0
- package/lib/Types/USync.js +3 -0
- package/lib/Types/index.js +28 -0
- package/lib/Utils/auth-utils.js +219 -0
- package/lib/Utils/baileys-event-stream.js +44 -0
- package/lib/Utils/browser-utils.js +17 -0
- package/lib/Utils/business.js +233 -0
- package/lib/Utils/chat-utils.js +752 -0
- package/lib/Utils/crypto.js +130 -0
- package/lib/Utils/decode-wa-message.js +267 -0
- package/lib/Utils/event-buffer.js +528 -0
- package/lib/Utils/generics.js +355 -0
- package/lib/Utils/history.js +87 -0
- package/lib/Utils/index.js +21 -0
- package/lib/Utils/link-preview.js +81 -0
- package/lib/Utils/logger.js +5 -0
- package/lib/Utils/lt-hash.js +45 -0
- package/lib/Utils/make-mutex.js +36 -0
- package/lib/Utils/message-retry-manager.js +113 -0
- package/lib/Utils/messages-media.js +601 -0
- package/lib/Utils/messages.js +776 -0
- package/lib/Utils/noise-handler.js +144 -0
- package/lib/Utils/pre-key-manager.js +85 -0
- package/lib/Utils/process-message.js +341 -0
- package/lib/Utils/signal.js +161 -0
- package/lib/Utils/use-multi-file-auth-state.js +111 -0
- package/lib/Utils/validate-connection.js +200 -0
- package/lib/WABinary/constants.js +1303 -0
- package/lib/WABinary/decode.js +240 -0
- package/lib/WABinary/encode.js +218 -0
- package/lib/WABinary/generic-utils.js +113 -0
- package/lib/WABinary/index.js +7 -0
- package/lib/WABinary/jid-utils.js +93 -0
- package/lib/WABinary/types.js +3 -0
- package/lib/WAM/BinaryInfo.js +11 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +154 -0
- package/lib/WAM/index.js +5 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +30 -0
- package/lib/WAUSync/Protocols/index.js +6 -0
- package/lib/WAUSync/USyncQuery.js +90 -0
- package/lib/WAUSync/USyncUser.js +24 -0
- package/lib/WAUSync/index.js +5 -0
- package/lib/index.js +15 -0
- package/package.json +102 -0
|
@@ -0,0 +1,776 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { downloadContentFromMessage, encryptedStream, generateThumbnail, getAudioDuration, getAudioWaveform, getRawMediaUploadData } from "./messages-media.js";
|
|
3
|
+
import { CALL_AUDIO_PREFIX, CALL_VIDEO_PREFIX, MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from "../Defaults/index.js";
|
|
4
|
+
import { isJidGroup, isJidNewsletter, isJidStatusBroadcast, jidNormalizedUser } from "../WABinary/index.js";
|
|
5
|
+
import { generateMessageIDV2, getKeyAuthor, unixTimestampSeconds } from "./generics.js";
|
|
6
|
+
import { WAMessageStatus, WAProto } from "../Types/index.js";
|
|
7
|
+
import { proto } from "../../WAProto/index.js";
|
|
8
|
+
import { sha256 } from "./crypto.js";
|
|
9
|
+
import { randomBytes } from "crypto";
|
|
10
|
+
import { promises as fs } from "fs";
|
|
11
|
+
import { Boom } from "@hapi/boom";
|
|
12
|
+
import {} from "stream";
|
|
13
|
+
//=======================================================//
|
|
14
|
+
const MIMETYPE_MAP = {
|
|
15
|
+
"image": "image/jpeg",
|
|
16
|
+
"video": "video/mp4",
|
|
17
|
+
"document": "application/pdf",
|
|
18
|
+
"audio": "audio/ogg; codecs=opus",
|
|
19
|
+
"sticker": "image/webp",
|
|
20
|
+
"product-catalog-image": "image/jpeg"
|
|
21
|
+
};
|
|
22
|
+
//=======================================================//
|
|
23
|
+
const MessageTypeProto = {
|
|
24
|
+
"image": WAProto.Message.ImageMessage,
|
|
25
|
+
"video": WAProto.Message.VideoMessage,
|
|
26
|
+
"audio": WAProto.Message.AudioMessage,
|
|
27
|
+
"sticker": WAProto.Message.StickerMessage,
|
|
28
|
+
"document": WAProto.Message.DocumentMessage
|
|
29
|
+
};
|
|
30
|
+
//=======================================================//
|
|
31
|
+
export const extractUrlFromText = (text) => text.match(URL_REGEX)?.[0];
|
|
32
|
+
export const generateLinkPreviewIfRequired = async (text, getUrlInfo, logger) => {
|
|
33
|
+
const url = extractUrlFromText(text);
|
|
34
|
+
if (!!getUrlInfo && url) {
|
|
35
|
+
try {
|
|
36
|
+
const urlInfo = await getUrlInfo(url);
|
|
37
|
+
return urlInfo;
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
logger?.warn({ trace: error.stack }, "url generation failed");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const assertColor = async (color) => {
|
|
45
|
+
let assertedColor;
|
|
46
|
+
if (typeof color === "number") {
|
|
47
|
+
assertedColor = color > 0 ? color : 0xffffffff + Number(color) + 1;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
let hex = color.trim().replace("#", "");
|
|
51
|
+
if (hex.length <= 6) {
|
|
52
|
+
hex = "FF" + hex.padStart(6, "0");
|
|
53
|
+
}
|
|
54
|
+
assertedColor = parseInt(hex, 16);
|
|
55
|
+
return assertedColor;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
//=======================================================//
|
|
59
|
+
export const prepareWAMessageMedia = async (message, options) => {
|
|
60
|
+
const logger = options.logger;
|
|
61
|
+
let mediaType;
|
|
62
|
+
for (const key of MEDIA_KEYS) {
|
|
63
|
+
if (key in message) {
|
|
64
|
+
mediaType = key;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (!mediaType) {
|
|
68
|
+
throw new Boom("Invalid media type", { statusCode: 400 });
|
|
69
|
+
}
|
|
70
|
+
const uploadData = {
|
|
71
|
+
...message,
|
|
72
|
+
media: message[mediaType]
|
|
73
|
+
};
|
|
74
|
+
delete uploadData[mediaType];
|
|
75
|
+
const cacheableKey = typeof uploadData.media === "object" &&
|
|
76
|
+
"url" in uploadData.media &&
|
|
77
|
+
!!uploadData.media.url &&
|
|
78
|
+
!!options.mediaCache &&
|
|
79
|
+
mediaType + ":" + uploadData.media.url.toString();
|
|
80
|
+
if (mediaType === "document" && !uploadData.fileName) {
|
|
81
|
+
uploadData.fileName = "file";
|
|
82
|
+
}
|
|
83
|
+
if (!uploadData.mimetype) {
|
|
84
|
+
uploadData.mimetype = MIMETYPE_MAP[mediaType];
|
|
85
|
+
}
|
|
86
|
+
if (cacheableKey) {
|
|
87
|
+
const mediaBuff = await options.mediaCache.get(cacheableKey);
|
|
88
|
+
if (mediaBuff) {
|
|
89
|
+
logger?.debug({ cacheableKey }, "got media cache hit");
|
|
90
|
+
const obj = proto.Message.decode(mediaBuff);
|
|
91
|
+
const key = `${mediaType}Message`;
|
|
92
|
+
Object.assign(obj[key], { ...uploadData, media: undefined });
|
|
93
|
+
return obj;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const isNewsletter = !!options.jid && isJidNewsletter(options.jid);
|
|
97
|
+
if (isNewsletter) {
|
|
98
|
+
logger?.info({ key: cacheableKey }, "Preparing raw media for newsletter");
|
|
99
|
+
const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(uploadData.media, options.mediaTypeOverride || mediaType, logger);
|
|
100
|
+
const fileSha256B64 = fileSha256.toString("base64");
|
|
101
|
+
const { mediaUrl, directPath } = await options.upload(filePath, {
|
|
102
|
+
fileEncSha256B64: fileSha256B64,
|
|
103
|
+
mediaType: mediaType,
|
|
104
|
+
timeoutMs: options.mediaUploadTimeoutMs
|
|
105
|
+
});
|
|
106
|
+
await fs.unlink(filePath);
|
|
107
|
+
const obj = WAProto.Message.fromObject({
|
|
108
|
+
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
109
|
+
url: mediaUrl,
|
|
110
|
+
directPath,
|
|
111
|
+
fileSha256,
|
|
112
|
+
fileLength,
|
|
113
|
+
...uploadData,
|
|
114
|
+
media: undefined
|
|
115
|
+
})
|
|
116
|
+
});
|
|
117
|
+
if (uploadData.ptv) {
|
|
118
|
+
obj.ptvMessage = obj.videoMessage;
|
|
119
|
+
delete obj.videoMessage;
|
|
120
|
+
}
|
|
121
|
+
if (obj.stickerMessage) {
|
|
122
|
+
obj.stickerMessage.stickerSentTs = Date.now();
|
|
123
|
+
}
|
|
124
|
+
if (cacheableKey) {
|
|
125
|
+
logger?.debug({ cacheableKey }, "set cache");
|
|
126
|
+
await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish());
|
|
127
|
+
}
|
|
128
|
+
return obj;
|
|
129
|
+
}
|
|
130
|
+
const requiresDurationComputation = mediaType === "audio" && typeof uploadData.seconds === "undefined";
|
|
131
|
+
const requiresThumbnailComputation = (mediaType === "image" || mediaType === "video") && typeof uploadData["jpegThumbnail"] === "undefined";
|
|
132
|
+
const requiresWaveformProcessing = mediaType === "audio" && uploadData.ptt === true;
|
|
133
|
+
const requiresAudioBackground = options.backgroundColor && mediaType === "audio" && uploadData.ptt === true;
|
|
134
|
+
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation;
|
|
135
|
+
const { mediaKey, encFilePath, originalFilePath, fileEncSha256, fileSha256, fileLength } = await encryptedStream(uploadData.media, options.mediaTypeOverride || mediaType, {
|
|
136
|
+
logger,
|
|
137
|
+
saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
|
|
138
|
+
opts: options.options
|
|
139
|
+
});
|
|
140
|
+
const fileEncSha256B64 = fileEncSha256.toString("base64");
|
|
141
|
+
const [{ mediaUrl, directPath }] = await Promise.all([
|
|
142
|
+
(async () => {
|
|
143
|
+
const result = await options.upload(encFilePath, {
|
|
144
|
+
fileEncSha256B64,
|
|
145
|
+
mediaType,
|
|
146
|
+
timeoutMs: options.mediaUploadTimeoutMs
|
|
147
|
+
});
|
|
148
|
+
logger?.debug({ mediaType, cacheableKey }, "uploaded media");
|
|
149
|
+
return result;
|
|
150
|
+
})(),
|
|
151
|
+
(async () => {
|
|
152
|
+
try {
|
|
153
|
+
if (requiresThumbnailComputation) {
|
|
154
|
+
const { thumbnail, originalImageDimensions } = await generateThumbnail(originalFilePath, mediaType, options);
|
|
155
|
+
uploadData.jpegThumbnail = thumbnail;
|
|
156
|
+
if (!uploadData.width && originalImageDimensions) {
|
|
157
|
+
uploadData.width = originalImageDimensions.width;
|
|
158
|
+
uploadData.height = originalImageDimensions.height;
|
|
159
|
+
logger?.debug("set dimensions");
|
|
160
|
+
}
|
|
161
|
+
logger?.debug("generated thumbnail");
|
|
162
|
+
}
|
|
163
|
+
if (requiresDurationComputation) {
|
|
164
|
+
uploadData.seconds = await getAudioDuration(originalFilePath);
|
|
165
|
+
logger?.debug("computed audio duration");
|
|
166
|
+
}
|
|
167
|
+
if (requiresWaveformProcessing) {
|
|
168
|
+
uploadData.waveform = await getAudioWaveform(originalFilePath, logger);
|
|
169
|
+
logger?.debug("processed waveform");
|
|
170
|
+
}
|
|
171
|
+
if (requiresAudioBackground) {
|
|
172
|
+
uploadData.backgroundArgb = await assertColor(options.backgroundColor);
|
|
173
|
+
logger?.debug("computed backgroundColor audio status");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
logger?.warn({ trace: error.stack }, "failed to obtain extra info");
|
|
178
|
+
}
|
|
179
|
+
})()
|
|
180
|
+
]).finally(async () => {
|
|
181
|
+
try {
|
|
182
|
+
await fs.unlink(encFilePath);
|
|
183
|
+
if (originalFilePath) {
|
|
184
|
+
await fs.unlink(originalFilePath);
|
|
185
|
+
}
|
|
186
|
+
logger?.debug("removed tmp files");
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
logger?.warn("failed to remove tmp file");
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
const obj = WAProto.Message.fromObject({
|
|
193
|
+
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
194
|
+
url: mediaUrl,
|
|
195
|
+
directPath,
|
|
196
|
+
mediaKey,
|
|
197
|
+
fileEncSha256,
|
|
198
|
+
fileSha256,
|
|
199
|
+
fileLength,
|
|
200
|
+
mediaKeyTimestamp: unixTimestampSeconds(),
|
|
201
|
+
...uploadData,
|
|
202
|
+
media: undefined
|
|
203
|
+
})
|
|
204
|
+
});
|
|
205
|
+
if (uploadData.ptv) {
|
|
206
|
+
obj.ptvMessage = obj.videoMessage;
|
|
207
|
+
delete obj.videoMessage;
|
|
208
|
+
}
|
|
209
|
+
if (cacheableKey) {
|
|
210
|
+
logger?.debug({ cacheableKey }, "set cache");
|
|
211
|
+
await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish());
|
|
212
|
+
}
|
|
213
|
+
return obj;
|
|
214
|
+
};
|
|
215
|
+
//=======================================================//
|
|
216
|
+
export const prepareDisappearingMessageSettingContent = (ephemeralExpiration) => {
|
|
217
|
+
ephemeralExpiration = ephemeralExpiration || 0;
|
|
218
|
+
const content = {
|
|
219
|
+
ephemeralMessage: {
|
|
220
|
+
message: {
|
|
221
|
+
protocolMessage: {
|
|
222
|
+
type: WAProto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
|
|
223
|
+
ephemeralExpiration
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
return WAProto.Message.fromObject(content);
|
|
229
|
+
};
|
|
230
|
+
//=======================================================//
|
|
231
|
+
export const generateForwardMessageContent = (message, forceForward) => {
|
|
232
|
+
let content = message.message;
|
|
233
|
+
if (!content) {
|
|
234
|
+
throw new Boom("no content in message", { statusCode: 400 });
|
|
235
|
+
}
|
|
236
|
+
content = normalizeMessageContent(content);
|
|
237
|
+
content = proto.Message.decode(proto.Message.encode(content).finish());
|
|
238
|
+
let key = Object.keys(content)[0];
|
|
239
|
+
let score = content?.[key]?.contextInfo?.forwardingScore || 0;
|
|
240
|
+
score += message.key.fromMe && !forceForward ? 0 : 1;
|
|
241
|
+
if (key === "conversation") {
|
|
242
|
+
content.extendedTextMessage = { text: content[key] };
|
|
243
|
+
delete content.conversation;
|
|
244
|
+
key = "extendedTextMessage";
|
|
245
|
+
}
|
|
246
|
+
const key_ = content?.[key];
|
|
247
|
+
if (score > 0) {
|
|
248
|
+
key_.contextInfo = { forwardingScore: score, isForwarded: true };
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
key_.contextInfo = {};
|
|
252
|
+
}
|
|
253
|
+
return content;
|
|
254
|
+
};
|
|
255
|
+
export const generateWAMessageContent = async (message, options) => {
|
|
256
|
+
var _a, _b;
|
|
257
|
+
let m = {};
|
|
258
|
+
if ("text" in message) {
|
|
259
|
+
const extContent = { text: message.text };
|
|
260
|
+
let urlInfo = message.linkPreview;
|
|
261
|
+
if (typeof urlInfo === "undefined") {
|
|
262
|
+
urlInfo = await generateLinkPreviewIfRequired(message.text, options.getUrlInfo, options.logger);
|
|
263
|
+
}
|
|
264
|
+
if (urlInfo) {
|
|
265
|
+
extContent.matchedText = urlInfo["matched-text"];
|
|
266
|
+
extContent.jpegThumbnail = urlInfo.jpegThumbnail;
|
|
267
|
+
extContent.description = urlInfo.description;
|
|
268
|
+
extContent.title = urlInfo.title;
|
|
269
|
+
extContent.previewType = 0;
|
|
270
|
+
const img = urlInfo.highQualityThumbnail;
|
|
271
|
+
if (img) {
|
|
272
|
+
extContent.thumbnailDirectPath = img.directPath;
|
|
273
|
+
extContent.mediaKey = img.mediaKey;
|
|
274
|
+
extContent.mediaKeyTimestamp = img.mediaKeyTimestamp;
|
|
275
|
+
extContent.thumbnailWidth = img.width;
|
|
276
|
+
extContent.thumbnailHeight = img.height;
|
|
277
|
+
extContent.thumbnailSha256 = img.fileSha256;
|
|
278
|
+
extContent.thumbnailEncSha256 = img.fileEncSha256;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (options.backgroundColor) {
|
|
282
|
+
extContent.backgroundArgb = await assertColor(options.backgroundColor);
|
|
283
|
+
}
|
|
284
|
+
if (options.font) {
|
|
285
|
+
extContent.font = options.font;
|
|
286
|
+
}
|
|
287
|
+
m.extendedTextMessage = extContent;
|
|
288
|
+
}
|
|
289
|
+
else if ("contacts" in message) {
|
|
290
|
+
const contactLen = message.contacts.contacts.length;
|
|
291
|
+
if (!contactLen) {
|
|
292
|
+
throw new Boom("require atleast 1 contact", { statusCode: 400 });
|
|
293
|
+
}
|
|
294
|
+
if (contactLen === 1) {
|
|
295
|
+
m.contactMessage = WAProto.Message.ContactMessage.fromObject(message.contacts.contacts[0]);
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
m.contactsArrayMessage = WAProto.Message.ContactsArrayMessage.fromObject(message.contacts);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
else if ("location" in message) {
|
|
302
|
+
m.locationMessage = WAProto.Message.LocationMessage.fromObject(message.location);
|
|
303
|
+
}
|
|
304
|
+
else if ("react" in message) {
|
|
305
|
+
if (!message.react.senderTimestampMs) {
|
|
306
|
+
message.react.senderTimestampMs = Date.now();
|
|
307
|
+
}
|
|
308
|
+
m.reactionMessage = WAProto.Message.ReactionMessage.fromObject(message.react);
|
|
309
|
+
}
|
|
310
|
+
else if ("delete" in message) {
|
|
311
|
+
m.protocolMessage = {
|
|
312
|
+
key: message.delete,
|
|
313
|
+
type: WAProto.Message.ProtocolMessage.Type.REVOKE
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
else if ("forward" in message) {
|
|
317
|
+
m = generateForwardMessageContent(message.forward, message.force);
|
|
318
|
+
}
|
|
319
|
+
else if ("disappearingMessagesInChat" in message) {
|
|
320
|
+
const exp = typeof message.disappearingMessagesInChat === "boolean"
|
|
321
|
+
? message.disappearingMessagesInChat
|
|
322
|
+
? WA_DEFAULT_EPHEMERAL
|
|
323
|
+
: 0
|
|
324
|
+
: message.disappearingMessagesInChat;
|
|
325
|
+
m = prepareDisappearingMessageSettingContent(exp);
|
|
326
|
+
}
|
|
327
|
+
else if ("groupInvite" in message) {
|
|
328
|
+
m.groupInviteMessage = {};
|
|
329
|
+
m.groupInviteMessage.inviteCode = message.groupInvite.inviteCode;
|
|
330
|
+
m.groupInviteMessage.inviteExpiration = message.groupInvite.inviteExpiration;
|
|
331
|
+
m.groupInviteMessage.caption = message.groupInvite.text;
|
|
332
|
+
m.groupInviteMessage.groupJid = message.groupInvite.jid;
|
|
333
|
+
m.groupInviteMessage.groupName = message.groupInvite.subject;
|
|
334
|
+
if (options.getProfilePicUrl) {
|
|
335
|
+
const pfpUrl = await options.getProfilePicUrl(message.groupInvite.jid, "preview");
|
|
336
|
+
if (pfpUrl) {
|
|
337
|
+
const resp = await fetch(pfpUrl, { method: "GET", dispatcher: options?.options?.dispatcher });
|
|
338
|
+
if (resp.ok) {
|
|
339
|
+
const buf = Buffer.from(await resp.arrayBuffer());
|
|
340
|
+
m.groupInviteMessage.jpegThumbnail = buf;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
else if ("pin" in message) {
|
|
346
|
+
m.pinInChatMessage = {};
|
|
347
|
+
m.messageContextInfo = {};
|
|
348
|
+
m.pinInChatMessage.key = message.pin;
|
|
349
|
+
m.pinInChatMessage.type = message.type;
|
|
350
|
+
m.pinInChatMessage.senderTimestampMs = Date.now();
|
|
351
|
+
m.messageContextInfo.messageAddOnDurationInSecs = message.type === 1 ? message.time || 86400 : 0;
|
|
352
|
+
}
|
|
353
|
+
else if ("buttonReply" in message) {
|
|
354
|
+
switch (message.type) {
|
|
355
|
+
case "template":
|
|
356
|
+
m.templateButtonReplyMessage = {
|
|
357
|
+
selectedDisplayText: message.buttonReply.displayText,
|
|
358
|
+
selectedId: message.buttonReply.id,
|
|
359
|
+
selectedIndex: message.buttonReply.index
|
|
360
|
+
};
|
|
361
|
+
break;
|
|
362
|
+
case "plain":
|
|
363
|
+
m.buttonsResponseMessage = {
|
|
364
|
+
selectedButtonId: message.buttonReply.id,
|
|
365
|
+
selectedDisplayText: message.buttonReply.displayText,
|
|
366
|
+
type: proto.Message.ButtonsResponseMessage.Type.DISPLAY_TEXT
|
|
367
|
+
};
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
else if ("ptv" in message && message.ptv) {
|
|
372
|
+
const { videoMessage } = await prepareWAMessageMedia({ video: message.video }, options);
|
|
373
|
+
m.ptvMessage = videoMessage;
|
|
374
|
+
}
|
|
375
|
+
else if ("product" in message) {
|
|
376
|
+
const { imageMessage } = await prepareWAMessageMedia({ image: message.product.productImage }, options);
|
|
377
|
+
m.productMessage = WAProto.Message.ProductMessage.fromObject({
|
|
378
|
+
...message,
|
|
379
|
+
product: {
|
|
380
|
+
...message.product,
|
|
381
|
+
productImage: imageMessage
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
else if ("listReply" in message) {
|
|
386
|
+
m.listResponseMessage = { ...message.listReply };
|
|
387
|
+
}
|
|
388
|
+
else if ("event" in message) {
|
|
389
|
+
m.eventMessage = {};
|
|
390
|
+
const startTime = Math.floor(message.event.startDate.getTime() / 1000);
|
|
391
|
+
if (message.event.call && options.getCallLink) {
|
|
392
|
+
const token = await options.getCallLink(message.event.call, { startTime });
|
|
393
|
+
m.eventMessage.joinLink = (message.event.call === "audio" ? CALL_AUDIO_PREFIX : CALL_VIDEO_PREFIX) + token;
|
|
394
|
+
}
|
|
395
|
+
m.messageContextInfo = {
|
|
396
|
+
messageSecret: message.event.messageSecret || randomBytes(32)
|
|
397
|
+
};
|
|
398
|
+
m.eventMessage.name = message.event.name;
|
|
399
|
+
m.eventMessage.description = message.event.description;
|
|
400
|
+
m.eventMessage.startTime = startTime;
|
|
401
|
+
m.eventMessage.endTime = message.event.endDate ? message.event.endDate.getTime() / 1000 : undefined;
|
|
402
|
+
m.eventMessage.isCanceled = message.event.isCancelled ?? false;
|
|
403
|
+
m.eventMessage.extraGuestsAllowed = message.event.extraGuestsAllowed;
|
|
404
|
+
m.eventMessage.isScheduleCall = message.event.isScheduleCall ?? false;
|
|
405
|
+
m.eventMessage.location = message.event.location;
|
|
406
|
+
}
|
|
407
|
+
else if ("poll" in message) {
|
|
408
|
+
(_a = message.poll).selectableCount || (_a.selectableCount = 0);
|
|
409
|
+
(_b = message.poll).toAnnouncementGroup || (_b.toAnnouncementGroup = false);
|
|
410
|
+
if (!Array.isArray(message.poll.values)) {
|
|
411
|
+
throw new Boom("Invalid poll values", { statusCode: 400 });
|
|
412
|
+
}
|
|
413
|
+
if (message.poll.selectableCount < 0 || message.poll.selectableCount > message.poll.values.length) {
|
|
414
|
+
throw new Boom(`poll.selectableCount in poll should be >= 0 and <= ${message.poll.values.length}`, {
|
|
415
|
+
statusCode: 400
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
m.messageContextInfo = {
|
|
419
|
+
messageSecret: message.poll.messageSecret || randomBytes(32)
|
|
420
|
+
};
|
|
421
|
+
const pollCreationMessage = {
|
|
422
|
+
name: message.poll.name,
|
|
423
|
+
selectableOptionsCount: message.poll.selectableCount,
|
|
424
|
+
options: message.poll.values.map(optionName => ({ optionName }))
|
|
425
|
+
};
|
|
426
|
+
if (message.poll.toAnnouncementGroup) {
|
|
427
|
+
m.pollCreationMessageV2 = pollCreationMessage;
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
if (message.poll.selectableCount === 1) {
|
|
431
|
+
m.pollCreationMessageV3 = pollCreationMessage;
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
m.pollCreationMessage = pollCreationMessage;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
else if ("sharePhoneNumber" in message) {
|
|
439
|
+
m.protocolMessage = {
|
|
440
|
+
type: proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
else if ("requestPhoneNumber" in message) {
|
|
444
|
+
m.requestPhoneNumberMessage = {};
|
|
445
|
+
}
|
|
446
|
+
else if ("limitSharing" in message) {
|
|
447
|
+
m.protocolMessage = {
|
|
448
|
+
type: proto.Message.ProtocolMessage.Type.LIMIT_SHARING,
|
|
449
|
+
limitSharing: {
|
|
450
|
+
sharingLimited: message.limitSharing === true,
|
|
451
|
+
trigger: 1,
|
|
452
|
+
limitSharingSettingTimestamp: Date.now(),
|
|
453
|
+
initiatedByMe: true
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
m = await prepareWAMessageMedia(message, options);
|
|
459
|
+
}
|
|
460
|
+
if ("viewOnce" in message && !!message.viewOnce) {
|
|
461
|
+
m = { viewOnceMessage: { message: m } };
|
|
462
|
+
}
|
|
463
|
+
if ("mentions" in message && message.mentions?.length) {
|
|
464
|
+
const messageType = Object.keys(m)[0];
|
|
465
|
+
const key = m[messageType];
|
|
466
|
+
if ("contextInfo" in key && !!key.contextInfo) {
|
|
467
|
+
key.contextInfo.mentionedJid = message.mentions;
|
|
468
|
+
}
|
|
469
|
+
else if (key) {
|
|
470
|
+
key.contextInfo = {
|
|
471
|
+
mentionedJid: message.mentions
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if ("edit" in message) {
|
|
476
|
+
m = {
|
|
477
|
+
protocolMessage: {
|
|
478
|
+
key: message.edit,
|
|
479
|
+
editedMessage: m,
|
|
480
|
+
timestampMs: Date.now(),
|
|
481
|
+
type: WAProto.Message.ProtocolMessage.Type.MESSAGE_EDIT
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
if ("contextInfo" in message && !!message.contextInfo) {
|
|
486
|
+
const messageType = Object.keys(m)[0];
|
|
487
|
+
const key = m[messageType];
|
|
488
|
+
if ("contextInfo" in key && !!key.contextInfo) {
|
|
489
|
+
key.contextInfo = { ...key.contextInfo, ...message.contextInfo };
|
|
490
|
+
}
|
|
491
|
+
else if (key) {
|
|
492
|
+
key.contextInfo = message.contextInfo;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return WAProto.Message.fromObject(m);
|
|
496
|
+
};
|
|
497
|
+
//=======================================================//
|
|
498
|
+
export const generateWAMessageFromContent = (jid, message, options) => {
|
|
499
|
+
if (!options.timestamp) {
|
|
500
|
+
options.timestamp = new Date();
|
|
501
|
+
}
|
|
502
|
+
const innerMessage = normalizeMessageContent(message);
|
|
503
|
+
const key = getContentType(innerMessage);
|
|
504
|
+
const timestamp = unixTimestampSeconds(options.timestamp);
|
|
505
|
+
const { quoted, userJid } = options;
|
|
506
|
+
if (quoted && !isJidNewsletter(jid)) {
|
|
507
|
+
const participant = quoted.key.fromMe
|
|
508
|
+
? userJid
|
|
509
|
+
: quoted.participant || quoted.key.participant || quoted.key.remoteJid;
|
|
510
|
+
let quotedMsg = normalizeMessageContent(quoted.message);
|
|
511
|
+
const msgType = getContentType(quotedMsg);
|
|
512
|
+
quotedMsg = proto.Message.fromObject({ [msgType]: quotedMsg[msgType] });
|
|
513
|
+
const quotedContent = quotedMsg[msgType];
|
|
514
|
+
if (typeof quotedContent === "object" && quotedContent && "contextInfo" in quotedContent) {
|
|
515
|
+
delete quotedContent.contextInfo;
|
|
516
|
+
}
|
|
517
|
+
const contextInfo = ("contextInfo" in innerMessage[key] && innerMessage[key]?.contextInfo) || {};
|
|
518
|
+
contextInfo.participant = jidNormalizedUser(participant);
|
|
519
|
+
contextInfo.stanzaId = quoted.key.id;
|
|
520
|
+
contextInfo.quotedMessage = quotedMsg;
|
|
521
|
+
if (jid !== quoted.key.remoteJid) {
|
|
522
|
+
contextInfo.remoteJid = quoted.key.remoteJid;
|
|
523
|
+
}
|
|
524
|
+
if (contextInfo && innerMessage[key]) {
|
|
525
|
+
innerMessage[key].contextInfo = contextInfo;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
if (
|
|
529
|
+
!!options?.ephemeralExpiration &&
|
|
530
|
+
key !== "protocolMessage" &&
|
|
531
|
+
key !== "ephemeralMessage" &&
|
|
532
|
+
!isJidNewsletter(jid)) {
|
|
533
|
+
innerMessage[key].contextInfo = {
|
|
534
|
+
...(innerMessage[key].contextInfo || {}),
|
|
535
|
+
expiration: options.ephemeralExpiration || WA_DEFAULT_EPHEMERAL
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
message = WAProto.Message.fromObject(message);
|
|
539
|
+
const messageJSON = {
|
|
540
|
+
key: {
|
|
541
|
+
remoteJid: jid,
|
|
542
|
+
fromMe: true,
|
|
543
|
+
id: options?.messageId || generateMessageIDV2()
|
|
544
|
+
},
|
|
545
|
+
message: message,
|
|
546
|
+
messageTimestamp: timestamp,
|
|
547
|
+
messageStubParameters: [],
|
|
548
|
+
participant: isJidGroup(jid) || isJidStatusBroadcast(jid) ? userJid : undefined,
|
|
549
|
+
status: WAMessageStatus.PENDING
|
|
550
|
+
};
|
|
551
|
+
return WAProto.WebMessageInfo.fromObject(messageJSON);
|
|
552
|
+
};
|
|
553
|
+
//=======================================================//
|
|
554
|
+
export const generateWAMessage = async (jid, content, options) => {
|
|
555
|
+
options.logger = options?.logger?.child({ msgId: options.messageId });
|
|
556
|
+
return generateWAMessageFromContent(jid, await generateWAMessageContent(content, { ...options, jid }), options);
|
|
557
|
+
};
|
|
558
|
+
//=======================================================//
|
|
559
|
+
export const getContentType = (content) => {
|
|
560
|
+
if (content) {
|
|
561
|
+
const keys = Object.keys(content);
|
|
562
|
+
const key = keys.find(k => (k === "conversation" || k.includes("Message")) && k !== "senderKeyDistributionMessage");
|
|
563
|
+
return key;
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
//=======================================================//
|
|
567
|
+
export const normalizeMessageContent = (content) => {
|
|
568
|
+
if (!content) {
|
|
569
|
+
return undefined;
|
|
570
|
+
}
|
|
571
|
+
for (let i = 0; i < 5; i++) {
|
|
572
|
+
const inner = getFutureProofMessage(content);
|
|
573
|
+
if (!inner) {
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
576
|
+
content = inner.message;
|
|
577
|
+
}
|
|
578
|
+
return content;
|
|
579
|
+
function getFutureProofMessage(message) {
|
|
580
|
+
return (message?.ephemeralMessage ||
|
|
581
|
+
message?.viewOnceMessage ||
|
|
582
|
+
message?.documentWithCaptionMessage ||
|
|
583
|
+
message?.viewOnceMessageV2 ||
|
|
584
|
+
message?.viewOnceMessageV2Extension ||
|
|
585
|
+
message?.editedMessage);
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
//=======================================================//
|
|
589
|
+
export const extractMessageContent = (content) => {
|
|
590
|
+
const extractFromTemplateMessage = (msg) => {
|
|
591
|
+
if (msg.imageMessage) {
|
|
592
|
+
return { imageMessage: msg.imageMessage };
|
|
593
|
+
}
|
|
594
|
+
else if (msg.documentMessage) {
|
|
595
|
+
return { documentMessage: msg.documentMessage };
|
|
596
|
+
}
|
|
597
|
+
else if (msg.videoMessage) {
|
|
598
|
+
return { videoMessage: msg.videoMessage };
|
|
599
|
+
}
|
|
600
|
+
else if (msg.locationMessage) {
|
|
601
|
+
return { locationMessage: msg.locationMessage };
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
return {
|
|
605
|
+
conversation: "contentText" in msg ? msg.contentText : "hydratedContentText" in msg ? msg.hydratedContentText : ""
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
};
|
|
609
|
+
content = normalizeMessageContent(content);
|
|
610
|
+
if (content?.buttonsMessage) {
|
|
611
|
+
return extractFromTemplateMessage(content.buttonsMessage);
|
|
612
|
+
}
|
|
613
|
+
if (content?.templateMessage?.hydratedFourRowTemplate) {
|
|
614
|
+
return extractFromTemplateMessage(content?.templateMessage?.hydratedFourRowTemplate);
|
|
615
|
+
}
|
|
616
|
+
if (content?.templateMessage?.hydratedTemplate) {
|
|
617
|
+
return extractFromTemplateMessage(content?.templateMessage?.hydratedTemplate);
|
|
618
|
+
}
|
|
619
|
+
if (content?.templateMessage?.fourRowTemplate) {
|
|
620
|
+
return extractFromTemplateMessage(content?.templateMessage?.fourRowTemplate);
|
|
621
|
+
}
|
|
622
|
+
return content;
|
|
623
|
+
};
|
|
624
|
+
//=======================================================//
|
|
625
|
+
export const getDevice = (id) => /^3A.{18}$/.test(id)
|
|
626
|
+
? "ios"
|
|
627
|
+
: /^3E.{20}$/.test(id)
|
|
628
|
+
? "web"
|
|
629
|
+
: /^(.{21}|.{32})$/.test(id)
|
|
630
|
+
? "android"
|
|
631
|
+
: /^(3F|.{18}$)/.test(id)
|
|
632
|
+
? "desktop"
|
|
633
|
+
: "unknown";
|
|
634
|
+
//=======================================================//
|
|
635
|
+
export const updateMessageWithReceipt = (msg, receipt) => {
|
|
636
|
+
msg.userReceipt = msg.userReceipt || [];
|
|
637
|
+
const recp = msg.userReceipt.find(m => m.userJid === receipt.userJid);
|
|
638
|
+
if (recp) {
|
|
639
|
+
Object.assign(recp, receipt);
|
|
640
|
+
}
|
|
641
|
+
else {
|
|
642
|
+
msg.userReceipt.push(receipt);
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
//=======================================================//
|
|
646
|
+
export const updateMessageWithReaction = (msg, reaction) => {
|
|
647
|
+
const authorID = getKeyAuthor(reaction.key);
|
|
648
|
+
const reactions = (msg.reactions || []).filter(r => getKeyAuthor(r.key) !== authorID);
|
|
649
|
+
reaction.text = reaction.text || "";
|
|
650
|
+
reactions.push(reaction);
|
|
651
|
+
msg.reactions = reactions;
|
|
652
|
+
};
|
|
653
|
+
//=======================================================//
|
|
654
|
+
export const updateMessageWithPollUpdate = (msg, update) => {
|
|
655
|
+
const authorID = getKeyAuthor(update.pollUpdateMessageKey);
|
|
656
|
+
const reactions = (msg.pollUpdates || []).filter(r => getKeyAuthor(r.pollUpdateMessageKey) !== authorID);
|
|
657
|
+
if (update.vote?.selectedOptions?.length) {
|
|
658
|
+
reactions.push(update);
|
|
659
|
+
}
|
|
660
|
+
msg.pollUpdates = reactions;
|
|
661
|
+
};
|
|
662
|
+
//=======================================================//
|
|
663
|
+
export function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
|
|
664
|
+
const opts = message?.pollCreationMessage?.options ||
|
|
665
|
+
message?.pollCreationMessageV2?.options ||
|
|
666
|
+
message?.pollCreationMessageV3?.options ||
|
|
667
|
+
[];
|
|
668
|
+
const voteHashMap = opts.reduce((acc, opt) => {
|
|
669
|
+
const hash = sha256(Buffer.from(opt.optionName || "")).toString();
|
|
670
|
+
acc[hash] = {
|
|
671
|
+
name: opt.optionName || "",
|
|
672
|
+
voters: []
|
|
673
|
+
};
|
|
674
|
+
return acc;
|
|
675
|
+
}, {});
|
|
676
|
+
for (const update of pollUpdates || []) {
|
|
677
|
+
const { vote } = update;
|
|
678
|
+
if (!vote) {
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
for (const option of vote.selectedOptions || []) {
|
|
682
|
+
const hash = option.toString();
|
|
683
|
+
let data = voteHashMap[hash];
|
|
684
|
+
if (!data) {
|
|
685
|
+
voteHashMap[hash] = {
|
|
686
|
+
name: "Unknown",
|
|
687
|
+
voters: []
|
|
688
|
+
};
|
|
689
|
+
data = voteHashMap[hash];
|
|
690
|
+
}
|
|
691
|
+
voteHashMap[hash].voters.push(getKeyAuthor(update.pollUpdateMessageKey, meId));
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
return Object.values(voteHashMap);
|
|
695
|
+
}
|
|
696
|
+
//=======================================================//
|
|
697
|
+
export const aggregateMessageKeysNotFromMe = (keys) => {
|
|
698
|
+
const keyMap = {};
|
|
699
|
+
for (const { remoteJid, id, participant, fromMe } of keys) {
|
|
700
|
+
if (!fromMe) {
|
|
701
|
+
const uqKey = `${remoteJid}:${participant || ""}`;
|
|
702
|
+
if (!keyMap[uqKey]) {
|
|
703
|
+
keyMap[uqKey] = {
|
|
704
|
+
jid: remoteJid,
|
|
705
|
+
participant: participant,
|
|
706
|
+
messageIds: []
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
keyMap[uqKey].messageIds.push(id);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
return Object.values(keyMap);
|
|
713
|
+
};
|
|
714
|
+
const REUPLOAD_REQUIRED_STATUS = [410, 404];
|
|
715
|
+
//=======================================================//
|
|
716
|
+
export const downloadMediaMessage = async (message, type, options, ctx) => {
|
|
717
|
+
const result = await downloadMsg().catch(async (error) => {
|
|
718
|
+
if (ctx &&
|
|
719
|
+
typeof error?.status === "number" &&
|
|
720
|
+
REUPLOAD_REQUIRED_STATUS.includes(error.status)) {
|
|
721
|
+
ctx.logger.info({ key: message.key }, "sending reupload media request...");
|
|
722
|
+
// request reupload
|
|
723
|
+
message = await ctx.reuploadRequest(message);
|
|
724
|
+
const result = await downloadMsg();
|
|
725
|
+
return result;
|
|
726
|
+
}
|
|
727
|
+
throw error;
|
|
728
|
+
});
|
|
729
|
+
return result;
|
|
730
|
+
async function downloadMsg() {
|
|
731
|
+
const mContent = extractMessageContent(message.message);
|
|
732
|
+
if (!mContent) {
|
|
733
|
+
throw new Boom("No message present", { statusCode: 400, data: message });
|
|
734
|
+
}
|
|
735
|
+
const contentType = getContentType(mContent);
|
|
736
|
+
let mediaType = contentType?.replace("Message", "");
|
|
737
|
+
const media = mContent[contentType];
|
|
738
|
+
if (!media || typeof media !== "object" || (!("url" in media) && !("thumbnailDirectPath" in media))) {
|
|
739
|
+
throw new Boom(`"${contentType}" message is not a media message`);
|
|
740
|
+
}
|
|
741
|
+
let download;
|
|
742
|
+
if ("thumbnailDirectPath" in media && !("url" in media)) {
|
|
743
|
+
download = {
|
|
744
|
+
directPath: media.thumbnailDirectPath,
|
|
745
|
+
mediaKey: media.mediaKey
|
|
746
|
+
};
|
|
747
|
+
mediaType = "thumbnail-link";
|
|
748
|
+
}
|
|
749
|
+
else {
|
|
750
|
+
download = media;
|
|
751
|
+
}
|
|
752
|
+
const stream = await downloadContentFromMessage(download, mediaType, options);
|
|
753
|
+
if (type === "buffer") {
|
|
754
|
+
const bufferArray = [];
|
|
755
|
+
for await (const chunk of stream) {
|
|
756
|
+
bufferArray.push(chunk);
|
|
757
|
+
}
|
|
758
|
+
return Buffer.concat(bufferArray);
|
|
759
|
+
}
|
|
760
|
+
return stream;
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
//=======================================================//
|
|
764
|
+
export const assertMediaContent = (content) => {
|
|
765
|
+
content = extractMessageContent(content);
|
|
766
|
+
const mediaContent = content?.documentMessage ||
|
|
767
|
+
content?.imageMessage ||
|
|
768
|
+
content?.videoMessage ||
|
|
769
|
+
content?.audioMessage ||
|
|
770
|
+
content?.stickerMessage;
|
|
771
|
+
if (!mediaContent) {
|
|
772
|
+
throw new Boom("given message is not a media message", { statusCode: 400, data: content });
|
|
773
|
+
}
|
|
774
|
+
return mediaContent;
|
|
775
|
+
};
|
|
776
|
+
//=======================================================//
|