@itsliaaa/baileys 0.1.0

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.
Files changed (103) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +1078 -0
  3. package/WAProto/index.js +100441 -0
  4. package/engine-requirements.js +10 -0
  5. package/lib/Defaults/index.js +144 -0
  6. package/lib/Signal/Group/ciphertext-message.js +11 -0
  7. package/lib/Signal/Group/group-session-builder.js +29 -0
  8. package/lib/Signal/Group/group_cipher.js +81 -0
  9. package/lib/Signal/Group/index.js +11 -0
  10. package/lib/Signal/Group/keyhelper.js +17 -0
  11. package/lib/Signal/Group/sender-chain-key.js +25 -0
  12. package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
  13. package/lib/Signal/Group/sender-key-message.js +65 -0
  14. package/lib/Signal/Group/sender-key-name.js +47 -0
  15. package/lib/Signal/Group/sender-key-record.js +40 -0
  16. package/lib/Signal/Group/sender-key-state.js +83 -0
  17. package/lib/Signal/Group/sender-message-key.js +25 -0
  18. package/lib/Signal/libsignal.js +402 -0
  19. package/lib/Signal/lid-mapping.js +270 -0
  20. package/lib/Socket/Client/index.js +2 -0
  21. package/lib/Socket/Client/types.js +10 -0
  22. package/lib/Socket/Client/websocket.js +53 -0
  23. package/lib/Socket/business.js +378 -0
  24. package/lib/Socket/chats.js +1048 -0
  25. package/lib/Socket/communities.js +430 -0
  26. package/lib/Socket/groups.js +328 -0
  27. package/lib/Socket/index.js +11 -0
  28. package/lib/Socket/messages-recv.js +1442 -0
  29. package/lib/Socket/messages-send.js +1153 -0
  30. package/lib/Socket/mex.js +41 -0
  31. package/lib/Socket/newsletter.js +227 -0
  32. package/lib/Socket/socket.js +936 -0
  33. package/lib/Store/index.js +3 -0
  34. package/lib/Store/make-in-memory-store.js +421 -0
  35. package/lib/Store/make-ordered-dictionary.js +78 -0
  36. package/lib/Store/object-repository.js +23 -0
  37. package/lib/Types/Auth.js +1 -0
  38. package/lib/Types/Bussines.js +1 -0
  39. package/lib/Types/Call.js +1 -0
  40. package/lib/Types/Chat.js +7 -0
  41. package/lib/Types/Contact.js +1 -0
  42. package/lib/Types/Events.js +1 -0
  43. package/lib/Types/GroupMetadata.js +1 -0
  44. package/lib/Types/Label.js +24 -0
  45. package/lib/Types/LabelAssociation.js +6 -0
  46. package/lib/Types/Message.js +17 -0
  47. package/lib/Types/Newsletter.js +33 -0
  48. package/lib/Types/Product.js +1 -0
  49. package/lib/Types/Signal.js +1 -0
  50. package/lib/Types/Socket.js +2 -0
  51. package/lib/Types/State.js +12 -0
  52. package/lib/Types/USync.js +1 -0
  53. package/lib/Types/index.js +25 -0
  54. package/lib/Utils/auth-utils.js +289 -0
  55. package/lib/Utils/browser-utils.js +28 -0
  56. package/lib/Utils/business.js +230 -0
  57. package/lib/Utils/chat-utils.js +811 -0
  58. package/lib/Utils/crypto.js +117 -0
  59. package/lib/Utils/decode-wa-message.js +282 -0
  60. package/lib/Utils/event-buffer.js +573 -0
  61. package/lib/Utils/generics.js +385 -0
  62. package/lib/Utils/history.js +130 -0
  63. package/lib/Utils/identity-change-handler.js +48 -0
  64. package/lib/Utils/index.js +19 -0
  65. package/lib/Utils/link-preview.js +84 -0
  66. package/lib/Utils/logger.js +2 -0
  67. package/lib/Utils/lt-hash.js +7 -0
  68. package/lib/Utils/make-mutex.js +32 -0
  69. package/lib/Utils/message-retry-manager.js +224 -0
  70. package/lib/Utils/messages-media.js +789 -0
  71. package/lib/Utils/messages.js +1832 -0
  72. package/lib/Utils/noise-handler.js +200 -0
  73. package/lib/Utils/pre-key-manager.js +105 -0
  74. package/lib/Utils/process-message.js +527 -0
  75. package/lib/Utils/reporting-utils.js +257 -0
  76. package/lib/Utils/signal.js +158 -0
  77. package/lib/Utils/sync-action-utils.js +47 -0
  78. package/lib/Utils/tc-token-utils.js +17 -0
  79. package/lib/Utils/use-multi-file-auth-state.js +120 -0
  80. package/lib/Utils/validate-connection.js +206 -0
  81. package/lib/WABinary/constants.js +1300 -0
  82. package/lib/WABinary/decode.js +261 -0
  83. package/lib/WABinary/encode.js +219 -0
  84. package/lib/WABinary/generic-utils.js +197 -0
  85. package/lib/WABinary/index.js +5 -0
  86. package/lib/WABinary/jid-utils.js +95 -0
  87. package/lib/WABinary/types.js +1 -0
  88. package/lib/WAM/BinaryInfo.js +9 -0
  89. package/lib/WAM/constants.js +22852 -0
  90. package/lib/WAM/encode.js +149 -0
  91. package/lib/WAM/index.js +3 -0
  92. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -0
  93. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
  94. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
  95. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
  96. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
  97. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  98. package/lib/WAUSync/Protocols/index.js +4 -0
  99. package/lib/WAUSync/USyncQuery.js +93 -0
  100. package/lib/WAUSync/USyncUser.js +22 -0
  101. package/lib/WAUSync/index.js +3 -0
  102. package/lib/index.js +11 -0
  103. package/package.json +72 -0
