@hansaka02/baileys 7.3.2 → 7.3.4
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/README.md +247 -203
- package/lib/Defaults/connection.js +51 -0
- package/lib/Defaults/constants.js +62 -0
- package/lib/Defaults/history.js +17 -0
- package/lib/Defaults/index.js +36 -142
- package/lib/Defaults/media.js +48 -0
- package/lib/Defaults/prefix.js +18 -0
- package/lib/Signal/Group/group-session-builder.js +10 -42
- package/lib/Signal/Group/group_cipher.js +9 -6
- package/lib/Signal/Group/index.js +39 -53
- package/lib/Signal/Group/keyhelper.js +8 -41
- package/lib/Signal/Group/sender-chain-key.js +4 -4
- package/lib/Signal/Group/sender-key-distribution-message.js +5 -5
- package/lib/Signal/Group/sender-key-message.js +12 -8
- package/lib/Signal/Group/sender-key-record.js +7 -7
- package/lib/Signal/Group/sender-key-state.js +4 -4
- package/lib/Signal/Group/sender-message-key.js +2 -2
- package/lib/Signal/libsignal.js +45 -69
- package/lib/Signal/lid-mapping.js +15 -11
- package/lib/Socket/Client/types.js +2 -2
- package/lib/Socket/Client/websocket.js +16 -14
- package/lib/Socket/business.js +41 -32
- package/lib/Socket/chats.js +123 -98
- package/lib/Socket/community.js +50 -40
- package/lib/Socket/groups.js +59 -47
- package/lib/Socket/index.js +4 -4
- package/lib/Socket/messages-recv.js +219 -172
- package/lib/Socket/messages-send.js +187 -143
- package/lib/Socket/newsletter.js +61 -47
- package/lib/Socket/socket.js +133 -90
- package/lib/Socket/usync.js +6 -6
- package/lib/Store/index.js +27 -11
- package/lib/Store/make-cache-manager-store.js +14 -15
- package/lib/Store/make-in-memory-store.js +28 -24
- package/lib/Types/LabelAssociation.js +2 -2
- package/lib/Types/Message.js +6 -6
- package/lib/Types/MexUpdates.js +5 -5
- package/lib/Types/State.js +4 -4
- package/lib/Types/index.js +28 -12
- package/lib/Utils/auth-utils.js +28 -26
- package/lib/Utils/baileys-event-stream.js +68 -69
- package/lib/Utils/business.js +63 -53
- package/lib/Utils/chat-utils.js +81 -71
- package/lib/Utils/crypto.js +25 -45
- package/lib/Utils/decode-wa-message.js +319 -311
- package/lib/Utils/event-buffer.js +21 -22
- package/lib/Utils/generics.js +65 -82
- package/lib/Utils/history.js +21 -21
- package/lib/Utils/index.js +27 -13
- package/lib/Utils/link-preview.js +7 -30
- package/lib/Utils/logger.js +5 -5
- package/lib/Utils/lt-hash.js +3 -3
- package/lib/Utils/message-retry-manager.js +4 -4
- package/lib/Utils/messages-media.js +104 -109
- package/lib/Utils/messages.js +203 -171
- package/lib/Utils/noise-handler.js +28 -19
- package/lib/Utils/process-message.js +111 -96
- package/lib/Utils/signal.js +36 -25
- package/lib/Utils/use-multi-file-auth-state.js +18 -22
- package/lib/Utils/validate-connection.js +52 -45
- package/lib/WABinary/decode.js +6 -32
- package/lib/WABinary/encode.js +3 -29
- package/lib/WABinary/generic-utils.js +4 -4
- package/lib/WABinary/index.js +27 -11
- package/lib/WAM/encode.js +16 -8
- package/lib/WAM/index.js +27 -11
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +20 -16
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +2 -2
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +7 -4
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +2 -2
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -2
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +2 -2
- package/lib/WAUSync/Protocols/index.js +27 -11
- package/lib/WAUSync/USyncQuery.js +17 -10
- package/lib/WAUSync/index.js +27 -11
- package/lib/index.js +60 -31
- package/package.json +8 -14
- package/WAProto/AICommon/AICommon.d.ts +0 -11702
- package/WAProto/Adv/Adv.d.ts +0 -643
- package/WAProto/BotMetadata/BotMetadata.d.ts +0 -5654
- package/WAProto/Cert/Cert.d.ts +0 -613
- package/WAProto/ChatLockSettings/ChatLockSettings.d.ts +0 -476
- package/WAProto/CompanionReg/CompanionReg.d.ts +0 -1361
- package/WAProto/DeviceCapabilities/DeviceCapabilities.d.ts +0 -577
- package/WAProto/E2E/E2E.d.ts +0 -41724
- package/WAProto/Ephemeral/Ephemeral.d.ts +0 -114
- package/WAProto/HistorySync/HistorySync.d.ts +0 -51700
- package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.d.ts +0 -229
- package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.d.ts +0 -583
- package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.d.ts +0 -42897
- package/WAProto/MmsRetry/MmsRetry.d.ts +0 -243
- package/WAProto/Protocol/Protocol.d.ts +0 -270
- package/WAProto/Reporting/Reporting.d.ts +0 -371
- package/WAProto/ServerSync/ServerSync.d.ts +0 -1285
- package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.d.ts +0 -1868
- package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.d.ts +0 -767
- package/WAProto/StatusAttributions/StatusAttributions.d.ts +0 -1027
- package/WAProto/SyncAction/SyncAction.d.ts +0 -11193
- package/WAProto/UserPassword/UserPassword.d.ts +0 -363
- package/WAProto/VnameCert/VnameCert.d.ts +0 -821
- package/WAProto/Wa6/Wa6.d.ts +0 -2128
- package/WAProto/Web/Web.d.ts +0 -46383
- package/WAProto/index.d.ts +0 -55
- package/lib/Defaults/index.d.ts +0 -77
- package/lib/Signal/Group/ciphertext-message.d.ts +0 -9
- package/lib/Signal/Group/group-session-builder.d.ts +0 -17
- package/lib/Signal/Group/group_cipher.d.ts +0 -19
- package/lib/Signal/Group/index.d.ts +0 -11
- package/lib/Signal/Group/keyhelper.d.ts +0 -16
- package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
- package/lib/Signal/Group/sender-key-message.d.ts +0 -19
- package/lib/Signal/Group/sender-key-name.d.ts +0 -19
- package/lib/Signal/Group/sender-key-record.d.ts +0 -32
- package/lib/Signal/Group/sender-key-state.d.ts +0 -44
- package/lib/Signal/Group/sender-message-key.d.ts +0 -11
- package/lib/Signal/libsignal.d.ts +0 -8
- package/lib/Signal/lid-mapping.d.ts +0 -28
- package/lib/Socket/Client/index.d.ts +0 -2
- package/lib/Socket/Client/types.d.ts +0 -16
- package/lib/Socket/Client/websocket.d.ts +0 -13
- package/lib/Socket/business.d.ts +0 -187
- package/lib/Socket/chats.d.ts +0 -97
- package/lib/Socket/community.d.ts +0 -129
- package/lib/Socket/groups.d.ts +0 -129
- package/lib/Socket/index.d.ts +0 -191
- package/lib/Socket/messages-recv.d.ts +0 -174
- package/lib/Socket/messages-send.d.ts +0 -165
- package/lib/Socket/newsletter.d.ts +0 -145
- package/lib/Socket/socket.d.ts +0 -45
- package/lib/Socket/usync.d.ts +0 -37
- package/lib/Store/index.d.ts +0 -4
- package/lib/Store/make-cache-manager-store.d.ts +0 -14
- package/lib/Store/make-in-memory-store.d.ts +0 -123
- package/lib/Store/make-ordered-dictionary.d.ts +0 -12
- package/lib/Store/object-repository.d.ts +0 -10
- package/lib/Types/Auth.d.ts +0 -121
- package/lib/Types/Bussiness.d.ts +0 -28
- package/lib/Types/Call.d.ts +0 -14
- package/lib/Types/Chat.d.ts +0 -143
- package/lib/Types/Contact.d.ts +0 -23
- package/lib/Types/Events.d.ts +0 -226
- package/lib/Types/GroupMetadata.d.ts +0 -66
- package/lib/Types/Label.d.ts +0 -48
- package/lib/Types/LabelAssociation.d.ts +0 -35
- package/lib/Types/Message.d.ts +0 -484
- package/lib/Types/MexUpdates.d.ts +0 -9
- package/lib/Types/Newsletter.d.ts +0 -109
- package/lib/Types/Product.d.ts +0 -92
- package/lib/Types/Signal.d.ts +0 -98
- package/lib/Types/Socket.d.ts +0 -141
- package/lib/Types/State.d.ts +0 -41
- package/lib/Types/USync.d.ts +0 -26
- package/lib/Types/index.d.ts +0 -80
- package/lib/Utils/auth-utils.d.ts +0 -21
- package/lib/Utils/baileys-event-stream.d.ts +0 -18
- package/lib/Utils/business.d.ts +0 -29
- package/lib/Utils/chat-utils.d.ts +0 -82
- package/lib/Utils/crypto.d.ts +0 -56
- package/lib/Utils/decode-wa-message.d.ts +0 -53
- package/lib/Utils/event-buffer.d.ts +0 -39
- package/lib/Utils/generics.d.ts +0 -117
- package/lib/Utils/history.d.ts +0 -23
- package/lib/Utils/index.d.ts +0 -20
- package/lib/Utils/link-preview.d.ts +0 -23
- package/lib/Utils/logger.d.ts +0 -13
- package/lib/Utils/lt-hash.d.ts +0 -14
- package/lib/Utils/make-mutex.d.ts +0 -9
- package/lib/Utils/message-retry-manager.d.ts +0 -88
- package/lib/Utils/messages-media.d.ts +0 -135
- package/lib/Utils/messages.d.ts +0 -105
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/process-message.d.ts +0 -49
- package/lib/Utils/signal.d.ts +0 -42
- package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
- package/lib/Utils/use-mongo-file-auth-state.js +0 -84
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
- package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
- package/lib/Utils/use-single-file-auth-state.js +0 -80
- package/lib/Utils/validate-connection.d.ts +0 -13
- package/lib/WABinary/constants.d.ts +0 -30
- package/lib/WABinary/decode.d.ts +0 -9
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -28
- package/lib/WABinary/index.d.ts +0 -5
- package/lib/WABinary/jid-utils.d.ts +0 -58
- package/lib/WABinary/types.d.ts +0 -22
- package/lib/WAM/BinaryInfo.d.ts +0 -16
- package/lib/WAM/constants.d.ts +0 -47
- package/lib/WAM/encode.d.ts +0 -3
- package/lib/WAM/index.d.ts +0 -3
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.d.ts +0 -28
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -26
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -14
- package/lib/WAUSync/Protocols/USyncLIDProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -14
- package/lib/WAUSync/Protocols/index.d.ts +0 -6
- package/lib/WAUSync/USyncQuery.d.ts +0 -31
- package/lib/WAUSync/USyncUser.d.ts +0 -12
- package/lib/WAUSync/index.d.ts +0 -3
- package/lib/index.d.ts +0 -13
package/lib/Utils/messages.js
CHANGED
|
@@ -1,22 +1,43 @@
|
|
|
1
1
|
"use strict"
|
|
2
2
|
|
|
3
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
-
return (mod && mod.__esModule) ? mod : { "default": mod }
|
|
5
|
-
}
|
|
6
|
-
|
|
7
3
|
Object.defineProperty(exports, "__esModule", { value: true })
|
|
8
4
|
|
|
9
|
-
const
|
|
10
|
-
const axios_1 =
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
const
|
|
5
|
+
const { Boom } = require("@hapi/boom")
|
|
6
|
+
const axios_1 = require("axios")
|
|
7
|
+
const { randomBytes } = require("crypto")
|
|
8
|
+
const { promises } = require("fs")
|
|
9
|
+
const { proto } = require("../../WAProto")
|
|
10
|
+
const {
|
|
11
|
+
URL_REGEX,
|
|
12
|
+
WA_DEFAULT_EPHEMERAL
|
|
13
|
+
} = require("../Defaults/constants")
|
|
14
|
+
const { MEDIA_KEYS } = require("../Defaults/media")
|
|
15
|
+
const {
|
|
16
|
+
WAProto,
|
|
17
|
+
WAMessageStatus
|
|
18
|
+
} = require("../Types")
|
|
19
|
+
const {
|
|
20
|
+
isJidGroup,
|
|
21
|
+
isJidNewsletter,
|
|
22
|
+
isJidStatusBroadcast,
|
|
23
|
+
jidNormalizedUser
|
|
24
|
+
} = require("../WABinary")
|
|
25
|
+
const { sha256 } = require("./crypto")
|
|
26
|
+
const {
|
|
27
|
+
generateMessageID,
|
|
28
|
+
getKeyAuthor,
|
|
29
|
+
unixTimestampSeconds
|
|
30
|
+
} = require("./generics")
|
|
31
|
+
const {
|
|
32
|
+
downloadContentFromMessage,
|
|
33
|
+
encryptedStream,
|
|
34
|
+
generateThumbnail,
|
|
35
|
+
getAudioDuration,
|
|
36
|
+
getAudioWaveform,
|
|
37
|
+
getRawMediaUploadData,
|
|
38
|
+
getStream,
|
|
39
|
+
toBuffer
|
|
40
|
+
} = require("./messages-media")
|
|
20
41
|
|
|
21
42
|
const MIMETYPE_MAP = {
|
|
22
43
|
image: 'image/jpeg',
|
|
@@ -28,11 +49,11 @@ const MIMETYPE_MAP = {
|
|
|
28
49
|
}
|
|
29
50
|
|
|
30
51
|
const MessageTypeProto = {
|
|
31
|
-
'image':
|
|
32
|
-
'video':
|
|
33
|
-
'audio':
|
|
34
|
-
'sticker':
|
|
35
|
-
'document':
|
|
52
|
+
'image': WAProto.Message.ImageMessage,
|
|
53
|
+
'video': WAProto.Message.VideoMessage,
|
|
54
|
+
'audio': WAProto.Message.AudioMessage,
|
|
55
|
+
'sticker': WAProto.Message.StickerMessage,
|
|
56
|
+
'document': WAProto.Message.DocumentMessage
|
|
36
57
|
}
|
|
37
58
|
|
|
38
59
|
/**
|
|
@@ -40,7 +61,7 @@ const MessageTypeProto = {
|
|
|
40
61
|
* @param text eg. hello https://google.com
|
|
41
62
|
* @returns the URL, eg. https://google.com
|
|
42
63
|
*/
|
|
43
|
-
const extractUrlFromText = (text) => text.match(
|
|
64
|
+
const extractUrlFromText = (text) => text.match(URL_REGEX)?.[0]
|
|
44
65
|
|
|
45
66
|
const generateLinkPreviewIfRequired = async (text, getUrlInfo, logger) => {
|
|
46
67
|
const url = extractUrlFromText(text)
|
|
@@ -78,14 +99,14 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
78
99
|
const logger = options.logger
|
|
79
100
|
let mediaType
|
|
80
101
|
|
|
81
|
-
for (const key of
|
|
102
|
+
for (const key of MEDIA_KEYS) {
|
|
82
103
|
if (key in message) {
|
|
83
104
|
mediaType = key
|
|
84
105
|
}
|
|
85
106
|
}
|
|
86
107
|
|
|
87
108
|
if (!mediaType) {
|
|
88
|
-
throw new
|
|
109
|
+
throw new Boom('Invalid media type', { statusCode: 400 });
|
|
89
110
|
}
|
|
90
111
|
|
|
91
112
|
const uploadData = {
|
|
@@ -114,18 +135,18 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
114
135
|
const mediaBuff = await options.mediaCache.get(cacheableKey)
|
|
115
136
|
if (mediaBuff) {
|
|
116
137
|
logger?.debug({ cacheableKey }, 'got media cache hit')
|
|
117
|
-
const obj =
|
|
138
|
+
const obj = WAProto.Message.decode(mediaBuff)
|
|
118
139
|
const key = `${mediaType}Message`
|
|
119
140
|
Object.assign(obj[key], { ...uploadData, media: undefined })
|
|
120
141
|
return obj
|
|
121
142
|
}
|
|
122
143
|
}
|
|
123
144
|
|
|
124
|
-
const isNewsletter = !!options.jid &&
|
|
145
|
+
const isNewsletter = !!options.jid && isJidNewsletter(options.jid)
|
|
125
146
|
|
|
126
147
|
if (isNewsletter) {
|
|
127
148
|
logger?.info({ key: cacheableKey }, 'Preparing raw media for newsletter')
|
|
128
|
-
const { filePath, fileSha256, fileLength } = await
|
|
149
|
+
const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(uploadData.media, options.mediaTypeOverride || mediaType, logger)
|
|
129
150
|
const fileSha256B64 = fileSha256.toString('base64')
|
|
130
151
|
const { mediaUrl, directPath } = await options.upload(filePath, {
|
|
131
152
|
fileEncSha256B64: fileSha256B64,
|
|
@@ -133,9 +154,9 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
133
154
|
timeoutMs: options.mediaUploadTimeoutMs
|
|
134
155
|
})
|
|
135
156
|
|
|
136
|
-
await
|
|
157
|
+
await promises.unlink(filePath)
|
|
137
158
|
|
|
138
|
-
const obj =
|
|
159
|
+
const obj = WAProto.Message.fromObject({
|
|
139
160
|
// todo: add more support here
|
|
140
161
|
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
141
162
|
url: mediaUrl,
|
|
@@ -154,7 +175,7 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
154
175
|
|
|
155
176
|
if (cacheableKey) {
|
|
156
177
|
logger?.debug({ cacheableKey }, 'set cache');
|
|
157
|
-
await options.mediaCache.set(cacheableKey,
|
|
178
|
+
await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish())
|
|
158
179
|
}
|
|
159
180
|
|
|
160
181
|
return obj
|
|
@@ -166,7 +187,7 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
166
187
|
const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === true
|
|
167
188
|
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation
|
|
168
189
|
|
|
169
|
-
const { mediaKey, encFilePath, originalFilePath, fileEncSha256, fileSha256, fileLength } = await
|
|
190
|
+
const { mediaKey, encFilePath, originalFilePath, fileEncSha256, fileSha256, fileLength } = await encryptedStream(uploadData.media, options.mediaTypeOverride || mediaType, {
|
|
170
191
|
logger,
|
|
171
192
|
saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
|
|
172
193
|
opts: options.options
|
|
@@ -188,7 +209,7 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
188
209
|
(async () => {
|
|
189
210
|
try {
|
|
190
211
|
if (requiresThumbnailComputation) {
|
|
191
|
-
const { thumbnail, originalImageDimensions } = await
|
|
212
|
+
const { thumbnail, originalImageDimensions } = await generateThumbnail(originalFilePath, mediaType, options)
|
|
192
213
|
uploadData.jpegThumbnail = thumbnail
|
|
193
214
|
if (!uploadData.width && originalImageDimensions) {
|
|
194
215
|
uploadData.width = originalImageDimensions.width
|
|
@@ -201,12 +222,12 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
201
222
|
}
|
|
202
223
|
|
|
203
224
|
if (requiresDurationComputation) {
|
|
204
|
-
uploadData.seconds = await
|
|
225
|
+
uploadData.seconds = await getAudioDuration(originalFilePath)
|
|
205
226
|
logger?.debug('computed audio duration')
|
|
206
227
|
}
|
|
207
228
|
|
|
208
229
|
if (requiresWaveformProcessing) {
|
|
209
|
-
uploadData.waveform = await
|
|
230
|
+
uploadData.waveform = await getAudioWaveform(originalFilePath, logger)
|
|
210
231
|
logger?.debug('processed waveform')
|
|
211
232
|
}
|
|
212
233
|
|
|
@@ -221,10 +242,10 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
221
242
|
})()
|
|
222
243
|
]).finally(async () => {
|
|
223
244
|
try {
|
|
224
|
-
await
|
|
245
|
+
await promises.unlink(encFilePath)
|
|
225
246
|
|
|
226
247
|
if (originalFilePath) {
|
|
227
|
-
await
|
|
248
|
+
await promises.unlink(originalFilePath)
|
|
228
249
|
}
|
|
229
250
|
|
|
230
251
|
logger?.debug('removed tmp files')
|
|
@@ -234,7 +255,7 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
234
255
|
}
|
|
235
256
|
})
|
|
236
257
|
|
|
237
|
-
const obj =
|
|
258
|
+
const obj = WAProto.Message.fromObject({
|
|
238
259
|
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
239
260
|
url: mediaUrl,
|
|
240
261
|
directPath,
|
|
@@ -242,7 +263,7 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
242
263
|
fileEncSha256,
|
|
243
264
|
fileSha256,
|
|
244
265
|
fileLength,
|
|
245
|
-
mediaKeyTimestamp:
|
|
266
|
+
mediaKeyTimestamp: unixTimestampSeconds(),
|
|
246
267
|
...uploadData,
|
|
247
268
|
media: undefined
|
|
248
269
|
})
|
|
@@ -255,75 +276,86 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
255
276
|
|
|
256
277
|
if (cacheableKey) {
|
|
257
278
|
logger?.debug({ cacheableKey }, 'set cache');
|
|
258
|
-
await options.mediaCache.set(cacheableKey,
|
|
279
|
+
await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish())
|
|
259
280
|
}
|
|
260
281
|
|
|
261
282
|
return obj
|
|
262
283
|
}
|
|
263
284
|
|
|
264
285
|
const prepareAlbumMessageContent = async (jid, albums, options) => {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
const albumMsg = generateWAMessageFromContent(jid, {
|
|
270
|
-
albumMessage: {
|
|
271
|
-
expectedImageCount: albums.filter(item => 'image' in item).length,
|
|
272
|
-
expectedVideoCount: albums.filter(item => 'video' in item).length
|
|
273
|
-
}
|
|
274
|
-
}, options)
|
|
275
|
-
|
|
276
|
-
await options.suki.relayMessage(jid, albumMsg.message, {
|
|
277
|
-
messageId: albumMsg.key.id
|
|
278
|
-
})
|
|
279
|
-
|
|
280
|
-
for (const i in albums) {
|
|
281
|
-
const media = albums[i]
|
|
282
|
-
|
|
283
|
-
if ('image' in media) {
|
|
284
|
-
mediaMsg = await generateWAMessage(jid, {
|
|
285
|
-
image: media.image,
|
|
286
|
-
...media,
|
|
287
|
-
...options
|
|
288
|
-
}, {
|
|
289
|
-
userJid: options.userJid,
|
|
290
|
-
upload: async (encFilePath, opts) => {
|
|
291
|
-
const up = await options.suki.waUploadToServer(encFilePath, { ...opts, newsletter: WABinary_1.isJidNewsletter(jid) })
|
|
292
|
-
mediaHandle = up.handle
|
|
293
|
-
return up
|
|
294
|
-
},
|
|
295
|
-
...options
|
|
296
|
-
})
|
|
297
|
-
} else if ('video' in message) {
|
|
298
|
-
mediaMsg = await generateWAMessage(jid, {
|
|
299
|
-
video: media.video,
|
|
300
|
-
...media,
|
|
301
|
-
...options
|
|
302
|
-
}, {
|
|
303
|
-
userJid: options.userJid,
|
|
304
|
-
upload: async (encFilePath, opts) => {
|
|
305
|
-
const up = await options.suki.waUploadToServer(encFilePath, { ...opts, newsletter: WABinary_1.isJidNewsletter(jid) })
|
|
306
|
-
mediaHandle = up.handle
|
|
307
|
-
return up
|
|
308
|
-
},
|
|
309
|
-
...options
|
|
310
|
-
})
|
|
311
|
-
}
|
|
286
|
+
if (!Array.isArray(albums)) {
|
|
287
|
+
throw new Error("albums must be an array containing media objects.")
|
|
288
|
+
}
|
|
312
289
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
290
|
+
if (albums.length === 0) {
|
|
291
|
+
throw new Error("albums cannot be empty. At least one media item is required.")
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const validCount = albums.filter(m => ('image' in m) || ('video' in m)).length
|
|
295
|
+
|
|
296
|
+
if (validCount === 0) {
|
|
297
|
+
throw new Error("albums contains no valid media. Use 'image' or 'video' keys.")
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
let mediaHandle
|
|
301
|
+
let mediaMsg
|
|
302
|
+
const message = []
|
|
303
|
+
|
|
304
|
+
const albumMsg = generateWAMessageFromContent(jid, {
|
|
305
|
+
albumMessage: {
|
|
306
|
+
expectedImageCount: albums.filter(item => 'image' in item).length,
|
|
307
|
+
expectedVideoCount: albums.filter(item => 'video' in item).length
|
|
308
|
+
}
|
|
309
|
+
}, options)
|
|
322
310
|
|
|
323
|
-
|
|
311
|
+
await options.suki.relayMessage(jid, albumMsg.message, {
|
|
312
|
+
messageId: albumMsg.key.id
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
for (const media of albums) {
|
|
316
|
+
let content = {}
|
|
317
|
+
if ('image' in media) {
|
|
318
|
+
content = { image: media.image }
|
|
319
|
+
} else if ('video' in media) {
|
|
320
|
+
content = { video: media.video }
|
|
321
|
+
} else {
|
|
322
|
+
continue
|
|
324
323
|
}
|
|
325
324
|
|
|
326
|
-
|
|
325
|
+
mediaMsg = await generateWAMessage(
|
|
326
|
+
jid,
|
|
327
|
+
{
|
|
328
|
+
...content,
|
|
329
|
+
...media
|
|
330
|
+
},
|
|
331
|
+
{
|
|
332
|
+
userJid: options.userJid,
|
|
333
|
+
upload: async (encFilePath, opts) => {
|
|
334
|
+
const up = await options.suki.waUploadToServer(
|
|
335
|
+
encFilePath,
|
|
336
|
+
{ ...opts, newsletter: isJidNewsletter(jid) }
|
|
337
|
+
)
|
|
338
|
+
mediaHandle = up.handle
|
|
339
|
+
return up
|
|
340
|
+
},
|
|
341
|
+
...options
|
|
342
|
+
}
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
if (mediaMsg) {
|
|
346
|
+
mediaMsg.message.messageContextInfo = {
|
|
347
|
+
messageSecret: randomBytes(32),
|
|
348
|
+
messageAssociation: {
|
|
349
|
+
associationType: proto.MessageAssociation.AssociationType.MEDIA_ALBUM,
|
|
350
|
+
parentMessageKey: albumMsg.key
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
message.push(mediaMsg)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return message
|
|
327
359
|
}
|
|
328
360
|
|
|
329
361
|
const prepareDisappearingMessageSettingContent = (expiration) => {
|
|
@@ -331,14 +363,14 @@ const prepareDisappearingMessageSettingContent = (expiration) => {
|
|
|
331
363
|
ephemeralMessage: {
|
|
332
364
|
message: {
|
|
333
365
|
protocolMessage: {
|
|
334
|
-
type:
|
|
366
|
+
type: WAProto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
|
|
335
367
|
ephemeralExpiration: expiration ? expiration : 0
|
|
336
368
|
}
|
|
337
369
|
}
|
|
338
370
|
}
|
|
339
371
|
}
|
|
340
372
|
|
|
341
|
-
return
|
|
373
|
+
return WAProto.Message.fromObject(content)
|
|
342
374
|
}
|
|
343
375
|
|
|
344
376
|
/**
|
|
@@ -350,12 +382,12 @@ const generateForwardMessageContent = (message, forceForward) => {
|
|
|
350
382
|
let content = message.message
|
|
351
383
|
|
|
352
384
|
if (!content) {
|
|
353
|
-
throw new
|
|
385
|
+
throw new Boom('no content in message', { statusCode: 400 })
|
|
354
386
|
}
|
|
355
387
|
|
|
356
388
|
// hacky copy
|
|
357
389
|
content = normalizeMessageContent(content)
|
|
358
|
-
content =
|
|
390
|
+
content = proto.Message.decode(proto.Message.encode(content).finish())
|
|
359
391
|
|
|
360
392
|
let key = Object.keys(content)[0]
|
|
361
393
|
let score = content[key].contextInfo?.forwardingScore || 0
|
|
@@ -435,18 +467,18 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
435
467
|
let contactMessage
|
|
436
468
|
|
|
437
469
|
if (!contactLen) {
|
|
438
|
-
throw new
|
|
470
|
+
throw new Boom('require atleast 1 contact', { statusCode: 400 })
|
|
439
471
|
}
|
|
440
472
|
|
|
441
473
|
if (contactLen === 1) {
|
|
442
474
|
contactMessage = {
|
|
443
|
-
contactMessage:
|
|
475
|
+
contactMessage: WAProto.Message.ContactMessage.fromObject(message.contacts.contacts[0])
|
|
444
476
|
}
|
|
445
477
|
}
|
|
446
478
|
|
|
447
479
|
else {
|
|
448
480
|
contactMessage = {
|
|
449
|
-
contactsArrayMessage:
|
|
481
|
+
contactsArrayMessage: WAProto.Message.ContactsArrayMessage.fromObject(message.contacts)
|
|
450
482
|
}
|
|
451
483
|
}
|
|
452
484
|
|
|
@@ -465,13 +497,13 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
465
497
|
|
|
466
498
|
if (message.live) {
|
|
467
499
|
locationMessage = {
|
|
468
|
-
liveLocationMessage:
|
|
500
|
+
liveLocationMessage: WAProto.Message.LiveLocationMessage.fromObject(message.location)
|
|
469
501
|
}
|
|
470
502
|
}
|
|
471
503
|
|
|
472
504
|
else {
|
|
473
505
|
locationMessage = {
|
|
474
|
-
locationMessage:
|
|
506
|
+
locationMessage: WAProto.Message.LocationMessage.fromObject(message.location)
|
|
475
507
|
}
|
|
476
508
|
}
|
|
477
509
|
|
|
@@ -490,13 +522,13 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
490
522
|
message.react.senderTimestampMs = Date.now()
|
|
491
523
|
}
|
|
492
524
|
|
|
493
|
-
m.reactionMessage =
|
|
525
|
+
m.reactionMessage = WAProto.Message.ReactionMessage.fromObject(message.react)
|
|
494
526
|
}
|
|
495
527
|
|
|
496
528
|
else if ('delete' in message) {
|
|
497
529
|
m.protocolMessage = {
|
|
498
530
|
key: message.delete,
|
|
499
|
-
type:
|
|
531
|
+
type: WAProto.Message.ProtocolMessage.Type.REVOKE
|
|
500
532
|
}
|
|
501
533
|
}
|
|
502
534
|
|
|
@@ -514,7 +546,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
514
546
|
|
|
515
547
|
else if ('disappearingMessagesInChat' in message) {
|
|
516
548
|
const exp = typeof message.disappearingMessagesInChat === 'boolean' ?
|
|
517
|
-
(message.disappearingMessagesInChat ?
|
|
549
|
+
(message.disappearingMessagesInChat ? WA_DEFAULT_EPHEMERAL : 0) :
|
|
518
550
|
message.disappearingMessagesInChat
|
|
519
551
|
m = prepareDisappearingMessageSettingContent(exp)
|
|
520
552
|
}
|
|
@@ -531,7 +563,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
531
563
|
|
|
532
564
|
if (options.getProfilePicUrl) {
|
|
533
565
|
const pfpUrl = await options.getProfilePicUrl(message.groupInvite.jid)
|
|
534
|
-
const { thumbnail } = await
|
|
566
|
+
const { thumbnail } = await generateThumbnail(pfpUrl, 'image')
|
|
535
567
|
m.groupInviteMessage.jpegThumbnail = thumbnail
|
|
536
568
|
}
|
|
537
569
|
|
|
@@ -552,7 +584,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
552
584
|
|
|
553
585
|
if (options.getProfilePicUrl) {
|
|
554
586
|
const pfpUrl = await options.getProfilePicUrl(message.adminInvite.jid)
|
|
555
|
-
const { thumbnail } = await
|
|
587
|
+
const { thumbnail } = await generateThumbnail(pfpUrl, 'image')
|
|
556
588
|
m.newsletterAdminInviteMessage.jpegThumbnail = thumbnail
|
|
557
589
|
}
|
|
558
590
|
|
|
@@ -570,7 +602,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
570
602
|
m.pinInChatMessage.type = message.pin?.type || 1
|
|
571
603
|
m.pinInChatMessage.senderTimestampMs = message.pin?.time || Date.now()
|
|
572
604
|
m.messageContextInfo.messageAddOnDurationInSecs = message.pin.type === 1 ? message.pin.time || 86400 : 0
|
|
573
|
-
m.messageContextInfo.messageAddOnExpiryType =
|
|
605
|
+
m.messageContextInfo.messageAddOnExpiryType = proto.MessageContextInfo.MessageAddonExpiryType.STATIC
|
|
574
606
|
}
|
|
575
607
|
|
|
576
608
|
else if ('keep' in message) {
|
|
@@ -616,7 +648,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
616
648
|
singleSelectReply: {
|
|
617
649
|
selectedRowId: message.buttonReply.rowId
|
|
618
650
|
},
|
|
619
|
-
lisType:
|
|
651
|
+
lisType: proto.Message.ListResponseMessage.ListType.SINGLE_SELECT
|
|
620
652
|
}
|
|
621
653
|
break
|
|
622
654
|
case 'template':
|
|
@@ -630,14 +662,14 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
630
662
|
m.buttonsResponseMessage = {
|
|
631
663
|
selectedButtonId: message.buttonReply.id,
|
|
632
664
|
selectedDisplayText: message.buttonReply.displayText,
|
|
633
|
-
type:
|
|
665
|
+
type: proto.Message.ButtonsResponseMessage.Type.DISPLAY_TEXT
|
|
634
666
|
}
|
|
635
667
|
break
|
|
636
668
|
case 'interactive':
|
|
637
669
|
m.interactiveResponseMessage = {
|
|
638
670
|
body: {
|
|
639
671
|
text: message.buttonReply.displayText,
|
|
640
|
-
format:
|
|
672
|
+
format: proto.Message.InteractiveResponseMessage.Body.Format.EXTENSIONS_1
|
|
641
673
|
},
|
|
642
674
|
nativeFlowResponseMessage: {
|
|
643
675
|
name: message.buttonReply.nativeFlows.name,
|
|
@@ -659,14 +691,14 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
659
691
|
const imageMessages = message.album.filter(item => 'image' in item)
|
|
660
692
|
const videoMessages = message.album.filter(item => 'video' in item)
|
|
661
693
|
|
|
662
|
-
m.albumMessage =
|
|
694
|
+
m.albumMessage = WAProto.Message.AlbumMessage.fromObject({
|
|
663
695
|
expectedImageCount: imageMessages.length,
|
|
664
696
|
expectedVideoCount: videoMessages.length
|
|
665
697
|
})
|
|
666
698
|
}
|
|
667
699
|
|
|
668
700
|
else if ('order' in message) {
|
|
669
|
-
m.orderMessage =
|
|
701
|
+
m.orderMessage = WAProto.Message.OrderMessage.fromObject(message.order)
|
|
670
702
|
|
|
671
703
|
m.orderMessage.contextInfo = {
|
|
672
704
|
...(message.contextInfo || {}),
|
|
@@ -675,10 +707,10 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
675
707
|
}
|
|
676
708
|
|
|
677
709
|
else if ('event' in message) {
|
|
678
|
-
m.eventMessage =
|
|
710
|
+
m.eventMessage = WAProto.Message.EventMessage.fromObject(message.event)
|
|
679
711
|
|
|
680
712
|
if (!message.event.startTime) {
|
|
681
|
-
m.eventMessage.startTime =
|
|
713
|
+
m.eventMessage.startTime = unixTimestampSeconds() + 86400
|
|
682
714
|
}
|
|
683
715
|
|
|
684
716
|
if (options.getCallLink && message.event.call) {
|
|
@@ -696,7 +728,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
696
728
|
else if ('product' in message) {
|
|
697
729
|
const { imageMessage } = await prepareWAMessageMedia({ image: message.product.productImage }, options)
|
|
698
730
|
|
|
699
|
-
m.productMessage =
|
|
731
|
+
m.productMessage = WAProto.Message.ProductMessage.fromObject({
|
|
700
732
|
...message,
|
|
701
733
|
product: {
|
|
702
734
|
...message.product,
|
|
@@ -712,7 +744,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
712
744
|
|
|
713
745
|
else if ('pollResult' in message) {
|
|
714
746
|
if (!Array.isArray(message.pollResult.values)) {
|
|
715
|
-
throw new
|
|
747
|
+
throw new Boom('Invalid pollResult values', { statusCode: 400 })
|
|
716
748
|
}
|
|
717
749
|
|
|
718
750
|
const pollResultSnapshotMessage = {
|
|
@@ -733,12 +765,12 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
733
765
|
|
|
734
766
|
else if ('poll' in message) {
|
|
735
767
|
if (!Array.isArray(message.poll.values)) {
|
|
736
|
-
throw new
|
|
768
|
+
throw new Boom('Invalid poll values', { statusCode: 400 })
|
|
737
769
|
}
|
|
738
770
|
|
|
739
771
|
if (message.poll.selectableCount < 0
|
|
740
772
|
|| message.poll.selectableCount > message.poll.values.length) {
|
|
741
|
-
throw new
|
|
773
|
+
throw new Boom(`poll.selectableCount in poll should be >= 0 and <= ${message.poll.values.length}`, { statusCode: 400 })
|
|
742
774
|
}
|
|
743
775
|
|
|
744
776
|
const pollCreationMessage = {
|
|
@@ -802,9 +834,9 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
802
834
|
|
|
803
835
|
const stickerData = {}
|
|
804
836
|
const stickerPromises = stickers.map(async (s, i) => {
|
|
805
|
-
const { stream } = await
|
|
806
|
-
const buffer = await
|
|
807
|
-
const hash =
|
|
837
|
+
const { stream } = await getStream(s.sticker)
|
|
838
|
+
const buffer = await toBuffer(stream)
|
|
839
|
+
const hash = sha256(buffer).toString('base64url')
|
|
808
840
|
const fileName = `${i.toString().padStart(2, '0')}_${hash}.webp`
|
|
809
841
|
stickerData[fileName] = [new Uint8Array(buffer), { level: 0 }]
|
|
810
842
|
|
|
@@ -830,10 +862,10 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
830
862
|
})
|
|
831
863
|
})
|
|
832
864
|
|
|
833
|
-
const coverBuffer = await
|
|
865
|
+
const coverBuffer = await toBuffer((await getStream(cover)).stream)
|
|
834
866
|
|
|
835
867
|
const [stickerPackUpload, coverUpload] = await Promise.all([
|
|
836
|
-
|
|
868
|
+
encryptedStream(zipBuffer, 'sticker-pack', { logger: options.logger, opts: options.options }),
|
|
837
869
|
prepareWAMessageMedia({ image: coverBuffer }, { ...options, mediaTypeOverride: 'image' })
|
|
838
870
|
])
|
|
839
871
|
|
|
@@ -845,15 +877,15 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
845
877
|
|
|
846
878
|
|
|
847
879
|
const coverImage = coverUpload.imageMessage
|
|
848
|
-
const imageDataHash =
|
|
849
|
-
const stickerPackId = packId ||
|
|
880
|
+
const imageDataHash = sha256(coverBuffer).toString('base64')
|
|
881
|
+
const stickerPackId = packId || generateMessageID()
|
|
850
882
|
|
|
851
883
|
m.stickerPackMessage = {
|
|
852
884
|
name,
|
|
853
885
|
publisher,
|
|
854
886
|
stickerPackId,
|
|
855
887
|
packDescription: description,
|
|
856
|
-
stickerPackOrigin:
|
|
888
|
+
stickerPackOrigin: proto.Message.StickerPackMessage.StickerPackOrigin.THIRD_PARTY,
|
|
857
889
|
stickerPackSize: stickerPackUpload.fileLength,
|
|
858
890
|
stickers: stickerMetadata,
|
|
859
891
|
fileSha256: stickerPackUpload.fileSha256,
|
|
@@ -861,7 +893,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
861
893
|
mediaKey: stickerPackUpload.mediaKey,
|
|
862
894
|
directPath: stickerPackUploadResult.directPath,
|
|
863
895
|
fileLength: stickerPackUpload.fileLength,
|
|
864
|
-
mediaKeyTimestamp:
|
|
896
|
+
mediaKeyTimestamp: unixTimestampSeconds(),
|
|
865
897
|
trayIconFileName: `${stickerPackId}.png`,
|
|
866
898
|
imageDataHash,
|
|
867
899
|
thumbnailDirectPath: coverImage.directPath,
|
|
@@ -879,7 +911,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
879
911
|
|
|
880
912
|
else if ('sharePhoneNumber' in message) {
|
|
881
913
|
m.protocolMessage = {
|
|
882
|
-
type:
|
|
914
|
+
type: WAProto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER
|
|
883
915
|
}
|
|
884
916
|
}
|
|
885
917
|
|
|
@@ -906,7 +938,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
906
938
|
footerText: message.footer,
|
|
907
939
|
description: message.text,
|
|
908
940
|
sections: message.sections,
|
|
909
|
-
listType:
|
|
941
|
+
listType: proto.Message.ListMessage.ListType.SINGLE_SELECT
|
|
910
942
|
}
|
|
911
943
|
|
|
912
944
|
listMessage.contextInfo = {
|
|
@@ -918,7 +950,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
918
950
|
}
|
|
919
951
|
|
|
920
952
|
else if ('productList' in message && !!message.productList) {
|
|
921
|
-
const thumbnail = message.thumbnail ? await
|
|
953
|
+
const thumbnail = message.thumbnail ? await generateThumbnail(message.thumbnail, 'image') : null
|
|
922
954
|
|
|
923
955
|
const listMessage = {
|
|
924
956
|
title: message.title,
|
|
@@ -933,7 +965,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
933
965
|
},
|
|
934
966
|
businessOwnerJid: message.businessOwnerJid
|
|
935
967
|
},
|
|
936
|
-
listType:
|
|
968
|
+
listType: proto.Message.ListMessage.ListType.PRODUCT_LIST
|
|
937
969
|
}
|
|
938
970
|
|
|
939
971
|
listMessage.contextInfo = {
|
|
@@ -946,12 +978,12 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
946
978
|
|
|
947
979
|
else if ('buttons' in message && !!message.buttons) {
|
|
948
980
|
const buttonsMessage = {
|
|
949
|
-
buttons: message.buttons.map(b => ({ ...b, type:
|
|
981
|
+
buttons: message.buttons.map(b => ({ ...b, type: proto.Message.ButtonsMessage.Button.Type.RESPONSE }))
|
|
950
982
|
}
|
|
951
983
|
|
|
952
984
|
if ('text' in message) {
|
|
953
985
|
buttonsMessage.contentText = message.text
|
|
954
|
-
buttonsMessage.headerType =
|
|
986
|
+
buttonsMessage.headerType = proto.Message.ButtonsMessage.HeaderType.EMPTY
|
|
955
987
|
}
|
|
956
988
|
|
|
957
989
|
else {
|
|
@@ -961,7 +993,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
961
993
|
|
|
962
994
|
const type = Object.keys(m)[0].replace('Message', '').toUpperCase()
|
|
963
995
|
|
|
964
|
-
buttonsMessage.headerType =
|
|
996
|
+
buttonsMessage.headerType = proto.Message.ButtonsMessage.HeaderType[type]
|
|
965
997
|
|
|
966
998
|
Object.assign(buttonsMessage, m)
|
|
967
999
|
}
|
|
@@ -972,7 +1004,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
972
1004
|
|
|
973
1005
|
if ('title' in message && !!message.title) {
|
|
974
1006
|
buttonsMessage.text = message.title
|
|
975
|
-
buttonsMessage.headerType =
|
|
1007
|
+
buttonsMessage.headerType = proto.Message.ButtonsMessage.HeaderType.TEXT
|
|
976
1008
|
}
|
|
977
1009
|
|
|
978
1010
|
buttonsMessage.contextInfo = {
|
|
@@ -1250,12 +1282,12 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
1250
1282
|
key: message.edit,
|
|
1251
1283
|
editedMessage: m,
|
|
1252
1284
|
timestampMs: Date.now(),
|
|
1253
|
-
type:
|
|
1285
|
+
type: WAProto.Message.ProtocolMessage.Type.MESSAGE_EDIT
|
|
1254
1286
|
}
|
|
1255
1287
|
}
|
|
1256
1288
|
}
|
|
1257
1289
|
|
|
1258
|
-
return
|
|
1290
|
+
return WAProto.Message.fromObject(m)
|
|
1259
1291
|
}
|
|
1260
1292
|
|
|
1261
1293
|
const generateWAMessageFromContent = (jid, message, options) => {
|
|
@@ -1265,11 +1297,11 @@ const generateWAMessageFromContent = (jid, message, options) => {
|
|
|
1265
1297
|
|
|
1266
1298
|
const innerMessage = normalizeMessageContent(message)
|
|
1267
1299
|
const key = getContentType(innerMessage)
|
|
1268
|
-
const timestamp =
|
|
1300
|
+
const timestamp = unixTimestampSeconds(options.timestamp)
|
|
1269
1301
|
const threadId = []
|
|
1270
1302
|
const { quoted, userJid } = options
|
|
1271
1303
|
|
|
1272
|
-
if (quoted && !
|
|
1304
|
+
if (quoted && !isJidNewsletter(jid)) {
|
|
1273
1305
|
const participant = quoted.key.fromMe
|
|
1274
1306
|
? userJid
|
|
1275
1307
|
: quoted.participant || quoted.key.participant || quoted.key.remoteJid
|
|
@@ -1277,7 +1309,7 @@ const generateWAMessageFromContent = (jid, message, options) => {
|
|
|
1277
1309
|
let quotedMsg = normalizeMessageContent(quoted.message)
|
|
1278
1310
|
const msgType = getContentType(quotedMsg)
|
|
1279
1311
|
|
|
1280
|
-
quotedMsg =
|
|
1312
|
+
quotedMsg = proto.Message.fromObject({ [msgType]: quotedMsg[msgType] })
|
|
1281
1313
|
|
|
1282
1314
|
const quotedContent = quotedMsg[msgType]
|
|
1283
1315
|
|
|
@@ -1297,7 +1329,7 @@ const generateWAMessageFromContent = (jid, message, options) => {
|
|
|
1297
1329
|
|
|
1298
1330
|
const contextInfo = (key === 'requestPaymentMessage' ? requestPayment?.contextInfo : innerMessage[key].contextInfo) || {}
|
|
1299
1331
|
|
|
1300
|
-
contextInfo.participant =
|
|
1332
|
+
contextInfo.participant = jidNormalizedUser(participant)
|
|
1301
1333
|
contextInfo.stanzaId = quoted.key.id
|
|
1302
1334
|
contextInfo.quotedMessage = quotedMsg
|
|
1303
1335
|
|
|
@@ -1306,16 +1338,16 @@ const generateWAMessageFromContent = (jid, message, options) => {
|
|
|
1306
1338
|
}
|
|
1307
1339
|
|
|
1308
1340
|
if (contextInfo.quotedMessage) {
|
|
1309
|
-
contextInfo.quotedType =
|
|
1341
|
+
contextInfo.quotedType = proto.ContextInfo.QuotedType.EXPLICIT
|
|
1310
1342
|
}
|
|
1311
1343
|
|
|
1312
|
-
if (contextInfo.quotedMessage &&
|
|
1344
|
+
if (contextInfo.quotedMessage && isJidGroup(jid)) {
|
|
1313
1345
|
threadId.push({
|
|
1314
|
-
threadType:
|
|
1346
|
+
threadType: proto.ThreadID.ThreadType.VIEW_REPLIES,
|
|
1315
1347
|
threadKey: {
|
|
1316
1348
|
remoteJid: quoted?.key?.remoteJid,
|
|
1317
1349
|
fromMe: quoted?.key?.fromMe,
|
|
1318
|
-
id:
|
|
1350
|
+
id: generateMessageID(),
|
|
1319
1351
|
...(quoted?.key?.fromMe ? {} : { participant: quoted?.key?.participant })
|
|
1320
1352
|
}
|
|
1321
1353
|
})
|
|
@@ -1330,10 +1362,10 @@ const generateWAMessageFromContent = (jid, message, options) => {
|
|
|
1330
1362
|
|
|
1331
1363
|
if (key !== 'protocolMessage' &&
|
|
1332
1364
|
key !== 'ephemeralMessage' &&
|
|
1333
|
-
!
|
|
1365
|
+
!isJidNewsletter(jid)) {
|
|
1334
1366
|
message.messageContextInfo = {
|
|
1335
1367
|
threadId: threadId.length > 0 ? threadId : [],
|
|
1336
|
-
messageSecret:
|
|
1368
|
+
messageSecret: randomBytes(32),
|
|
1337
1369
|
...message.messageContextInfo
|
|
1338
1370
|
}
|
|
1339
1371
|
innerMessage[key].contextInfo = {
|
|
@@ -1342,28 +1374,28 @@ const generateWAMessageFromContent = (jid, message, options) => {
|
|
|
1342
1374
|
}
|
|
1343
1375
|
}
|
|
1344
1376
|
|
|
1345
|
-
message =
|
|
1377
|
+
message = WAProto.Message.fromObject(message)
|
|
1346
1378
|
|
|
1347
1379
|
const messageJSON = {
|
|
1348
1380
|
key: {
|
|
1349
1381
|
remoteJid: jid,
|
|
1350
1382
|
fromMe: true,
|
|
1351
|
-
id: options?.messageId ||
|
|
1383
|
+
id: options?.messageId || generateMessageID()
|
|
1352
1384
|
},
|
|
1353
1385
|
message: message,
|
|
1354
1386
|
messageTimestamp: timestamp,
|
|
1355
1387
|
messageStubParameters: [],
|
|
1356
|
-
participant:
|
|
1357
|
-
status:
|
|
1388
|
+
participant: isJidGroup(jid) || isJidStatusBroadcast(jid) ? userJid : undefined,
|
|
1389
|
+
status: WAMessageStatus.PENDING
|
|
1358
1390
|
}
|
|
1359
1391
|
|
|
1360
|
-
return
|
|
1392
|
+
return WAProto.WebMessageInfo.fromObject(messageJSON)
|
|
1361
1393
|
}
|
|
1362
1394
|
|
|
1363
1395
|
const generateWAMessage = async (jid, content, options) => {
|
|
1364
1396
|
options.logger = options?.logger?.child({ msgId: options.messageId })
|
|
1365
1397
|
|
|
1366
|
-
return generateWAMessageFromContent(jid, await generateWAMessageContent(content, { newsletter:
|
|
1398
|
+
return generateWAMessageFromContent(jid, await generateWAMessageContent(content, { newsletter: isJidNewsletter(jid), ...options }), options)
|
|
1367
1399
|
}
|
|
1368
1400
|
|
|
1369
1401
|
const getContentType = (content) => {
|
|
@@ -1517,9 +1549,9 @@ const updateMessageWithReceipt = (msg, receipt) => {
|
|
|
1517
1549
|
|
|
1518
1550
|
/** Update the message with a new reaction */
|
|
1519
1551
|
const updateMessageWithReaction = (msg, reaction) => {
|
|
1520
|
-
const authorID =
|
|
1552
|
+
const authorID = getKeyAuthor(reaction.key)
|
|
1521
1553
|
const reactions = (msg.reactions || [])
|
|
1522
|
-
.filter(r =>
|
|
1554
|
+
.filter(r => getKeyAuthor(r.key) !== authorID)
|
|
1523
1555
|
|
|
1524
1556
|
reaction.text = reaction.text || ''
|
|
1525
1557
|
reactions.push(reaction)
|
|
@@ -1528,9 +1560,9 @@ const updateMessageWithReaction = (msg, reaction) => {
|
|
|
1528
1560
|
|
|
1529
1561
|
/** Update the message with a new poll update */
|
|
1530
1562
|
const updateMessageWithPollUpdate = (msg, update) => {
|
|
1531
|
-
const authorID =
|
|
1563
|
+
const authorID = getKeyAuthor(update.pollUpdateMessageKey)
|
|
1532
1564
|
const votes = (msg.pollUpdates || [])
|
|
1533
|
-
.filter(r =>
|
|
1565
|
+
.filter(r => getKeyAuthor(r.pollUpdateMessageKey) !== authorID)
|
|
1534
1566
|
|
|
1535
1567
|
if (update.vote?.selectedOptions?.length) {
|
|
1536
1568
|
votes.push(update)
|
|
@@ -1541,9 +1573,9 @@ const updateMessageWithPollUpdate = (msg, update) => {
|
|
|
1541
1573
|
|
|
1542
1574
|
/** Update the message with a new event response*/
|
|
1543
1575
|
const updateMessageWithEventResponse = (msg, update) => {
|
|
1544
|
-
const authorID =
|
|
1576
|
+
const authorID = getKeyAuthor(update.eventResponseMessageKey)
|
|
1545
1577
|
const responses = (msg.eventResponses || [])
|
|
1546
|
-
.filter(r =>
|
|
1578
|
+
.filter(r => getKeyAuthor(r.eventResponseMessageKey) !== authorID)
|
|
1547
1579
|
|
|
1548
1580
|
responses.push(update)
|
|
1549
1581
|
msg.eventResponses = responses
|
|
@@ -1561,7 +1593,7 @@ function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
|
|
|
1561
1593
|
const opts = message?.pollCreationMessage?.options || message?.pollCreationMessageV2?.options || message?.pollCreationMessageV3?.options || []
|
|
1562
1594
|
|
|
1563
1595
|
const voteHashMap = opts.reduce((acc, opt) => {
|
|
1564
|
-
const hash =
|
|
1596
|
+
const hash = sha256(Buffer.from(opt.optionName || '')).toString()
|
|
1565
1597
|
acc[hash] = {
|
|
1566
1598
|
name: opt.optionName || '',
|
|
1567
1599
|
voters: []
|
|
@@ -1590,7 +1622,7 @@ function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
|
|
|
1590
1622
|
data = voteHashMap[hash]
|
|
1591
1623
|
}
|
|
1592
1624
|
|
|
1593
|
-
voteHashMap[hash].voters.push(
|
|
1625
|
+
voteHashMap[hash].voters.push(getKeyAuthor(update.pollUpdateMessageKey, meId))
|
|
1594
1626
|
}
|
|
1595
1627
|
}
|
|
1596
1628
|
|
|
@@ -1616,9 +1648,9 @@ function getAggregateResponsesInEventMessage({ eventResponses }, meLid) {
|
|
|
1616
1648
|
|
|
1617
1649
|
for (const update of eventResponses) {
|
|
1618
1650
|
const { response } = update.response || 0
|
|
1619
|
-
const responseType =
|
|
1651
|
+
const responseType = proto.Message.EventResponseMessage.EventResponseType[response]
|
|
1620
1652
|
if (responseType !== 'UNKNOWN' && responseMap[responseType]) {
|
|
1621
|
-
responseMap[responseType].responders.push(
|
|
1653
|
+
responseMap[responseType].responders.push(getKeyAuthor(update.eventResponseMessageKey, meLid))
|
|
1622
1654
|
}
|
|
1623
1655
|
}
|
|
1624
1656
|
|
|
@@ -1676,7 +1708,7 @@ const downloadMediaMessage = async (message, type, options, ctx) => {
|
|
|
1676
1708
|
const mContent = extractMessageContent(message.message)
|
|
1677
1709
|
|
|
1678
1710
|
if (!mContent) {
|
|
1679
|
-
throw new
|
|
1711
|
+
throw new Boom('No message present', { statusCode: 400, data: message })
|
|
1680
1712
|
}
|
|
1681
1713
|
|
|
1682
1714
|
const contentType = getContentType(mContent)
|
|
@@ -1684,7 +1716,7 @@ const downloadMediaMessage = async (message, type, options, ctx) => {
|
|
|
1684
1716
|
const media = contentType === 'productMessage' ? mContent[contentType]?.product?.productImage : mContent[contentType]
|
|
1685
1717
|
|
|
1686
1718
|
if (!media || typeof media !== 'object' || (!('url' in media) && !('thumbnailDirectPath' in media))) {
|
|
1687
|
-
throw new
|
|
1719
|
+
throw new Boom(`"${contentType}" message is not a media message`)
|
|
1688
1720
|
}
|
|
1689
1721
|
|
|
1690
1722
|
let download
|
|
@@ -1701,7 +1733,7 @@ const downloadMediaMessage = async (message, type, options, ctx) => {
|
|
|
1701
1733
|
download = media
|
|
1702
1734
|
}
|
|
1703
1735
|
|
|
1704
|
-
const stream = await
|
|
1736
|
+
const stream = await downloadContentFromMessage(download, mediaType, options)
|
|
1705
1737
|
|
|
1706
1738
|
if (type === 'buffer') {
|
|
1707
1739
|
const bufferArray = []
|
|
@@ -1728,7 +1760,7 @@ const assertMediaContent = (content) => {
|
|
|
1728
1760
|
|| content?.stickerMessage
|
|
1729
1761
|
|
|
1730
1762
|
if (!mediaContent) {
|
|
1731
|
-
throw new
|
|
1763
|
+
throw new Boom('given message is not a media message', { statusCode: 400, data: content })
|
|
1732
1764
|
}
|
|
1733
1765
|
return mediaContent
|
|
1734
1766
|
}
|