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