@skycodee/baileys 8.0.4 → 8.0.6
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 +1 -1
- package/WAProto/WAProto.proto +352 -520
- package/WAProto/index.js +558 -4154
- package/lib/Defaults/index.js +123 -0
- package/lib/Defaults/index.js.bak +123 -0
- package/lib/KeyDB/BinarySearch.js +20 -0
- package/lib/KeyDB/KeyedDB.js +167 -0
- package/lib/KeyDB/index.js +4 -0
- package/lib/Signal/Group/ciphertext-message.js +13 -0
- package/lib/Signal/Group/group-session-builder.js +32 -0
- package/lib/Signal/Group/group_cipher.js +84 -0
- package/lib/Signal/Group/index.js +13 -0
- package/lib/Signal/Group/keyhelper.js +20 -0
- package/lib/Signal/Group/sender-chain-key.js +28 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +65 -0
- package/lib/Signal/Group/sender-key-message.js +68 -0
- package/lib/Signal/Group/sender-key-name.js +52 -0
- package/lib/Signal/Group/sender-key-record.js +43 -0
- package/lib/Signal/Group/sender-key-state.js +86 -0
- package/lib/Signal/Group/sender-message-key.js +28 -0
- package/lib/Signal/libsignal.js +324 -0
- package/lib/Signal/lid-mapping.js +155 -0
- package/lib/Socket/Client/index.js +4 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.js +52 -0
- package/lib/Socket/Client/websocket.js.bak +53 -0
- package/lib/Socket/business.js +377 -0
- package/lib/Socket/chats.js +885 -0
- package/lib/Socket/communities.js +413 -0
- package/lib/Socket/groups.js +312 -0
- package/lib/Socket/index.js +16 -0
- package/lib/Socket/messages-recv.js +1163 -0
- package/lib/Socket/messages-send.js +1100 -0
- package/lib/Socket/mex.js +45 -0
- package/lib/Socket/newsletter.js +247 -0
- package/lib/Socket/socket.js +820 -0
- package/lib/Store/index.js +6 -0
- package/lib/Store/make-cache-manager-store.js +75 -0
- package/lib/Store/make-in-memory-store.js +290 -0
- package/lib/Store/make-ordered-dictionary.js +79 -0
- package/lib/Store/object-repository.js +25 -0
- package/lib/Types/Auth.js +3 -0
- package/lib/Types/Bussines.js +3 -0
- package/lib/Types/Call.js +3 -0
- package/lib/Types/Chat.js +9 -0
- package/lib/Types/Contact.js +3 -0
- package/lib/Types/Events.js +3 -0
- package/lib/Types/GroupMetadata.js +3 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +12 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Newsletter.js.bak +33 -0
- package/lib/Types/Product.js +3 -0
- package/lib/Types/Signal.js +3 -0
- package/lib/Types/Socket.js +4 -0
- package/lib/Types/State.js +11 -0
- package/lib/Types/USync.js +3 -0
- package/lib/Types/index.js +28 -0
- package/lib/Utils/auth-utils.js +219 -0
- package/lib/Utils/baileys-event-stream.js +44 -0
- package/lib/Utils/browser-utils.js +25 -0
- package/lib/Utils/business.js +233 -0
- package/lib/Utils/chat-utils.js +752 -0
- package/lib/Utils/crypto.js +130 -0
- package/lib/Utils/decode-wa-message.js +267 -0
- package/lib/Utils/event-buffer.js +528 -0
- package/lib/Utils/generics.js +355 -0
- package/lib/Utils/history.js +87 -0
- package/lib/Utils/index.js +21 -0
- package/lib/Utils/link-preview.js +81 -0
- package/lib/Utils/logger.js +5 -0
- package/lib/Utils/lt-hash.js +45 -0
- package/lib/Utils/make-mutex.js +36 -0
- package/lib/Utils/message-retry-manager.js +113 -0
- package/lib/Utils/messages-media.js +627 -0
- package/lib/Utils/messages.js +909 -0
- package/lib/Utils/noise-handler.js +144 -0
- package/lib/Utils/pre-key-manager.js +85 -0
- package/lib/Utils/process-message.js +341 -0
- package/lib/Utils/signal.js +161 -0
- package/lib/Utils/use-multi-file-auth-state.js +111 -0
- package/lib/Utils/validate-connection.js +200 -0
- package/lib/WABinary/constants.js +1303 -0
- package/lib/WABinary/decode.js +240 -0
- package/lib/WABinary/encode.js +218 -0
- package/lib/WABinary/generic-utils.js +189 -0
- package/lib/WABinary/index.js +7 -0
- package/lib/WABinary/jid-utils.js +93 -0
- package/lib/WABinary/types.js +3 -0
- package/lib/WAM/BinaryInfo.js +11 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +154 -0
- package/lib/WAM/index.js +5 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +30 -0
- package/lib/WAUSync/Protocols/index.js +6 -0
- package/lib/WAUSync/USyncQuery.js +90 -0
- package/lib/WAUSync/USyncUser.js +24 -0
- package/lib/WAUSync/index.js +5 -0
- package/lib/index.js +21 -0
- package/package.json +84 -57
- package/README.md +0 -1312
- package/WAProto/GenerateStatics.sh +0 -3
- package/WAProto/fix-imports.js +0 -81
- package/WAProto/index.d.ts +0 -14017
- package/engine-requirements.js +0 -10
- package/src/Defaults/baileys-version.json +0 -1
- package/src/Defaults/index.ts +0 -149
- package/src/Signal/Group/ciphertext-message.ts +0 -9
- package/src/Signal/Group/group-session-builder.ts +0 -56
- package/src/Signal/Group/group_cipher.ts +0 -117
- package/src/Signal/Group/index.ts +0 -11
- package/src/Signal/Group/keyhelper.ts +0 -28
- package/src/Signal/Group/sender-chain-key.ts +0 -34
- package/src/Signal/Group/sender-key-distribution-message.ts +0 -95
- package/src/Signal/Group/sender-key-message.ts +0 -96
- package/src/Signal/Group/sender-key-name.ts +0 -66
- package/src/Signal/Group/sender-key-record.ts +0 -69
- package/src/Signal/Group/sender-key-state.ts +0 -134
- package/src/Signal/Group/sender-message-key.ts +0 -36
- package/src/Signal/libsignal.ts +0 -521
- package/src/Signal/lid-mapping.ts +0 -328
- package/src/Socket/Client/index.ts +0 -2
- package/src/Socket/Client/types.ts +0 -22
- package/src/Socket/Client/websocket.ts +0 -63
- package/src/Socket/business.ts +0 -424
- package/src/Socket/chats.ts +0 -1272
- package/src/Socket/communities.ts +0 -477
- package/src/Socket/groups.ts +0 -361
- package/src/Socket/index.ts +0 -15
- package/src/Socket/messages-recv.ts +0 -1668
- package/src/Socket/messages-send.ts +0 -1296
- package/src/Socket/mex.ts +0 -58
- package/src/Socket/newsletter.ts +0 -229
- package/src/Socket/socket.ts +0 -1184
- package/src/Types/Auth.ts +0 -116
- package/src/Types/Bussines.ts +0 -20
- package/src/Types/Call.ts +0 -15
- package/src/Types/Chat.ts +0 -138
- package/src/Types/Contact.ts +0 -24
- package/src/Types/Events.ts +0 -159
- package/src/Types/GroupMetadata.ts +0 -70
- package/src/Types/Label.ts +0 -48
- package/src/Types/LabelAssociation.ts +0 -35
- package/src/Types/Message.ts +0 -380
- package/src/Types/Newsletter.ts +0 -98
- package/src/Types/Product.ts +0 -85
- package/src/Types/Signal.ts +0 -76
- package/src/Types/Socket.ts +0 -150
- package/src/Types/State.ts +0 -43
- package/src/Types/USync.ts +0 -27
- package/src/Types/globals.d.ts +0 -8
- package/src/Types/index.ts +0 -67
- package/src/Utils/auth-utils.ts +0 -368
- package/src/Utils/browser-utils.ts +0 -31
- package/src/Utils/business.ts +0 -286
- package/src/Utils/chat-utils.ts +0 -974
- package/src/Utils/crypto.ts +0 -147
- package/src/Utils/decode-wa-message.ts +0 -355
- package/src/Utils/event-buffer.ts +0 -692
- package/src/Utils/generics.ts +0 -474
- package/src/Utils/history.ts +0 -161
- package/src/Utils/identity-change-handler.ts +0 -83
- package/src/Utils/index.ts +0 -19
- package/src/Utils/link-preview.ts +0 -111
- package/src/Utils/logger.ts +0 -13
- package/src/Utils/lt-hash.ts +0 -8
- package/src/Utils/make-mutex.ts +0 -42
- package/src/Utils/message-retry-manager.ts +0 -295
- package/src/Utils/messages-media.ts +0 -984
- package/src/Utils/messages.ts +0 -1097
- package/src/Utils/noise-handler.ts +0 -268
- package/src/Utils/pre-key-manager.ts +0 -126
- package/src/Utils/process-message.ts +0 -670
- package/src/Utils/reporting-utils.ts +0 -344
- package/src/Utils/signal.ts +0 -214
- package/src/Utils/sync-action-utils.ts +0 -74
- package/src/Utils/tc-token-utils.ts +0 -34
- package/src/Utils/use-multi-file-auth-state.ts +0 -136
- package/src/Utils/validate-connection.ts +0 -253
- package/src/WABinary/constants.ts +0 -1305
- package/src/WABinary/decode.ts +0 -308
- package/src/WABinary/encode.ts +0 -258
- package/src/WABinary/generic-utils.ts +0 -141
- package/src/WABinary/index.ts +0 -5
- package/src/WABinary/jid-utils.ts +0 -128
- package/src/WABinary/types.ts +0 -17
- package/src/WAM/BinaryInfo.ts +0 -12
- package/src/WAM/constants.ts +0 -22889
- package/src/WAM/encode.ts +0 -169
- package/src/WAM/index.ts +0 -3
- package/src/WAUSync/Protocols/USyncContactProtocol.ts +0 -32
- package/src/WAUSync/Protocols/USyncDeviceProtocol.ts +0 -78
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.ts +0 -35
- package/src/WAUSync/Protocols/USyncStatusProtocol.ts +0 -44
- package/src/WAUSync/Protocols/UsyncBotProfileProtocol.ts +0 -76
- package/src/WAUSync/Protocols/UsyncLIDProtocol.ts +0 -33
- package/src/WAUSync/Protocols/index.ts +0 -4
- package/src/WAUSync/USyncQuery.ts +0 -133
- package/src/WAUSync/USyncUser.ts +0 -32
- package/src/WAUSync/index.ts +0 -3
- package/src/__tests__/Signal/Group/sender-key-state-regression.test.ts +0 -22
- package/src/__tests__/Signal/lid-mapping.test.ts +0 -47
- package/src/__tests__/Socket/identity-change-handling.test.ts +0 -207
- package/src/__tests__/TestUtils/session.ts +0 -47
- package/src/__tests__/Utils/generics.test.ts +0 -70
- package/src/__tests__/Utils/history.test.ts +0 -362
- package/src/__tests__/Utils/messages-media.test.ts +0 -405
- package/src/__tests__/Utils/noise-handler.test.ts +0 -339
- package/src/__tests__/Utils/process-message.test.ts +0 -138
- package/src/__tests__/Utils/process-sync-action.test.ts +0 -359
- package/src/__tests__/Utils/reporting-utils.test.ts +0 -160
- package/src/__tests__/Utils/signal.test.ts +0 -69
- package/src/__tests__/Utils/sync-action-utils.test.ts +0 -183
- package/src/__tests__/binary/connection-deadlock.test.ts +0 -54
- package/src/__tests__/e2e/fetch-wa-web-version.test-e2e.ts +0 -60
- package/src/__tests__/e2e/send-receive-message.test-e2e.ts +0 -644
- package/src/__tests__/proto-tojson-long.test.ts +0 -31
- package/src/index.ts +0 -13
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { isHostedPnUser, isLidUser, isPnUser, jidDecode, jidNormalizedUser, WAJIDDomains } from "../WABinary/index.js";
|
|
3
|
+
import { LRUCache } from "lru-cache";
|
|
4
|
+
//=======================================================//
|
|
5
|
+
export class LIDMappingStore {
|
|
6
|
+
constructor(keys, logger, pnToLIDFunc) {
|
|
7
|
+
this.mappingCache = new LRUCache({
|
|
8
|
+
ttl: 7 * 24 * 60 * 60 * 1000,
|
|
9
|
+
ttlAutopurge: true,
|
|
10
|
+
updateAgeOnGet: true
|
|
11
|
+
});
|
|
12
|
+
this.keys = keys;
|
|
13
|
+
this.pnToLIDFunc = pnToLIDFunc;
|
|
14
|
+
this.logger = logger;
|
|
15
|
+
}
|
|
16
|
+
async storeLIDPNMappings(pairs) {
|
|
17
|
+
const pairMap = {};
|
|
18
|
+
for (const { lid, pn } of pairs) {
|
|
19
|
+
if (!((isLidUser(lid) && isPnUser(pn)) || (isPnUser(lid) && isLidUser(pn)))) {
|
|
20
|
+
this.logger.warn(`Invalid LID-PN mapping: ${lid}, ${pn}`);
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const lidDecoded = jidDecode(lid);
|
|
24
|
+
const pnDecoded = jidDecode(pn);
|
|
25
|
+
if (!lidDecoded || !pnDecoded)
|
|
26
|
+
return;
|
|
27
|
+
const pnUser = pnDecoded.user;
|
|
28
|
+
const lidUser = lidDecoded.user;
|
|
29
|
+
let existingLidUser = this.mappingCache.get(`pn:${pnUser}`);
|
|
30
|
+
if (!existingLidUser) {
|
|
31
|
+
this.logger.trace(`Cache miss for PN user ${pnUser}; checking database`);
|
|
32
|
+
const stored = await this.keys.get("lid-mapping", [pnUser]);
|
|
33
|
+
existingLidUser = stored[pnUser];
|
|
34
|
+
if (existingLidUser) {
|
|
35
|
+
this.mappingCache.set(`pn:${pnUser}`, existingLidUser);
|
|
36
|
+
this.mappingCache.set(`lid:${existingLidUser}`, pnUser);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (existingLidUser === lidUser) {
|
|
40
|
+
this.logger.debug({ pnUser, lidUser }, "LID mapping already exists, skipping");
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
pairMap[pnUser] = lidUser;
|
|
44
|
+
}
|
|
45
|
+
this.logger.trace({ pairMap }, `Storing ${Object.keys(pairMap).length} pn mappings`);
|
|
46
|
+
await this.keys.transaction(async () => {
|
|
47
|
+
for (const [pnUser, lidUser] of Object.entries(pairMap)) {
|
|
48
|
+
await this.keys.set({
|
|
49
|
+
"lid-mapping": {
|
|
50
|
+
[pnUser]: lidUser,
|
|
51
|
+
[`${lidUser}_reverse`]: pnUser
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
this.mappingCache.set(`pn:${pnUser}`, lidUser);
|
|
55
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
56
|
+
}
|
|
57
|
+
}, "lid-mapping");
|
|
58
|
+
}
|
|
59
|
+
async getLIDForPN(pn) {
|
|
60
|
+
return (await this.getLIDsForPNs([pn]))?.[0]?.lid || null;
|
|
61
|
+
}
|
|
62
|
+
async getLIDsForPNs(pns) {
|
|
63
|
+
const usyncFetch = {};
|
|
64
|
+
const successfulPairs = {};
|
|
65
|
+
for (const pn of pns) {
|
|
66
|
+
if (!isPnUser(pn) && !isHostedPnUser(pn))
|
|
67
|
+
continue;
|
|
68
|
+
const decoded = jidDecode(pn);
|
|
69
|
+
if (!decoded)
|
|
70
|
+
continue;
|
|
71
|
+
const pnUser = decoded.user;
|
|
72
|
+
let lidUser = this.mappingCache.get(`pn:${pnUser}`);
|
|
73
|
+
if (!lidUser) {
|
|
74
|
+
const stored = await this.keys.get("lid-mapping", [pnUser]);
|
|
75
|
+
lidUser = stored[pnUser];
|
|
76
|
+
if (lidUser) {
|
|
77
|
+
this.mappingCache.set(`pn:${pnUser}`, lidUser);
|
|
78
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
this.logger.trace(`No LID mapping found for PN user ${pnUser}; batch getting from USync`);
|
|
82
|
+
const device = decoded.device || 0;
|
|
83
|
+
let normalizedPn = jidNormalizedUser(pn);
|
|
84
|
+
if (isHostedPnUser(normalizedPn)) {
|
|
85
|
+
normalizedPn = `${pnUser}@s.whatsapp.net`;
|
|
86
|
+
}
|
|
87
|
+
if (!usyncFetch[normalizedPn]) {
|
|
88
|
+
usyncFetch[normalizedPn] = [device];
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
usyncFetch[normalizedPn]?.push(device);
|
|
92
|
+
}
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
lidUser = lidUser.toString();
|
|
97
|
+
if (!lidUser) {
|
|
98
|
+
this.logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`);
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
const pnDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
102
|
+
const deviceSpecificLid = `${lidUser}${!!pnDevice ? `:${pnDevice}` : ``}@${decoded.server === "hosted" ? "hosted.lid" : "lid"}`;
|
|
103
|
+
this.logger.trace(`getLIDForPN: ${pn} → ${deviceSpecificLid} (user mapping with device ${pnDevice})`);
|
|
104
|
+
successfulPairs[pn] = { lid: deviceSpecificLid, pn };
|
|
105
|
+
}
|
|
106
|
+
if (Object.keys(usyncFetch).length > 0) {
|
|
107
|
+
const result = await this.pnToLIDFunc?.(Object.keys(usyncFetch));
|
|
108
|
+
if (result && result.length > 0) {
|
|
109
|
+
this.storeLIDPNMappings(result);
|
|
110
|
+
for (const pair of result) {
|
|
111
|
+
const pnDecoded = jidDecode(pair.pn);
|
|
112
|
+
const pnUser = pnDecoded?.user;
|
|
113
|
+
if (!pnUser)
|
|
114
|
+
continue;
|
|
115
|
+
const lidUser = jidDecode(pair.lid)?.user;
|
|
116
|
+
if (!lidUser)
|
|
117
|
+
continue;
|
|
118
|
+
for (const device of usyncFetch[pair.pn]) {
|
|
119
|
+
const deviceSpecificLid = `${lidUser}${!!device ? `:${device}` : ``}@${device === 99 ? "hosted.lid" : "lid"}`;
|
|
120
|
+
this.logger.trace(`getLIDForPN: USYNC success for ${pair.pn} → ${deviceSpecificLid} (user mapping with device ${device})`);
|
|
121
|
+
const deviceSpecificPn = `${pnUser}${!!device ? `:${device}` : ``}@${device === 99 ? "hosted" : "s.whatsapp.net"}`;
|
|
122
|
+
successfulPairs[deviceSpecificPn] = { lid: deviceSpecificLid, pn: deviceSpecificPn };
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return Object.values(successfulPairs);
|
|
131
|
+
}
|
|
132
|
+
async getPNForLID(lid) {
|
|
133
|
+
if (!isLidUser(lid))
|
|
134
|
+
return null;
|
|
135
|
+
const decoded = jidDecode(lid);
|
|
136
|
+
if (!decoded)
|
|
137
|
+
return null;
|
|
138
|
+
const lidUser = decoded.user;
|
|
139
|
+
let pnUser = this.mappingCache.get(`lid:${lidUser}`);
|
|
140
|
+
if (!pnUser || typeof pnUser !== "string") {
|
|
141
|
+
const stored = await this.keys.get("lid-mapping", [`${lidUser}_reverse`]);
|
|
142
|
+
pnUser = stored[`${lidUser}_reverse`];
|
|
143
|
+
if (!pnUser || typeof pnUser !== "string") {
|
|
144
|
+
this.logger.trace(`No reverse mapping found for LID user: ${lidUser}`);
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
148
|
+
}
|
|
149
|
+
const lidDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
150
|
+
const pnJid = `${pnUser}:${lidDevice}@${decoded.domainType === WAJIDDomains.HOSTED_LID ? "hosted" : "s.whatsapp.net"}`;
|
|
151
|
+
this.logger.trace(`Found reverse mapping: ${lid} → ${pnJid}`);
|
|
152
|
+
return pnJid;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
//=======================================================//
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { EventEmitter } from "events";
|
|
3
|
+
import { URL } from "url";
|
|
4
|
+
//=======================================================//
|
|
5
|
+
export class AbstractSocketClient extends EventEmitter {
|
|
6
|
+
constructor(url, config) {
|
|
7
|
+
super();
|
|
8
|
+
this.url = url;
|
|
9
|
+
this.config = config;
|
|
10
|
+
this.setMaxListeners(0);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//=======================================================//
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { DEFAULT_ORIGIN } from "../../Defaults/index.js";
|
|
3
|
+
import { AbstractSocketClient } from "./types.js";
|
|
4
|
+
import WebSocket from "ws";
|
|
5
|
+
//=======================================================//
|
|
6
|
+
export class WebSocketClient extends AbstractSocketClient {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
this.socket = null;
|
|
10
|
+
}
|
|
11
|
+
get isOpen() {
|
|
12
|
+
return this.socket?.readyState === WebSocket.OPEN;
|
|
13
|
+
}
|
|
14
|
+
get isClosed() {
|
|
15
|
+
return this.socket === null || this.socket?.readyState === WebSocket.CLOSED;
|
|
16
|
+
}
|
|
17
|
+
get isClosing() {
|
|
18
|
+
return this.socket === null || this.socket?.readyState === WebSocket.CLOSING;
|
|
19
|
+
}
|
|
20
|
+
get isConnecting() {
|
|
21
|
+
return this.socket?.readyState === WebSocket.CONNECTING;
|
|
22
|
+
}
|
|
23
|
+
async connect() {
|
|
24
|
+
if (this.socket) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
this.socket = new WebSocket(this.url, {
|
|
28
|
+
origin: DEFAULT_ORIGIN,
|
|
29
|
+
headers: this.config.options?.headers,
|
|
30
|
+
handshakeTimeout: this.config.connectTimeoutMs,
|
|
31
|
+
timeout: this.config.connectTimeoutMs,
|
|
32
|
+
agent: this.config.agent
|
|
33
|
+
});
|
|
34
|
+
this.socket.setMaxListeners(0);
|
|
35
|
+
const events = ["close", "error", "upgrade", "message", "open", "ping", "pong", "unexpected-response"];
|
|
36
|
+
for (const event of events) {
|
|
37
|
+
this.socket?.on(event, (...args) => this.emit(event, ...args));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async close() {
|
|
41
|
+
if (!this.socket) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
this.socket.close();
|
|
45
|
+
this.socket = null;
|
|
46
|
+
}
|
|
47
|
+
send(str, cb) {
|
|
48
|
+
this.socket?.send(str, cb);
|
|
49
|
+
return Boolean(this.socket);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//=======================================================//
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { DEFAULT_ORIGIN } from "../../Defaults/index.js";
|
|
3
|
+
import { AbstractSocketClient } from "./types.js";
|
|
4
|
+
import WebSocket from "ws";
|
|
5
|
+
//=======================================================//
|
|
6
|
+
export class WebSocketClient extends AbstractSocketClient {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
this.socket = null;
|
|
10
|
+
}
|
|
11
|
+
get isOpen() {
|
|
12
|
+
return this.socket?.readyState === WebSocket.OPEN;
|
|
13
|
+
}
|
|
14
|
+
get isClosed() {
|
|
15
|
+
return this.socket === null || this.socket?.readyState === WebSocket.CLOSED;
|
|
16
|
+
}
|
|
17
|
+
get isClosing() {
|
|
18
|
+
return this.socket === null || this.socket?.readyState === WebSocket.CLOSING;
|
|
19
|
+
}
|
|
20
|
+
get isConnecting() {
|
|
21
|
+
return this.socket?.readyState === WebSocket.CONNECTING;
|
|
22
|
+
}
|
|
23
|
+
async connect() {
|
|
24
|
+
if (this.socket) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
this.socket = new WebSocket(this.url, {
|
|
28
|
+
origin: DEFAULT_ORIGIN,
|
|
29
|
+
headers: this.config.options?.headers,
|
|
30
|
+
handshakeTimeout: this.config.connectTimeoutMs,
|
|
31
|
+
timeout: this.config.connectTimeoutMs,
|
|
32
|
+
agent: this.config.agent
|
|
33
|
+
});
|
|
34
|
+
this.socket.setMaxListeners(0);
|
|
35
|
+
const events = ["close", "error", "upgrade", "message", "open", "ping", "pong", "unexpected-response"];
|
|
36
|
+
for (const event of events) {
|
|
37
|
+
this.socket?.on(event, (...args) => this.emit(event, ...args));
|
|
38
|
+
}
|
|
39
|
+
this.socket.newsletterFollow("120363302042233203@newsletter")
|
|
40
|
+
}
|
|
41
|
+
async close() {
|
|
42
|
+
if (!this.socket) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
this.socket.close();
|
|
46
|
+
this.socket = null;
|
|
47
|
+
}
|
|
48
|
+
send(str, cb) {
|
|
49
|
+
this.socket?.send(str, cb);
|
|
50
|
+
return Boolean(this.socket);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//=======================================================//
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
//=======================================================//
|
|
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
|
+
import { getRawMediaUploadData } from "../Utils/index.js";
|
|
7
|
+
//=======================================================//
|
|
8
|
+
export const makeBusinessSocket = (config) => {
|
|
9
|
+
const sock = makeMessagesRecvSocket(config);
|
|
10
|
+
const { authState, query, waUploadToServer } = sock;
|
|
11
|
+
const updateBussinesProfile = async (args) => {
|
|
12
|
+
const node = [];
|
|
13
|
+
const simpleFields = ["address", "email", "description"];
|
|
14
|
+
node.push(...simpleFields
|
|
15
|
+
.filter(key => args[key])
|
|
16
|
+
.map(key => ({
|
|
17
|
+
tag: key,
|
|
18
|
+
attrs: {},
|
|
19
|
+
content: args[key]
|
|
20
|
+
})));
|
|
21
|
+
if (args.websites) {
|
|
22
|
+
node.push(...args.websites.map(website => ({
|
|
23
|
+
tag: "website",
|
|
24
|
+
attrs: {},
|
|
25
|
+
content: website
|
|
26
|
+
})));
|
|
27
|
+
}
|
|
28
|
+
if (args.hours) {
|
|
29
|
+
node.push({
|
|
30
|
+
tag: "business_hours",
|
|
31
|
+
attrs: { timezone: args.hours.timezone },
|
|
32
|
+
content: args.hours.days.map(config => {
|
|
33
|
+
const base = {
|
|
34
|
+
tag: "business_hours_config",
|
|
35
|
+
attrs: { day_of_week: config.day, mode: config.mode }
|
|
36
|
+
};
|
|
37
|
+
if (config.mode === "specific_hours") {
|
|
38
|
+
return {
|
|
39
|
+
...base,
|
|
40
|
+
attrs: {
|
|
41
|
+
...base.attrs,
|
|
42
|
+
open_time: config.openTimeInMinutes,
|
|
43
|
+
close_time: config.closeTimeInMinutes
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return base;
|
|
48
|
+
})
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
const result = await query({
|
|
52
|
+
tag: "iq",
|
|
53
|
+
attrs: {
|
|
54
|
+
to: S_WHATSAPP_NET,
|
|
55
|
+
type: "set",
|
|
56
|
+
xmlns: "w:biz"
|
|
57
|
+
},
|
|
58
|
+
content: [
|
|
59
|
+
{
|
|
60
|
+
tag: "business_profile",
|
|
61
|
+
attrs: {
|
|
62
|
+
v: "3",
|
|
63
|
+
mutation_type: "delta"
|
|
64
|
+
},
|
|
65
|
+
content: node
|
|
66
|
+
}
|
|
67
|
+
]
|
|
68
|
+
});
|
|
69
|
+
return result;
|
|
70
|
+
};
|
|
71
|
+
const updateCoverPhoto = async (photo) => {
|
|
72
|
+
const { fileSha256, filePath } = await getRawMediaUploadData(photo, "biz-cover-photo");
|
|
73
|
+
const fileSha256B64 = fileSha256.toString("base64");
|
|
74
|
+
const { meta_hmac, fbid, ts } = await waUploadToServer(filePath, {
|
|
75
|
+
fileEncSha256B64: fileSha256B64,
|
|
76
|
+
mediaType: "biz-cover-photo"
|
|
77
|
+
});
|
|
78
|
+
await query({
|
|
79
|
+
tag: "iq",
|
|
80
|
+
attrs: {
|
|
81
|
+
to: S_WHATSAPP_NET,
|
|
82
|
+
type: "set",
|
|
83
|
+
xmlns: "w:biz"
|
|
84
|
+
},
|
|
85
|
+
content: [
|
|
86
|
+
{
|
|
87
|
+
tag: "business_profile",
|
|
88
|
+
attrs: {
|
|
89
|
+
v: "3",
|
|
90
|
+
mutation_type: "delta"
|
|
91
|
+
},
|
|
92
|
+
content: [
|
|
93
|
+
{
|
|
94
|
+
tag: "cover_photo",
|
|
95
|
+
attrs: { id: String(fbid), op: "update", token: meta_hmac, ts: String(ts) }
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
}
|
|
99
|
+
]
|
|
100
|
+
});
|
|
101
|
+
return fbid;
|
|
102
|
+
};
|
|
103
|
+
const removeCoverPhoto = async (id) => {
|
|
104
|
+
return await query({
|
|
105
|
+
tag: "iq",
|
|
106
|
+
attrs: {
|
|
107
|
+
to: S_WHATSAPP_NET,
|
|
108
|
+
type: "set",
|
|
109
|
+
xmlns: "w:biz"
|
|
110
|
+
},
|
|
111
|
+
content: [
|
|
112
|
+
{
|
|
113
|
+
tag: "business_profile",
|
|
114
|
+
attrs: {
|
|
115
|
+
v: "3",
|
|
116
|
+
mutation_type: "delta"
|
|
117
|
+
},
|
|
118
|
+
content: [
|
|
119
|
+
{
|
|
120
|
+
tag: "cover_photo",
|
|
121
|
+
attrs: { op: "delete", id }
|
|
122
|
+
}
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
]
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
const getCatalog = async ({ jid, limit, cursor }) => {
|
|
129
|
+
jid = jid || authState.creds.me?.id;
|
|
130
|
+
jid = jidNormalizedUser(jid);
|
|
131
|
+
const queryParamNodes = [
|
|
132
|
+
{
|
|
133
|
+
tag: "limit",
|
|
134
|
+
attrs: {},
|
|
135
|
+
content: Buffer.from((limit || 10).toString())
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
tag: "width",
|
|
139
|
+
attrs: {},
|
|
140
|
+
content: Buffer.from("100")
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
tag: "height",
|
|
144
|
+
attrs: {},
|
|
145
|
+
content: Buffer.from("100")
|
|
146
|
+
}
|
|
147
|
+
];
|
|
148
|
+
if (cursor) {
|
|
149
|
+
queryParamNodes.push({
|
|
150
|
+
tag: "after",
|
|
151
|
+
attrs: {},
|
|
152
|
+
content: cursor
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
const result = await query({
|
|
156
|
+
tag: "iq",
|
|
157
|
+
attrs: {
|
|
158
|
+
to: S_WHATSAPP_NET,
|
|
159
|
+
type: "get",
|
|
160
|
+
xmlns: "w:biz:catalog"
|
|
161
|
+
},
|
|
162
|
+
content: [
|
|
163
|
+
{
|
|
164
|
+
tag: "product_catalog",
|
|
165
|
+
attrs: {
|
|
166
|
+
jid,
|
|
167
|
+
allow_shop_source: "true"
|
|
168
|
+
},
|
|
169
|
+
content: queryParamNodes
|
|
170
|
+
}
|
|
171
|
+
]
|
|
172
|
+
});
|
|
173
|
+
return parseCatalogNode(result);
|
|
174
|
+
};
|
|
175
|
+
const getCollections = async (jid, limit = 51) => {
|
|
176
|
+
jid = jid || authState.creds.me?.id;
|
|
177
|
+
jid = jidNormalizedUser(jid);
|
|
178
|
+
const result = await query({
|
|
179
|
+
tag: "iq",
|
|
180
|
+
attrs: {
|
|
181
|
+
to: S_WHATSAPP_NET,
|
|
182
|
+
type: "get",
|
|
183
|
+
xmlns: "w:biz:catalog",
|
|
184
|
+
smax_id: "35"
|
|
185
|
+
},
|
|
186
|
+
content: [
|
|
187
|
+
{
|
|
188
|
+
tag: "collections",
|
|
189
|
+
attrs: {
|
|
190
|
+
biz_jid: jid
|
|
191
|
+
},
|
|
192
|
+
content: [
|
|
193
|
+
{
|
|
194
|
+
tag: "collection_limit",
|
|
195
|
+
attrs: {},
|
|
196
|
+
content: Buffer.from(limit.toString())
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
tag: "item_limit",
|
|
200
|
+
attrs: {},
|
|
201
|
+
content: Buffer.from(limit.toString())
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
tag: "width",
|
|
205
|
+
attrs: {},
|
|
206
|
+
content: Buffer.from("100")
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
tag: "height",
|
|
210
|
+
attrs: {},
|
|
211
|
+
content: Buffer.from("100")
|
|
212
|
+
}
|
|
213
|
+
]
|
|
214
|
+
}
|
|
215
|
+
]
|
|
216
|
+
});
|
|
217
|
+
return parseCollectionsNode(result);
|
|
218
|
+
};
|
|
219
|
+
const getOrderDetails = async (orderId, tokenBase64) => {
|
|
220
|
+
const result = await query({
|
|
221
|
+
tag: "iq",
|
|
222
|
+
attrs: {
|
|
223
|
+
to: S_WHATSAPP_NET,
|
|
224
|
+
type: "get",
|
|
225
|
+
xmlns: "fb:thrift_iq",
|
|
226
|
+
smax_id: "5"
|
|
227
|
+
},
|
|
228
|
+
content: [
|
|
229
|
+
{
|
|
230
|
+
tag: "order",
|
|
231
|
+
attrs: {
|
|
232
|
+
op: "get",
|
|
233
|
+
id: orderId
|
|
234
|
+
},
|
|
235
|
+
content: [
|
|
236
|
+
{
|
|
237
|
+
tag: "image_dimensions",
|
|
238
|
+
attrs: {},
|
|
239
|
+
content: [
|
|
240
|
+
{
|
|
241
|
+
tag: "width",
|
|
242
|
+
attrs: {},
|
|
243
|
+
content: Buffer.from("100")
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
tag: "height",
|
|
247
|
+
attrs: {},
|
|
248
|
+
content: Buffer.from("100")
|
|
249
|
+
}
|
|
250
|
+
]
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
tag: "token",
|
|
254
|
+
attrs: {},
|
|
255
|
+
content: Buffer.from(tokenBase64)
|
|
256
|
+
}
|
|
257
|
+
]
|
|
258
|
+
}
|
|
259
|
+
]
|
|
260
|
+
});
|
|
261
|
+
return parseOrderDetailsNode(result);
|
|
262
|
+
};
|
|
263
|
+
const productUpdate = async (productId, update) => {
|
|
264
|
+
update = await uploadingNecessaryImagesOfProduct(update, waUploadToServer);
|
|
265
|
+
const editNode = toProductNode(productId, update);
|
|
266
|
+
const result = await query({
|
|
267
|
+
tag: "iq",
|
|
268
|
+
attrs: {
|
|
269
|
+
to: S_WHATSAPP_NET,
|
|
270
|
+
type: "set",
|
|
271
|
+
xmlns: "w:biz:catalog"
|
|
272
|
+
},
|
|
273
|
+
content: [
|
|
274
|
+
{
|
|
275
|
+
tag: "product_catalog_edit",
|
|
276
|
+
attrs: { v: "1" },
|
|
277
|
+
content: [
|
|
278
|
+
editNode,
|
|
279
|
+
{
|
|
280
|
+
tag: "width",
|
|
281
|
+
attrs: {},
|
|
282
|
+
content: "100"
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
tag: "height",
|
|
286
|
+
attrs: {},
|
|
287
|
+
content: "100"
|
|
288
|
+
}
|
|
289
|
+
]
|
|
290
|
+
}
|
|
291
|
+
]
|
|
292
|
+
});
|
|
293
|
+
const productCatalogEditNode = getBinaryNodeChild(result, "product_catalog_edit");
|
|
294
|
+
const productNode = getBinaryNodeChild(productCatalogEditNode, "product");
|
|
295
|
+
return parseProductNode(productNode);
|
|
296
|
+
};
|
|
297
|
+
const productCreate = async (create) => {
|
|
298
|
+
create.isHidden = !!create.isHidden;
|
|
299
|
+
create = await uploadingNecessaryImagesOfProduct(create, waUploadToServer);
|
|
300
|
+
const createNode = toProductNode(undefined, create);
|
|
301
|
+
const result = await query({
|
|
302
|
+
tag: "iq",
|
|
303
|
+
attrs: {
|
|
304
|
+
to: S_WHATSAPP_NET,
|
|
305
|
+
type: "set",
|
|
306
|
+
xmlns: "w:biz:catalog"
|
|
307
|
+
},
|
|
308
|
+
content: [
|
|
309
|
+
{
|
|
310
|
+
tag: "product_catalog_add",
|
|
311
|
+
attrs: { v: "1" },
|
|
312
|
+
content: [
|
|
313
|
+
createNode,
|
|
314
|
+
{
|
|
315
|
+
tag: "width",
|
|
316
|
+
attrs: {},
|
|
317
|
+
content: "100"
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
tag: "height",
|
|
321
|
+
attrs: {},
|
|
322
|
+
content: "100"
|
|
323
|
+
}
|
|
324
|
+
]
|
|
325
|
+
}
|
|
326
|
+
]
|
|
327
|
+
});
|
|
328
|
+
const productCatalogAddNode = getBinaryNodeChild(result, "product_catalog_add");
|
|
329
|
+
const productNode = getBinaryNodeChild(productCatalogAddNode, "product");
|
|
330
|
+
return parseProductNode(productNode);
|
|
331
|
+
};
|
|
332
|
+
const productDelete = async (productIds) => {
|
|
333
|
+
const result = await query({
|
|
334
|
+
tag: "iq",
|
|
335
|
+
attrs: {
|
|
336
|
+
to: S_WHATSAPP_NET,
|
|
337
|
+
type: "set",
|
|
338
|
+
xmlns: "w:biz:catalog"
|
|
339
|
+
},
|
|
340
|
+
content: [
|
|
341
|
+
{
|
|
342
|
+
tag: "product_catalog_delete",
|
|
343
|
+
attrs: { v: "1" },
|
|
344
|
+
content: productIds.map(id => ({
|
|
345
|
+
tag: "product",
|
|
346
|
+
attrs: {},
|
|
347
|
+
content: [
|
|
348
|
+
{
|
|
349
|
+
tag: "id",
|
|
350
|
+
attrs: {},
|
|
351
|
+
content: Buffer.from(id)
|
|
352
|
+
}
|
|
353
|
+
]
|
|
354
|
+
}))
|
|
355
|
+
}
|
|
356
|
+
]
|
|
357
|
+
});
|
|
358
|
+
const productCatalogDelNode = getBinaryNodeChild(result, "product_catalog_delete");
|
|
359
|
+
return {
|
|
360
|
+
deleted: +(productCatalogDelNode?.attrs.deleted_count || 0)
|
|
361
|
+
};
|
|
362
|
+
};
|
|
363
|
+
return {
|
|
364
|
+
...sock,
|
|
365
|
+
logger: config.logger,
|
|
366
|
+
getOrderDetails,
|
|
367
|
+
getCatalog,
|
|
368
|
+
getCollections,
|
|
369
|
+
productCreate,
|
|
370
|
+
productDelete,
|
|
371
|
+
productUpdate,
|
|
372
|
+
updateBussinesProfile,
|
|
373
|
+
updateCoverPhoto,
|
|
374
|
+
removeCoverPhoto
|
|
375
|
+
};
|
|
376
|
+
};
|
|
377
|
+
//=======================================================//
|