@@ -0,0 +1,53 @@
1
+ import WebSocket from 'ws';
2
+ import { DEFAULT_ORIGIN } from '../../Defaults/index.js';
3
+ import { AbstractSocketClient } from './types.js';
4
+ export class WebSocketClient extends AbstractSocketClient {
5
+ constructor() {
6
+ super(...arguments);
7
+ this.socket = null;
8
+ }
9
+ get isOpen() {
10
+ return this.socket?.readyState === WebSocket.OPEN;
11
+ }
12
+ get isClosed() {
13
+ return this.socket === null || this.socket?.readyState === WebSocket.CLOSED;
14
+ }
15
+ get isClosing() {
16
+ return this.socket === null || this.socket?.readyState === WebSocket.CLOSING;
17
+ }
18
+ get isConnecting() {
19
+ return this.socket?.readyState === WebSocket.CONNECTING;
20
+ }
21
+ connect() {
22
+ if (this.socket) {
23
+ return;
24
+ }
25
+ this.socket = new WebSocket(this.url, {
26
+ origin: DEFAULT_ORIGIN,
27
+ headers: this.config.options?.headers,
28
+ handshakeTimeout: this.config.connectTimeoutMs,
29
+ timeout: this.config.connectTimeoutMs,
30
+ agent: this.config.agent
31
+ });
32
+ this.socket.setMaxListeners(0);
33
+ const events = ['close', 'error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response'];
34
+ for (const event of events) {
35
+ this.socket?.on(event, (...args) => this.emit(event, ...args));
36
+ }
37
+ }
38
+ async close() {
39
+ if (!this.socket) {
40
+ return;
41
+ }
42
+ const closePromise = new Promise(resolve => {
43
+ this.socket?.once('close', resolve);
44
+ });
45
+ this.socket.close();
46
+ await closePromise;
47
+ this.socket = null;
48
+ }
49
+ send(str, cb) {
50
+ this.socket?.send(str, cb);
51
+ return Boolean(this.socket);
52
+ }
53
+ }
@@ -0,0 +1,378 @@
1
+ import { getRawMediaUploadData } from '../Utils/index.js';
2
+ import { parseCatalogNode, parseCollectionsNode, parseOrderDetailsNode, parseProductNode, toProductNode, uploadingNecessaryImagesOfProduct } from '../Utils/business.js';
3
+ import { jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary/index.js';
4
+ import { getBinaryNodeChild } from '../WABinary/generic-utils.js';
5
+ import { makeMessagesRecvSocket } from './messages-recv.js';
6
+ export const makeBusinessSocket = (config) => {
7
+ const sock = makeMessagesRecvSocket(config);
8
+ const { authState, query, waUploadToServer } = sock;
9
+ const updateBussinesProfile = async (args) => {
10
+ const node = [];
11
+ const simpleFields = ['address', 'email', 'description'];
12
+ node.push(...simpleFields
13
+ .filter(key => args[key] !== undefined && args[key] !== null)
14
+ .map(key => ({
15
+ tag: key,
16
+ attrs: {},
17
+ content: args[key]
18
+ })));
19
+ if (args.websites !== undefined) {
20
+ node.push(...args.websites.map(website => ({
21
+ tag: 'website',
22
+ attrs: {},
23
+ content: website
24
+ })));
25
+ }
26
+ if (args.hours !== undefined) {
27
+ node.push({
28
+ tag: 'business_hours',
29
+ attrs: { timezone: args.hours.timezone },
30
+ content: args.hours.days.map(dayConfig => {
31
+ const base = {
32
+ tag: 'business_hours_config',
33
+ attrs: {
34
+ day_of_week: dayConfig.day,
35
+ mode: dayConfig.mode
36
+ }
37
+ };
38
+ if (dayConfig.mode === 'specific_hours') {
39
+ return {
40
+ ...base,
41
+ attrs: {
42
+ ...base.attrs,
43
+ open_time: dayConfig.openTimeInMinutes,
44
+ close_time: dayConfig.closeTimeInMinutes
45
+ }
46
+ };
47
+ }
48
+ return base;
49
+ })
50
+ });
51
+ }
52
+ const result = await query({
53
+ tag: 'iq',
54
+ attrs: {
55
+ to: S_WHATSAPP_NET,
56
+ type: 'set',
57
+ xmlns: 'w:biz'
58
+ },
59
+ content: [
60
+ {
61
+ tag: 'business_profile',
62
+ attrs: {
63
+ v: '3',
64
+ mutation_type: 'delta'
65
+ },
66
+ content: node
67
+ }
68
+ ]
69
+ });
70
+ return result;
71
+ };
72
+ const updateCoverPhoto = async (photo) => {
73
+ const { fileSha256, filePath } = await getRawMediaUploadData(photo, 'biz-cover-photo');
74
+ const fileSha256B64 = fileSha256.toString('base64');
75
+ const { meta_hmac, fbid, ts } = await waUploadToServer(filePath, {
76
+ fileEncSha256B64: fileSha256B64,
77
+ mediaType: 'biz-cover-photo'
78
+ });
79
+ await query({
80
+ tag: 'iq',
81
+ attrs: {
82
+ to: S_WHATSAPP_NET,
83
+ type: 'set',
84
+ xmlns: 'w:biz'
85
+ },
86
+ content: [
87
+ {
88
+ tag: 'business_profile',
89
+ attrs: {
90
+ v: '3',
91
+ mutation_type: 'delta'
92
+ },
93
+ content: [
94
+ {
95
+ tag: 'cover_photo',
96
+ attrs: { id: String(fbid), op: 'update', token: meta_hmac, ts: String(ts) }
97
+ }
98
+ ]
99
+ }
100
+ ]
101
+ });
102
+ return fbid;
103
+ };
104
+ const removeCoverPhoto = async (id) => {
105
+ return await query({
106
+ tag: 'iq',
107
+ attrs: {
108
+ to: S_WHATSAPP_NET,
109
+ type: 'set',
110
+ xmlns: 'w:biz'
111
+ },
112
+ content: [
113
+ {
114
+ tag: 'business_profile',
115
+ attrs: {
116
+ v: '3',
117
+ mutation_type: 'delta'
118
+ },
119
+ content: [
120
+ {
121
+ tag: 'cover_photo',
122
+ attrs: { op: 'delete', id }
123
+ }
124
+ ]
125
+ }
126
+ ]
127
+ });
128
+ };
129
+ const getCatalog = async ({ jid, limit, cursor }) => {
130
+ jid = jid || authState.creds.me?.id;
131
+ jid = jidNormalizedUser(jid);
132
+ const queryParamNodes = [
133
+ {
134
+ tag: 'limit',
135
+ attrs: {},
136
+ content: Buffer.from((limit || 10).toString())
137
+ },
138
+ {
139
+ tag: 'width',
140
+ attrs: {},
141
+ content: Buffer.from('100')
142
+ },
143
+ {
144
+ tag: 'height',
145
+ attrs: {},
146
+ content: Buffer.from('100')
147
+ }
148
+ ];
149
+ if (cursor) {
150
+ queryParamNodes.push({
151
+ tag: 'after',
152
+ attrs: {},
153
+ content: cursor
154
+ });
155
+ }
156
+ const result = await query({
157
+ tag: 'iq',
158
+ attrs: {
159
+ to: S_WHATSAPP_NET,
160
+ type: 'get',
161
+ xmlns: 'w:biz:catalog'
162
+ },
163
+ content: [
164
+ {
165
+ tag: 'product_catalog',
166
+ attrs: {
167
+ jid,
168
+ allow_shop_source: 'true'
169
+ },
170
+ content: queryParamNodes
171
+ }
172
+ ]
173
+ });
174
+ return parseCatalogNode(result);
175
+ };
176
+ const getCollections = async (jid, limit = 51) => {
177
+ jid = jid || authState.creds.me?.id;
178
+ jid = jidNormalizedUser(jid);
179
+ const result = await query({
180
+ tag: 'iq',
181
+ attrs: {
182
+ to: S_WHATSAPP_NET,
183
+ type: 'get',
184
+ xmlns: 'w:biz:catalog',
185
+ smax_id: '35'
186
+ },
187
+ content: [
188
+ {
189
+ tag: 'collections',
190
+ attrs: {
191
+ biz_jid: jid
192
+ },
193
+ content: [
194
+ {
195
+ tag: 'collection_limit',
196
+ attrs: {},
197
+ content: Buffer.from(limit.toString())
198
+ },
199
+ {
200
+ tag: 'item_limit',
201
+ attrs: {},
202
+ content: Buffer.from(limit.toString())
203
+ },
204
+ {
205
+ tag: 'width',
206
+ attrs: {},
207
+ content: Buffer.from('100')
208
+ },
209
+ {
210
+ tag: 'height',
211
+ attrs: {},
212
+ content: Buffer.from('100')
213
+ }
214
+ ]
215
+ }
216
+ ]
217
+ });
218
+ return parseCollectionsNode(result);
219
+ };
220
+ const getOrderDetails = async (orderId, tokenBase64) => {
221
+ const result = await query({
222
+ tag: 'iq',
223
+ attrs: {
224
+ to: S_WHATSAPP_NET,
225
+ type: 'get',
226
+ xmlns: 'fb:thrift_iq',
227
+ smax_id: '5'
228
+ },
229
+ content: [
230
+ {
231
+ tag: 'order',
232
+ attrs: {
233
+ op: 'get',
234
+ id: orderId
235
+ },
236
+ content: [
237
+ {
238
+ tag: 'image_dimensions',
239
+ attrs: {},
240
+ content: [
241
+ {
242
+ tag: 'width',
243
+ attrs: {},
244
+ content: Buffer.from('100')
245
+ },
246
+ {
247
+ tag: 'height',
248
+ attrs: {},
249
+ content: Buffer.from('100')
250
+ }
251
+ ]
252
+ },
253
+ {
254
+ tag: 'token',
255
+ attrs: {},
256
+ content: Buffer.from(tokenBase64)
257
+ }
258
+ ]
259
+ }
260
+ ]
261
+ });
262
+ return parseOrderDetailsNode(result);
263
+ };
264
+ const productUpdate = async (productId, update) => {
265
+ update = await uploadingNecessaryImagesOfProduct(update, waUploadToServer);
266
+ const editNode = toProductNode(productId, update);
267
+ const result = await query({
268
+ tag: 'iq',
269
+ attrs: {
270
+ to: S_WHATSAPP_NET,
271
+ type: 'set',
272
+ xmlns: 'w:biz:catalog'
273
+ },
274
+ content: [
275
+ {
276
+ tag: 'product_catalog_edit',
277
+ attrs: { v: '1' },
278
+ content: [
279
+ editNode,
280
+ {
281
+ tag: 'width',
282
+ attrs: {},
283
+ content: '100'
284
+ },
285
+ {
286
+ tag: 'height',
287
+ attrs: {},
288
+ content: '100'
289
+ }
290
+ ]
291
+ }
292
+ ]
293
+ });
294
+ const productCatalogEditNode = getBinaryNodeChild(result, 'product_catalog_edit');
295
+ const productNode = getBinaryNodeChild(productCatalogEditNode, 'product');
296
+ return parseProductNode(productNode);
297
+ };
298
+ const productCreate = async (create) => {
299
+ // ensure isHidden is defined
300
+ create.isHidden = !!create.isHidden;
301
+ create = await uploadingNecessaryImagesOfProduct(create, waUploadToServer);
302
+ const createNode = toProductNode(undefined, create);
303
+ const result = await query({
304
+ tag: 'iq',
305
+ attrs: {
306
+ to: S_WHATSAPP_NET,
307
+ type: 'set',
308
+ xmlns: 'w:biz:catalog'
309
+ },
310
+ content: [
311
+ {
312
+ tag: 'product_catalog_add',
313
+ attrs: { v: '1' },
314
+ content: [
315
+ createNode,
316
+ {
317
+ tag: 'width',
318
+ attrs: {},
319
+ content: '100'
320
+ },
321
+ {
322
+ tag: 'height',
323
+ attrs: {},
324
+ content: '100'
325
+ }
326
+ ]
327
+ }
328
+ ]
329
+ });
330
+ const productCatalogAddNode = getBinaryNodeChild(result, 'product_catalog_add');
331
+ const productNode = getBinaryNodeChild(productCatalogAddNode, 'product');
332
+ return parseProductNode(productNode);
333
+ };
334
+ const productDelete = async (productIds) => {
335
+ const result = await query({
336
+ tag: 'iq',
337
+ attrs: {
338
+ to: S_WHATSAPP_NET,
339
+ type: 'set',
340
+ xmlns: 'w:biz:catalog'
341
+ },
342
+ content: [
343
+ {
344
+ tag: 'product_catalog_delete',
345
+ attrs: { v: '1' },
346
+ content: productIds.map(id => ({
347
+ tag: 'product',
348
+ attrs: {},
349
+ content: [
350
+ {
351
+ tag: 'id',
352
+ attrs: {},
353
+ content: Buffer.from(id)
354
+ }
355
+ ]
356
+ }))
357
+ }
358
+ ]
359
+ });
360
+ const productCatalogDelNode = getBinaryNodeChild(result, 'product_catalog_delete');
361
+ return {
362
+ deleted: +(productCatalogDelNode?.attrs.deleted_count || 0)
363
+ };
364
+ };
365
+ return {
366
+ ...sock,
367
+ logger: config.logger,
368
+ getOrderDetails,
369
+ getCatalog,
370
+ getCollections,
371
+ productCreate,
372
+ productDelete,
373
+ productUpdate,
374
+ updateBussinesProfile,
375
+ updateCoverPhoto,
376
+ removeCoverPhoto
377
+ };
378
+ };