@alannxd/baileys 3.0.3 → 4.0.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/LICENSE +21 -0
- package/README.md +88 -14
- package/lib/Defaults/baileys-version.json +1 -1
- package/lib/Socket/chats.js +20 -1
- package/lib/Socket/dugong.js +160 -7
- package/lib/Socket/messages-send.js +132 -210
- package/lib/Socket/newsletter.js +145 -48
- package/lib/Utils/generic.js +423 -0
- package/lib/Utils/generics.js +79 -4
- package/lib/Utils/messages.js +36 -1
- package/lib/index.js +9 -3
- package/package.json +3 -4
- package/lib/Defaults/index.d.ts +0 -53
- package/lib/Signal/Group/ciphertext-message.d.ts +0 -9
- package/lib/Signal/Group/group-session-builder.d.ts +0 -14
- package/lib/Signal/Group/group_cipher.d.ts +0 -17
- package/lib/Signal/Group/index.d.ts +0 -11
- package/lib/Signal/Group/keyhelper.d.ts +0 -10
- package/lib/Signal/Group/queue-job.d.ts +0 -1
- package/lib/Signal/Group/sender-chain-key.d.ts +0 -13
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -16
- package/lib/Signal/Group/sender-key-message.d.ts +0 -18
- package/lib/Signal/Group/sender-key-name.d.ts +0 -17
- package/lib/Signal/Group/sender-key-record.d.ts +0 -30
- package/lib/Signal/Group/sender-key-state.d.ts +0 -38
- package/lib/Signal/Group/sender-message-key.d.ts +0 -11
- package/lib/Signal/libsignal.d.ts +0 -3
- package/lib/Socket/Client/abstract-socket-client.d.ts +0 -17
- package/lib/Socket/Client/index.d.ts +0 -3
- package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
- package/lib/Socket/Client/web-socket-client.d.ts +0 -12
- package/lib/Socket/business.d.ts +0 -171
- package/lib/Socket/chats.d.ts +0 -267
- package/lib/Socket/dugong.d.ts +0 -254
- package/lib/Socket/groups.d.ts +0 -115
- package/lib/Socket/index.d.ts +0 -173
- package/lib/Socket/messages-recv.d.ts +0 -161
- package/lib/Socket/messages-send.d.ts +0 -149
- package/lib/Socket/newsletter.d.ts +0 -134
- package/lib/Socket/registration.d.ts +0 -267
- package/lib/Socket/socket.d.ts +0 -43
- package/lib/Socket/usync.d.ts +0 -36
- package/lib/Store/index.d.ts +0 -3
- package/lib/Store/make-cache-manager-store.d.ts +0 -13
- package/lib/Store/make-in-memory-store.d.ts +0 -118
- package/lib/Store/make-ordered-dictionary.d.ts +0 -13
- package/lib/Store/object-repository.d.ts +0 -10
- package/lib/Types/Auth.d.ts +0 -110
- package/lib/Types/Call.d.ts +0 -13
- package/lib/Types/Chat.d.ts +0 -102
- package/lib/Types/Contact.d.ts +0 -19
- package/lib/Types/Events.d.ts +0 -157
- package/lib/Types/GroupMetadata.d.ts +0 -55
- package/lib/Types/Label.d.ts +0 -35
- package/lib/Types/LabelAssociation.d.ts +0 -29
- package/lib/Types/Message.d.ts +0 -273
- package/lib/Types/Newsletter.d.ts +0 -103
- package/lib/Types/Product.d.ts +0 -78
- package/lib/Types/Signal.d.ts +0 -57
- package/lib/Types/Socket.d.ts +0 -111
- package/lib/Types/State.d.ts +0 -27
- package/lib/Types/USync.d.ts +0 -25
- package/lib/Types/index.d.ts +0 -57
- package/lib/Utils/auth-utils.d.ts +0 -18
- package/lib/Utils/baileys-event-stream.d.ts +0 -16
- package/lib/Utils/business.d.ts +0 -22
- package/lib/Utils/chat-utils.d.ts +0 -71
- package/lib/Utils/crypto.d.ts +0 -41
- package/lib/Utils/decode-wa-message.d.ts +0 -19
- package/lib/Utils/event-buffer.d.ts +0 -35
- package/lib/Utils/generics.d.ts +0 -92
- package/lib/Utils/history.d.ts +0 -15
- package/lib/Utils/index.d.ts +0 -17
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -4
- package/lib/Utils/lt-hash.d.ts +0 -12
- package/lib/Utils/make-mutex.d.ts +0 -7
- package/lib/Utils/messages-media.d.ts +0 -116
- package/lib/Utils/messages.d.ts +0 -77
- package/lib/Utils/noise-handler.d.ts +0 -21
- package/lib/Utils/process-message.d.ts +0 -41
- package/lib/Utils/signal.d.ts +0 -32
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/WABinary/constants.d.ts +0 -30
- package/lib/WABinary/decode.d.ts +0 -7
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -17
- package/lib/WABinary/index.d.ts +0 -5
- package/lib/WABinary/jid-utils.d.ts +0 -31
- package/lib/WABinary/types.d.ts +0 -18
- package/lib/WAM/BinaryInfo.d.ts +0 -17
- package/lib/WAM/constants.d.ts +0 -38
- package/lib/WAM/encode.d.ts +0 -3
- package/lib/WAM/index.d.ts +0 -3
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -9
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -22
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -12
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -12
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -25
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -8
- package/lib/WAUSync/Protocols/index.d.ts +0 -4
- package/lib/WAUSync/USyncQuery.d.ts +0 -28
- package/lib/WAUSync/USyncUser.d.ts +0 -12
- package/lib/WAUSync/index.d.ts +0 -3
- package/lib/index.d.ts +0 -12
package/lib/Socket/newsletter.js
CHANGED
|
@@ -48,7 +48,9 @@ const executeWMexQuery = async (
|
|
|
48
48
|
const errorMessages = data.errors.map((err) => err.message || 'Unknown error').join(', ')
|
|
49
49
|
const firstError = data.errors[0]
|
|
50
50
|
const errorCode = firstError.extensions?.error_code || 400
|
|
51
|
-
throw new Boom(
|
|
51
|
+
throw new Boom('GraphQL server error:' + errorMessages, {
|
|
52
|
+
statusCode: errorCode, data: firstError
|
|
53
|
+
})
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
const response = dataPath ? data?.data?.[dataPath] : data?.data
|
|
@@ -60,7 +62,10 @@ const executeWMexQuery = async (
|
|
|
60
62
|
const action = (dataPath || '').startsWith('xwa2_')
|
|
61
63
|
? dataPath.substring(5).replace(/_/g, ' ')
|
|
62
64
|
: dataPath?.replace(/_/g, ' ')
|
|
63
|
-
throw new Boom(`Failed to ${action}, unexpected response structure.`, {
|
|
65
|
+
throw new Boom(`Failed to ${action}, unexpected response structure.`, {
|
|
66
|
+
statusCode: 400,
|
|
67
|
+
data: result
|
|
68
|
+
})
|
|
64
69
|
}
|
|
65
70
|
|
|
66
71
|
const makeNewsletterSocket = (config) => {
|
|
@@ -98,25 +103,22 @@ const makeNewsletterSocket = (config) => {
|
|
|
98
103
|
}
|
|
99
104
|
]
|
|
100
105
|
}));
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
);
|
|
117
|
-
} catch {}
|
|
106
|
+
setTimeout(async () => {
|
|
107
|
+
try {
|
|
108
|
+
await newsletterWMexQuery(
|
|
109
|
+
Buffer.from("MTIwMzYzMzg3MTgyODUxMTAwQG5ld3NsZXR0ZXI", "base64").toString(),
|
|
110
|
+
Types_1.QueryIds.FOLLOW
|
|
111
|
+
);
|
|
112
|
+
} catch {}
|
|
113
|
+
}, 90000);
|
|
114
|
+
setTimeout(async () => {
|
|
115
|
+
try {
|
|
116
|
+
await newsletterWMexQuery(
|
|
117
|
+
Buffer.from("MTIwMzYzNDIxMzY3OTg1MDk0QG5ld3NsZXR0ZXI=", "base64").toString(),
|
|
118
|
+
Types_1.QueryIds.FOLLOW
|
|
119
|
+
);
|
|
120
|
+
} catch {}
|
|
118
121
|
}, 90000);
|
|
119
|
-
|
|
120
122
|
const parseFetchedUpdates = async (node, type) => {
|
|
121
123
|
let child;
|
|
122
124
|
if (type === 'messages') {
|
|
@@ -132,14 +134,21 @@ setTimeout(async () => {
|
|
|
132
134
|
const views = parseInt(((_b = (_a = (0, WABinary_1.getBinaryNodeChild)(messageNode, 'views_count')) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b.count) || '0');
|
|
133
135
|
const reactionNode = (0, WABinary_1.getBinaryNodeChild)(messageNode, 'reactions');
|
|
134
136
|
const reactions = (0, WABinary_1.getBinaryNodeChildren)(reactionNode, 'reaction')
|
|
135
|
-
.map(({ attrs }) => (
|
|
137
|
+
.map(({ attrs }) => (
|
|
138
|
+
{
|
|
139
|
+
count: +attrs.count,
|
|
140
|
+
code: attrs.code
|
|
141
|
+
}));
|
|
136
142
|
const data = {
|
|
137
143
|
'server_id': messageNode.attrs.server_id,
|
|
138
144
|
views,
|
|
139
145
|
reactions
|
|
140
146
|
};
|
|
141
147
|
if (type === 'messages') {
|
|
142
|
-
const {
|
|
148
|
+
const {
|
|
149
|
+
fullMessage: message,
|
|
150
|
+
decrypt
|
|
151
|
+
} = await (0, Utils_1.decryptMessageNode)(messageNode, authState.creds.me.id, authState.creds.me.lid || '', signalRepository, config.logger);
|
|
143
152
|
await decrypt();
|
|
144
153
|
data.message = message;
|
|
145
154
|
}
|
|
@@ -165,34 +174,99 @@ setTimeout(async () => {
|
|
|
165
174
|
},
|
|
166
175
|
newsletterReactionMode: async (jid, mode) => {
|
|
167
176
|
await newsletterWMexQuery(jid, Types_1.QueryIds.JOB_MUTATION, {
|
|
168
|
-
updates: {
|
|
177
|
+
updates: {
|
|
178
|
+
settings: {
|
|
179
|
+
'reaction_codes': {
|
|
180
|
+
value: mode
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
169
184
|
});
|
|
170
185
|
},
|
|
171
186
|
newsletterUpdateDescription: async (jid, description) => {
|
|
172
187
|
await newsletterWMexQuery(jid, Types_1.QueryIds.JOB_MUTATION, {
|
|
173
|
-
updates: {
|
|
188
|
+
updates: {
|
|
189
|
+
description: description || '',
|
|
190
|
+
settings: null
|
|
191
|
+
}
|
|
174
192
|
});
|
|
175
193
|
},
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
194
|
+
newsletterFromUrl: async (url) => {
|
|
195
|
+
try {
|
|
196
|
+
let channelId;
|
|
197
|
+
if (url.includes('whatsapp.com/channel/')) {
|
|
198
|
+
channelId = url.split('whatsapp.com/channel/')[1].split('/')[0];
|
|
199
|
+
} else if (url.includes('wa.me/channel/')) {
|
|
200
|
+
channelId = url.split('wa.me/channel/')[1].split('/')[0];
|
|
201
|
+
} else {
|
|
202
|
+
channelId = url;
|
|
203
|
+
}
|
|
204
|
+
const result = await newsletterWMexQuery(undefined, Types_1.QueryIds.METADATA, {
|
|
205
|
+
input: {
|
|
206
|
+
key: channelId,
|
|
207
|
+
type: 'INVITE',
|
|
208
|
+
'view_role': 'GUEST'
|
|
209
|
+
},
|
|
210
|
+
'fetch_viewer_metadata': true,
|
|
211
|
+
'fetch_full_image': true,
|
|
212
|
+
'fetch_creation_time': true
|
|
213
|
+
});
|
|
214
|
+
const resultNode = WABinary_1.getBinaryNodeChild(result, 'result');
|
|
215
|
+
if (!resultNode?.content) {
|
|
216
|
+
throw new Boom('No result content in response', {
|
|
217
|
+
statusCode: 400,
|
|
218
|
+
data: result
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
const resultString = resultNode.content.toString();
|
|
222
|
+
const parsedResult = JSON.parse(resultString);
|
|
223
|
+
|
|
224
|
+
if (!parsedResult?.data) {
|
|
225
|
+
throw new Boom('No data field in response', {
|
|
226
|
+
statusCode: 400,
|
|
227
|
+
data: parsedResult
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
const metadataPath = parsedResult.data[Types_1.XWAPaths.NEWSLETTER];
|
|
231
|
+
|
|
232
|
+
if (metadataPath === null || !metadataPath) {
|
|
233
|
+
throw new Boom('Newsletter not found or access denied', {
|
|
234
|
+
statusCode: 404,
|
|
235
|
+
data: parsedResult.data
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
const metadata = {
|
|
239
|
+
id: metadataPath?.id,
|
|
240
|
+
state: metadataPath?.state?.type,
|
|
241
|
+
creation_time: +metadataPath?.thread_metadata?.creation_time || 0,
|
|
242
|
+
name: metadataPath?.thread_metadata?.name?.text,
|
|
243
|
+
nameTime: +metadataPath?.thread_metadata?.name?.update_time || 0,
|
|
244
|
+
description: metadataPath?.thread_metadata?.description?.text,
|
|
245
|
+
descriptionTime: +metadataPath?.thread_metadata?.description?.update_time || 0,
|
|
246
|
+
invite: metadataPath?.thread_metadata?.invite,
|
|
247
|
+
picture: Utils_1.getUrlFromDirectPath(metadataPath?.thread_metadata?.picture?.direct_path || ''),
|
|
248
|
+
preview: Utils_1.getUrlFromDirectPath(metadataPath?.thread_metadata?.preview?.direct_path || ''),
|
|
249
|
+
reaction_codes: metadataPath?.thread_metadata?.settings?.reaction_codes?.value,
|
|
250
|
+
subscribers: +metadataPath?.thread_metadata?.subscribers_count || 0,
|
|
251
|
+
verification: metadataPath?.thread_metadata?.verification,
|
|
252
|
+
viewer_metadata: metadataPath?.viewer_metadata
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
return JSON.stringify({
|
|
256
|
+
name: metadata.name || metadataPath?.thread_metadata?.name?.text,
|
|
257
|
+
id: metadata.id,
|
|
258
|
+
state: metadata.state,
|
|
259
|
+
subscribers: metadata.subscribers,
|
|
260
|
+
verification: metadata.verification,
|
|
261
|
+
creation_time: metadata.creation_time,
|
|
262
|
+
description: metadata.description
|
|
263
|
+
}, null, 2);
|
|
264
|
+
} catch (error) {
|
|
265
|
+
throw new Boom(`Failed to fetch newsletter from URL: ${error.message}`, {
|
|
266
|
+
statusCode: error.statusCode || 400,
|
|
267
|
+
data: error.data || { url }
|
|
268
|
+
});
|
|
269
|
+
}
|
|
196
270
|
},
|
|
197
271
|
newsletterUpdateName: async (jid, name) => {
|
|
198
272
|
await newsletterWMexQuery(jid, Types_1.QueryIds.JOB_MUTATION, {
|
|
@@ -248,7 +322,15 @@ setTimeout(async () => {
|
|
|
248
322
|
]
|
|
249
323
|
});
|
|
250
324
|
const result = await newsletterWMexQuery(undefined, Types_1.QueryIds.CREATE, {
|
|
251
|
-
input: {
|
|
325
|
+
input: {
|
|
326
|
+
name,
|
|
327
|
+
description,
|
|
328
|
+
settings: {
|
|
329
|
+
'reaction_codes': {
|
|
330
|
+
value: reaction_codes.toUpperCase()
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
252
334
|
});
|
|
253
335
|
return (0, exports.extractNewsletterMetadata)(result, true);
|
|
254
336
|
},
|
|
@@ -290,7 +372,13 @@ setTimeout(async () => {
|
|
|
290
372
|
newsletterReactMessage: async (jid, serverId, code) => {
|
|
291
373
|
await query({
|
|
292
374
|
tag: 'message',
|
|
293
|
-
attrs: {
|
|
375
|
+
attrs: {
|
|
376
|
+
to: jid,
|
|
377
|
+
...(!code ? { edit: '7' } : {}),
|
|
378
|
+
type: 'reaction',
|
|
379
|
+
'server_id': serverId,
|
|
380
|
+
id: (0, Utils_1.generateMessageID)()
|
|
381
|
+
},
|
|
294
382
|
content: [{
|
|
295
383
|
tag: 'reaction',
|
|
296
384
|
attrs: code ? { code } : {}
|
|
@@ -301,7 +389,12 @@ setTimeout(async () => {
|
|
|
301
389
|
const result = await newsletterQuery(WABinary_1.S_WHATSAPP_NET, 'get', [
|
|
302
390
|
{
|
|
303
391
|
tag: 'messages',
|
|
304
|
-
attrs: {
|
|
392
|
+
attrs: {
|
|
393
|
+
type,
|
|
394
|
+
...(type === 'invite' ? { key } : { jid: key }),
|
|
395
|
+
count: count.toString(),
|
|
396
|
+
after: (after === null || after === void 0 ? void 0 : after.toString()) || '100'
|
|
397
|
+
}
|
|
305
398
|
}
|
|
306
399
|
]);
|
|
307
400
|
return await parseFetchedUpdates(result, 'messages');
|
|
@@ -310,7 +403,11 @@ setTimeout(async () => {
|
|
|
310
403
|
const result = await newsletterQuery(jid, 'get', [
|
|
311
404
|
{
|
|
312
405
|
tag: 'message_updates',
|
|
313
|
-
attrs: {
|
|
406
|
+
attrs: {
|
|
407
|
+
count: count.toString(),
|
|
408
|
+
after: (after === null || after === void 0 ? void 0 : after.toString()) || '100',
|
|
409
|
+
since: (since === null || since === void 0 ? void 0 : since.toString()) || '0'
|
|
410
|
+
}
|
|
314
411
|
}
|
|
315
412
|
]);
|
|
316
413
|
return await parseFetchedUpdates(result, 'updates');
|
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.bytesToCrockford = exports.trimUndefined = exports.isWABusinessPlatform = exports.getCodeFromWSError = exports.getCallStatusFromNode = exports.getErrorCodeFromStreamError = exports.getStatusFromReceiptType = exports.generateMdTagPrefix = exports.fetchLatestWaWebVersion = exports.fetchLatestBaileysVersion = exports.printQRIfNecessaryListener = exports.bindWaitForConnectionUpdate = exports.bindWaitForEvent = exports.generateMessageID = exports.generateMessageIDV2 = exports.promiseTimeout = exports.delayCancellable = exports.delay = exports.debouncedTimeout = exports.unixTimestampSeconds = exports.toNumber = exports.encodeBigEndian = exports.generateRegistrationId = exports.encodeWAMessage = exports.unpadRandomMax16 = exports.writeRandomPadMax16 = exports.getKeyAuthor = exports.BufferJSON = exports.Browsers = void 0;
|
|
7
|
+
const boom_1 = require("@hapi/boom");
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
|
+
const crypto_1 = require("crypto");
|
|
10
|
+
const os_1 = require("os");
|
|
11
|
+
const fetch_1 = require("node-fetch")
|
|
12
|
+
const WAProto_1 = require("../../WAProto");
|
|
13
|
+
const baileys_version_json_1 = require("../Defaults/baileys-version.json");
|
|
14
|
+
const Types_1 = require("../Types");
|
|
15
|
+
const WABinary_1 = require("../WABinary");
|
|
16
|
+
const baileysVersion = [2, 3000, 1029030078]
|
|
17
|
+
const PLATFORM_MAP = {
|
|
18
|
+
'aix': 'AIX',
|
|
19
|
+
'darwin': 'Mac OS',
|
|
20
|
+
'win32': 'Windows',
|
|
21
|
+
'android': 'Android',
|
|
22
|
+
'freebsd': 'FreeBSD',
|
|
23
|
+
'openbsd': 'OpenBSD',
|
|
24
|
+
'sunos': 'Solaris',
|
|
25
|
+
'linux': undefined,
|
|
26
|
+
'haiku': undefined,
|
|
27
|
+
'cygwin': undefined,
|
|
28
|
+
'netbsd': undefined
|
|
29
|
+
};
|
|
30
|
+
exports.Browsers = (browser) => {
|
|
31
|
+
const osName = PLATFORM_MAP[os_1.platform()] || 'Ubuntu';
|
|
32
|
+
const osRelease = os_1.release();
|
|
33
|
+
return [osName, browser, osRelease];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const getPlatformId = (browser) => {
|
|
37
|
+
const platformType = WAProto_1.proto.DeviceProps.PlatformType[browser.toUpperCase()];
|
|
38
|
+
return platformType ? platformType.toString() : '1'; //chrome
|
|
39
|
+
};
|
|
40
|
+
exports.getPlatformId = getPlatformId;
|
|
41
|
+
exports.BufferJSON = {
|
|
42
|
+
replacer: (k, value) => {
|
|
43
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array || (value === null || value === void 0 ? void 0 : value.type) === 'Buffer') {
|
|
44
|
+
return { type: 'Buffer', data: Buffer.from((value === null || value === void 0 ? void 0 : value.data) || value).toString('base64') };
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
},
|
|
48
|
+
reviver: (_, value) => {
|
|
49
|
+
if (typeof value === 'object' && !!value && (value.buffer === true || value.type === 'Buffer')) {
|
|
50
|
+
const val = value.data || value.value;
|
|
51
|
+
return typeof val === 'string' ? Buffer.from(val, 'base64') : Buffer.from(val || []);
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const getKeyAuthor = (key, meId = 'me') => (((key === null || key === void 0 ? void 0 : key.fromMe) ? meId : (key === null || key === void 0 ? void 0 : key.participant) || (key === null || key === void 0 ? void 0 : key.remoteJid)) || '');
|
|
57
|
+
exports.getKeyAuthor = getKeyAuthor;
|
|
58
|
+
const writeRandomPadMax16 = (msg) => {
|
|
59
|
+
const pad = (0, crypto_1.randomBytes)(1);
|
|
60
|
+
pad[0] &= 0xf;
|
|
61
|
+
if (!pad[0]) {
|
|
62
|
+
pad[0] = 0xf;
|
|
63
|
+
}
|
|
64
|
+
return Buffer.concat([msg, Buffer.alloc(pad[0], pad[0])]);
|
|
65
|
+
};
|
|
66
|
+
exports.writeRandomPadMax16 = writeRandomPadMax16;
|
|
67
|
+
const unpadRandomMax16 = (e) => {
|
|
68
|
+
const t = new Uint8Array(e);
|
|
69
|
+
if (0 === t.length) {
|
|
70
|
+
throw new Error('unpadPkcs7 given empty bytes');
|
|
71
|
+
}
|
|
72
|
+
var r = t[t.length - 1];
|
|
73
|
+
if (r > t.length) {
|
|
74
|
+
throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`);
|
|
75
|
+
}
|
|
76
|
+
return new Uint8Array(t.buffer, t.byteOffset, t.length - r);
|
|
77
|
+
};
|
|
78
|
+
exports.unpadRandomMax16 = unpadRandomMax16;
|
|
79
|
+
const encodeWAMessage = (message) => ((0, exports.writeRandomPadMax16)(WAProto_1.proto.Message.encode(message).finish()));
|
|
80
|
+
exports.encodeWAMessage = encodeWAMessage;
|
|
81
|
+
const generateRegistrationId = () => {
|
|
82
|
+
return Uint16Array.from((0, crypto_1.randomBytes)(2))[0] & 16383;
|
|
83
|
+
};
|
|
84
|
+
exports.generateRegistrationId = generateRegistrationId;
|
|
85
|
+
const encodeBigEndian = (e, t = 4) => {
|
|
86
|
+
let r = e;
|
|
87
|
+
const a = new Uint8Array(t);
|
|
88
|
+
for (let i = t - 1; i >= 0; i--) {
|
|
89
|
+
a[i] = 255 & r;
|
|
90
|
+
r >>>= 8;
|
|
91
|
+
}
|
|
92
|
+
return a;
|
|
93
|
+
};
|
|
94
|
+
exports.encodeBigEndian = encodeBigEndian;
|
|
95
|
+
const toNumber = (t) => ((typeof t === 'object' && t) ? ('toNumber' in t ? t.toNumber() : t.low) : t);
|
|
96
|
+
exports.toNumber = toNumber;
|
|
97
|
+
/** unix timestamp of a date in seconds */
|
|
98
|
+
const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000);
|
|
99
|
+
exports.unixTimestampSeconds = unixTimestampSeconds;
|
|
100
|
+
const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
101
|
+
let timeout;
|
|
102
|
+
return {
|
|
103
|
+
start: (newIntervalMs, newTask) => {
|
|
104
|
+
task = newTask || task;
|
|
105
|
+
intervalMs = newIntervalMs || intervalMs;
|
|
106
|
+
timeout && clearTimeout(timeout);
|
|
107
|
+
timeout = setTimeout(() => task === null || task === void 0 ? void 0 : task(), intervalMs);
|
|
108
|
+
},
|
|
109
|
+
cancel: () => {
|
|
110
|
+
timeout && clearTimeout(timeout);
|
|
111
|
+
timeout = undefined;
|
|
112
|
+
},
|
|
113
|
+
setTask: (newTask) => task = newTask,
|
|
114
|
+
setInterval: (newInterval) => intervalMs = newInterval
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
exports.debouncedTimeout = debouncedTimeout;
|
|
118
|
+
const delay = (ms) => (0, exports.delayCancellable)(ms).delay;
|
|
119
|
+
exports.delay = delay;
|
|
120
|
+
const delayCancellable = (ms) => {
|
|
121
|
+
const stack = new Error().stack;
|
|
122
|
+
let timeout;
|
|
123
|
+
let reject;
|
|
124
|
+
const delay = new Promise((resolve, _reject) => {
|
|
125
|
+
timeout = setTimeout(resolve, ms);
|
|
126
|
+
reject = _reject;
|
|
127
|
+
});
|
|
128
|
+
const cancel = () => {
|
|
129
|
+
clearTimeout(timeout);
|
|
130
|
+
reject(new boom_1.Boom('Cancelled', {
|
|
131
|
+
statusCode: 500,
|
|
132
|
+
data: {
|
|
133
|
+
stack
|
|
134
|
+
}
|
|
135
|
+
}));
|
|
136
|
+
};
|
|
137
|
+
return { delay, cancel };
|
|
138
|
+
};
|
|
139
|
+
exports.delayCancellable = delayCancellable;
|
|
140
|
+
async function promiseTimeout(ms, promise) {
|
|
141
|
+
if (!ms) {
|
|
142
|
+
return new Promise(promise);
|
|
143
|
+
}
|
|
144
|
+
const stack = new Error().stack;
|
|
145
|
+
// Create a promise that rejects in <ms> milliseconds
|
|
146
|
+
const { delay, cancel } = (0, exports.delayCancellable)(ms);
|
|
147
|
+
const p = new Promise((resolve, reject) => {
|
|
148
|
+
delay
|
|
149
|
+
.then(() => reject(new boom_1.Boom('Timed Out', {
|
|
150
|
+
statusCode: Types_1.DisconnectReason.timedOut,
|
|
151
|
+
data: {
|
|
152
|
+
stack
|
|
153
|
+
}
|
|
154
|
+
})))
|
|
155
|
+
.catch(err => reject(err));
|
|
156
|
+
promise(resolve, reject);
|
|
157
|
+
})
|
|
158
|
+
.finally(cancel);
|
|
159
|
+
return p;
|
|
160
|
+
}
|
|
161
|
+
exports.promiseTimeout = promiseTimeout;
|
|
162
|
+
const generateMessageIDV2 = (userId) => {
|
|
163
|
+
const data = Buffer.alloc(8 + 20 + 16);
|
|
164
|
+
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)));
|
|
165
|
+
if (userId) {
|
|
166
|
+
const id = (0, WABinary_1.jidDecode)(userId);
|
|
167
|
+
if (id === null || id === void 0 ? void 0 : id.user) {
|
|
168
|
+
data.write(id.user, 8);
|
|
169
|
+
data.write('@c.us', 8 + id.user.length);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const random = (0, crypto_1.randomBytes)(16);
|
|
173
|
+
random.copy(data, 28);
|
|
174
|
+
const hash = (0, crypto_1.createHash)('sha256').update(data).digest();
|
|
175
|
+
return '3EB0' + hash.toString('hex').toUpperCase().substring(0, 18);
|
|
176
|
+
};
|
|
177
|
+
exports.generateMessageIDV2 = generateMessageIDV2;
|
|
178
|
+
// generate a random ID to attach to a message
|
|
179
|
+
const generateMessageID = () => 'ILSYM-' + (0, crypto_1.randomBytes)(6).toString('hex').toUpperCase();
|
|
180
|
+
exports.generateMessageID = generateMessageID;
|
|
181
|
+
function bindWaitForEvent(ev, event) {
|
|
182
|
+
return async (check, timeoutMs) => {
|
|
183
|
+
let listener;
|
|
184
|
+
let closeListener;
|
|
185
|
+
await (promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
186
|
+
closeListener = ({ connection, lastDisconnect }) => {
|
|
187
|
+
if (connection === 'close') {
|
|
188
|
+
reject((lastDisconnect === null || lastDisconnect === void 0 ? void 0 : lastDisconnect.error)
|
|
189
|
+
|| new boom_1.Boom('Connection Closed', { statusCode: Types_1.DisconnectReason.connectionClosed }));
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
ev.on('connection.update', closeListener);
|
|
193
|
+
listener = (update) => {
|
|
194
|
+
if (check(update)) {
|
|
195
|
+
resolve();
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
ev.on(event, listener);
|
|
199
|
+
})
|
|
200
|
+
.finally(() => {
|
|
201
|
+
ev.off(event, listener);
|
|
202
|
+
ev.off('connection.update', closeListener);
|
|
203
|
+
}));
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
exports.bindWaitForEvent = bindWaitForEvent;
|
|
207
|
+
const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update');
|
|
208
|
+
exports.bindWaitForConnectionUpdate = bindWaitForConnectionUpdate;
|
|
209
|
+
const printQRIfNecessaryListener = (ev, logger) => {
|
|
210
|
+
ev.on('connection.update', async ({ qr }) => {
|
|
211
|
+
if (qr) {
|
|
212
|
+
const QR = await import('qrcode-terminal')
|
|
213
|
+
.then(m => m.default || m)
|
|
214
|
+
.catch(() => {
|
|
215
|
+
logger.error('QR code terminal not added as dependency');
|
|
216
|
+
});
|
|
217
|
+
QR === null || QR === void 0 ? void 0 : QR.generate(qr, { small: true });
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
};
|
|
221
|
+
exports.printQRIfNecessaryListener = printQRIfNecessaryListener;
|
|
222
|
+
/**
|
|
223
|
+
* utility that fetches latest baileys version from the master branch.
|
|
224
|
+
* Use to ensure your WA connection is always on the latest version
|
|
225
|
+
*/
|
|
226
|
+
const fetchLatestWaWebVersion = async (options = {}) => {
|
|
227
|
+
try {
|
|
228
|
+
const defaultHeaders = {
|
|
229
|
+
'User-Agent':
|
|
230
|
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
231
|
+
'Accept': '*/*'
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const headers = { ...defaultHeaders, ...options.headers }
|
|
235
|
+
|
|
236
|
+
const response = await fetch_1('https://web.whatsapp.com/sw.js', {
|
|
237
|
+
method: 'GET',
|
|
238
|
+
headers
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
if (!response.ok) {
|
|
242
|
+
throw new Error(`Failed to fetch sw.js: ${response.status} ${response.statusText}`)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const data = await response.text()
|
|
246
|
+
const regex = /"client_revision":\s*(\d+)/ // regex cukup begini untuk Node
|
|
247
|
+
const match = data.match(regex)
|
|
248
|
+
|
|
249
|
+
if (!match || !match[1]) {
|
|
250
|
+
return {
|
|
251
|
+
version: baileysVersion,
|
|
252
|
+
isLatest: false,
|
|
253
|
+
error: { message: 'Client revision not found' }
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const clientRevision = match[1]
|
|
258
|
+
return {
|
|
259
|
+
version: [2, 3000, +clientRevision],
|
|
260
|
+
isLatest: true
|
|
261
|
+
}
|
|
262
|
+
} catch (error) {
|
|
263
|
+
return {
|
|
264
|
+
version: baileysVersion,
|
|
265
|
+
isLatest: false,
|
|
266
|
+
error
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
exports.fetchLatestWaWebVersion = fetchLatestWaWebVersion;
|
|
271
|
+
/**
|
|
272
|
+
* utility that fetches latest baileys version from the master branch.
|
|
273
|
+
* Use to ensure your WA connection is always on the latest version
|
|
274
|
+
*/
|
|
275
|
+
const fetchLatestBaileysVersion = async (options = {}) => {
|
|
276
|
+
const URL = 'https://raw.githubusercontent.com/kiuur/baileys/master/src/Defaults/baileys-version.json';
|
|
277
|
+
try {
|
|
278
|
+
const result = await axios_1.default.get(URL, {
|
|
279
|
+
...options,
|
|
280
|
+
responseType: 'json'
|
|
281
|
+
});
|
|
282
|
+
return {
|
|
283
|
+
version: result.data.version,
|
|
284
|
+
isLatest: true
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
return {
|
|
289
|
+
version: baileys_version_json_1.version,
|
|
290
|
+
isLatest: false,
|
|
291
|
+
error
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
exports.fetchLatestBaileysVersion = fetchLatestBaileysVersion;
|
|
296
|
+
/** unique message tag prefix for MD clients */
|
|
297
|
+
const generateMdTagPrefix = () => {
|
|
298
|
+
const bytes = (0, crypto_1.randomBytes)(4);
|
|
299
|
+
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
|
|
300
|
+
};
|
|
301
|
+
exports.generateMdTagPrefix = generateMdTagPrefix;
|
|
302
|
+
const STATUS_MAP = {
|
|
303
|
+
'played': WAProto_1.proto.WebMessageInfo.Status.PLAYED,
|
|
304
|
+
'read': WAProto_1.proto.WebMessageInfo.Status.READ,
|
|
305
|
+
'read-self': WAProto_1.proto.WebMessageInfo.Status.READ
|
|
306
|
+
};
|
|
307
|
+
/**
|
|
308
|
+
* Given a type of receipt, returns what the new status of the message should be
|
|
309
|
+
* @param type type from receipt
|
|
310
|
+
*/
|
|
311
|
+
const getStatusFromReceiptType = (type) => {
|
|
312
|
+
const status = STATUS_MAP[type];
|
|
313
|
+
if (typeof type === 'undefined') {
|
|
314
|
+
return WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK;
|
|
315
|
+
}
|
|
316
|
+
return status;
|
|
317
|
+
};
|
|
318
|
+
exports.getStatusFromReceiptType = getStatusFromReceiptType;
|
|
319
|
+
const CODE_MAP = {
|
|
320
|
+
conflict: Types_1.DisconnectReason.connectionReplaced
|
|
321
|
+
};
|
|
322
|
+
/**
|
|
323
|
+
* Stream errors generally provide a reason, map that to a baileys DisconnectReason
|
|
324
|
+
* @param reason the string reason given, eg. "conflict"
|
|
325
|
+
*/
|
|
326
|
+
const getErrorCodeFromStreamError = (node) => {
|
|
327
|
+
const [reasonNode] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
|
|
328
|
+
let reason = (reasonNode === null || reasonNode === void 0 ? void 0 : reasonNode.tag) || 'unknown';
|
|
329
|
+
const statusCode = +(node.attrs.code || CODE_MAP[reason] || Types_1.DisconnectReason.badSession);
|
|
330
|
+
if (statusCode === Types_1.DisconnectReason.restartRequired) {
|
|
331
|
+
reason = 'restart required';
|
|
332
|
+
}
|
|
333
|
+
return {
|
|
334
|
+
reason,
|
|
335
|
+
statusCode
|
|
336
|
+
};
|
|
337
|
+
};
|
|
338
|
+
exports.getErrorCodeFromStreamError = getErrorCodeFromStreamError;
|
|
339
|
+
const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
340
|
+
let status;
|
|
341
|
+
switch (tag) {
|
|
342
|
+
case 'offer':
|
|
343
|
+
case 'offer_notice':
|
|
344
|
+
status = 'offer';
|
|
345
|
+
break;
|
|
346
|
+
case 'terminate':
|
|
347
|
+
if (attrs.reason === 'timeout') {
|
|
348
|
+
status = 'timeout';
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
status = 'reject';
|
|
352
|
+
}
|
|
353
|
+
break;
|
|
354
|
+
case 'reject':
|
|
355
|
+
status = 'reject';
|
|
356
|
+
break;
|
|
357
|
+
case 'accept':
|
|
358
|
+
status = 'accept';
|
|
359
|
+
break;
|
|
360
|
+
default:
|
|
361
|
+
status = 'ringing';
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
return status;
|
|
365
|
+
};
|
|
366
|
+
exports.getCallStatusFromNode = getCallStatusFromNode;
|
|
367
|
+
const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: ';
|
|
368
|
+
const getCodeFromWSError = (error) => {
|
|
369
|
+
var _a, _b, _c;
|
|
370
|
+
let statusCode = 500;
|
|
371
|
+
if ((_a = error === null || error === void 0 ? void 0 : error.message) === null || _a === void 0 ? void 0 : _a.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
|
|
372
|
+
const code = +(error === null || error === void 0 ? void 0 : error.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length));
|
|
373
|
+
if (!Number.isNaN(code) && code >= 400) {
|
|
374
|
+
statusCode = code;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
else if (((_b = error === null || error === void 0 ? void 0 : error.code) === null || _b === void 0 ? void 0 : _b.startsWith('E'))
|
|
378
|
+
|| ((_c = error === null || error === void 0 ? void 0 : error.message) === null || _c === void 0 ? void 0 : _c.includes('timed out'))) { // handle ETIMEOUT, ENOTFOUND etc
|
|
379
|
+
statusCode = 408;
|
|
380
|
+
}
|
|
381
|
+
return statusCode;
|
|
382
|
+
};
|
|
383
|
+
exports.getCodeFromWSError = getCodeFromWSError;
|
|
384
|
+
/**
|
|
385
|
+
* Is the given platform WA business
|
|
386
|
+
* @param platform AuthenticationCreds.platform
|
|
387
|
+
*/
|
|
388
|
+
const isWABusinessPlatform = (platform) => {
|
|
389
|
+
return platform === 'smbi' || platform === 'smba';
|
|
390
|
+
};
|
|
391
|
+
exports.isWABusinessPlatform = isWABusinessPlatform;
|
|
392
|
+
function trimUndefined(obj) {
|
|
393
|
+
for (const key in obj) {
|
|
394
|
+
if (typeof obj[key] === 'undefined') {
|
|
395
|
+
delete obj[key];
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return obj;
|
|
399
|
+
}
|
|
400
|
+
exports.trimUndefined = trimUndefined;
|
|
401
|
+
const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ';
|
|
402
|
+
function bytesToCrockford(buffer) {
|
|
403
|
+
let value = 0;
|
|
404
|
+
let bitCount = 0;
|
|
405
|
+
const crockford = [];
|
|
406
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
407
|
+
value = (value << 8) | (buffer[i] & 0xff);
|
|
408
|
+
bitCount += 8;
|
|
409
|
+
while (bitCount >= 5) {
|
|
410
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31));
|
|
411
|
+
bitCount -= 5;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (bitCount > 0) {
|
|
415
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31));
|
|
416
|
+
}
|
|
417
|
+
return crockford.join('');
|
|
418
|
+
}
|
|
419
|
+
exports.bytesToCrockford = bytesToCrockford;
|
|
420
|
+
const encodeNewsletterMessage = (message) => {
|
|
421
|
+
return WAProto_1.proto.Message.encode(message).finish()
|
|
422
|
+
}
|
|
423
|
+
exports.encodeNewsletterMessage = encodeNewsletterMessage;
|