@genuxofficial/baileys 1.0.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.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/WAProto/GenerateStatics.sh +4 -0
- package/WAProto/WAProto.proto +3344 -0
- package/WAProto/index.d.ts +37016 -0
- package/WAProto/index.js +101044 -0
- package/WASignalGroup/GroupProtocol.js +1697 -0
- package/WASignalGroup/ciphertext_message.js +16 -0
- package/WASignalGroup/group_cipher.js +120 -0
- package/WASignalGroup/group_session_builder.js +46 -0
- package/WASignalGroup/index.js +5 -0
- package/WASignalGroup/keyhelper.js +21 -0
- package/WASignalGroup/protobufs.js +3 -0
- package/WASignalGroup/queue_job.js +69 -0
- package/WASignalGroup/sender_chain_key.js +50 -0
- package/WASignalGroup/sender_key_distribution_message.js +78 -0
- package/WASignalGroup/sender_key_message.js +92 -0
- package/WASignalGroup/sender_key_name.js +70 -0
- package/WASignalGroup/sender_key_record.js +56 -0
- package/WASignalGroup/sender_key_state.js +129 -0
- package/WASignalGroup/sender_message_key.js +39 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.d.ts +53 -0
- package/lib/Defaults/index.js +106 -0
- package/lib/Signal/libsignal.d.ts +3 -0
- package/lib/Signal/libsignal.js +152 -0
- package/lib/Socket/Client/index.d.ts +2 -0
- package/lib/Socket/Client/index.js +18 -0
- package/lib/Socket/Client/types.d.ts +17 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.d.ts +12 -0
- package/lib/Socket/Client/websocket.js +62 -0
- package/lib/Socket/business.d.ts +149 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.d.ts +85 -0
- package/lib/Socket/chats.js +850 -0
- package/lib/Socket/groups.d.ts +124 -0
- package/lib/Socket/groups.js +314 -0
- package/lib/Socket/index.d.ts +149 -0
- package/lib/Socket/index.js +10 -0
- package/lib/Socket/messages-recv.d.ts +136 -0
- package/lib/Socket/messages-recv.js +912 -0
- package/lib/Socket/messages-send.d.ts +130 -0
- package/lib/Socket/messages-send.js +673 -0
- package/lib/Socket/socket.d.ts +45 -0
- package/lib/Socket/socket.js +622 -0
- package/lib/Socket/usync.d.ts +38 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/make-cache-manager-store.d.ts +14 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/Store/make-in-memory-store.d.ts +118 -0
- package/lib/Store/make-in-memory-store.js +420 -0
- package/lib/Store/make-ordered-dictionary.d.ts +13 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.d.ts +105 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.d.ts +13 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.d.ts +108 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.d.ts +19 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.d.ts +172 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.d.ts +56 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.d.ts +46 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.d.ts +29 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.d.ts +271 -0
- package/lib/Types/Message.js +9 -0
- package/lib/Types/Product.d.ts +78 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.d.ts +57 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.d.ts +118 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.d.ts +27 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.d.ts +25 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.d.ts +63 -0
- package/lib/Types/index.js +41 -0
- package/lib/Utils/auth-utils.d.ts +18 -0
- package/lib/Utils/auth-utils.js +200 -0
- package/lib/Utils/baileys-event-stream.d.ts +16 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/business.d.ts +22 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.d.ts +72 -0
- package/lib/Utils/chat-utils.js +745 -0
- package/lib/Utils/crypto.d.ts +42 -0
- package/lib/Utils/crypto.js +181 -0
- package/lib/Utils/decode-wa-message.d.ts +36 -0
- package/lib/Utils/decode-wa-message.js +205 -0
- package/lib/Utils/event-buffer.d.ts +35 -0
- package/lib/Utils/event-buffer.js +520 -0
- package/lib/Utils/generics.d.ts +94 -0
- package/lib/Utils/generics.js +433 -0
- package/lib/Utils/history.d.ts +19 -0
- package/lib/Utils/history.js +94 -0
- package/lib/Utils/index.d.ts +17 -0
- package/lib/Utils/index.js +33 -0
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.js +116 -0
- package/lib/Utils/logger.d.ts +11 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.d.ts +7 -0
- package/lib/Utils/make-mutex.js +44 -0
- package/lib/Utils/messages-media.d.ts +108 -0
- package/lib/Utils/messages-media.js +667 -0
- package/lib/Utils/messages.d.ts +78 -0
- package/lib/Utils/messages.js +760 -0
- package/lib/Utils/noise-handler.d.ts +21 -0
- package/lib/Utils/noise-handler.js +150 -0
- package/lib/Utils/process-message.d.ts +42 -0
- package/lib/Utils/process-message.js +373 -0
- package/lib/Utils/signal.d.ts +33 -0
- package/lib/Utils/signal.js +153 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +12 -0
- package/lib/Utils/use-multi-file-auth-state.js +94 -0
- package/lib/Utils/validate-connection.d.ts +10 -0
- package/lib/Utils/validate-connection.js +175 -0
- package/lib/WABinary/constants.d.ts +27 -0
- package/lib/WABinary/constants.js +40 -0
- package/lib/WABinary/decode.d.ts +8 -0
- package/lib/WABinary/decode.js +254 -0
- package/lib/WABinary/encode.d.ts +4 -0
- package/lib/WABinary/encode.js +234 -0
- package/lib/WABinary/generic-utils.d.ts +16 -0
- package/lib/WABinary/generic-utils.js +110 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.d.ts +31 -0
- package/lib/WABinary/jid-utils.js +62 -0
- package/lib/WABinary/types.d.ts +18 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.d.ts +18 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.d.ts +39 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.d.ts +4 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/index.d.ts +4 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.d.ts +26 -0
- package/lib/WAUSync/USyncQuery.js +79 -0
- package/lib/WAUSync/USyncUser.d.ts +10 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +31 -0
- package/package.json +104 -0
@@ -0,0 +1,433 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
27
|
+
};
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
29
|
+
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.getPlatformId = exports.Browsers = void 0;
|
30
|
+
const boom_1 = require("@hapi/boom");
|
31
|
+
const axios_1 = __importDefault(require("axios"));
|
32
|
+
const crypto_1 = require("crypto");
|
33
|
+
const os_1 = require("os");
|
34
|
+
const WAProto_1 = require("../../WAProto");
|
35
|
+
const baileys_version_json_1 = require("../Defaults/baileys-version.json");
|
36
|
+
const Types_1 = require("../Types");
|
37
|
+
const WABinary_1 = require("../WABinary");
|
38
|
+
const PLATFORM_MAP = {
|
39
|
+
'aix': 'AIX',
|
40
|
+
'darwin': 'Mac OS',
|
41
|
+
'win32': 'Windows',
|
42
|
+
'android': 'Android',
|
43
|
+
'freebsd': 'FreeBSD',
|
44
|
+
'openbsd': 'OpenBSD',
|
45
|
+
'sunos': 'Solaris'
|
46
|
+
};
|
47
|
+
exports.Browsers = {
|
48
|
+
ubuntu: (browser) => ['Ubuntu', browser, '22.04.4'],
|
49
|
+
macOS: (browser) => ['Mac OS', browser, '14.4.1'],
|
50
|
+
baileys: (browser) => ['Baileys', browser, '6.5.0'],
|
51
|
+
windows: (browser) => ['Windows', browser, '10.0.22631'],
|
52
|
+
/** The appropriate browser based on your OS & release */
|
53
|
+
appropriate: (browser) => [PLATFORM_MAP[(0, os_1.platform)()] || 'Ubuntu', browser, (0, os_1.release)()]
|
54
|
+
};
|
55
|
+
const getPlatformId = (browser) => {
|
56
|
+
const platformType = WAProto_1.proto.DeviceProps.PlatformType[browser.toUpperCase()];
|
57
|
+
return platformType ? platformType.toString().charCodeAt(0).toString() : '49'; //chrome
|
58
|
+
};
|
59
|
+
exports.getPlatformId = getPlatformId;
|
60
|
+
exports.BufferJSON = {
|
61
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
62
|
+
replacer: (k, value) => {
|
63
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array || (value === null || value === void 0 ? void 0 : value.type) === 'Buffer') {
|
64
|
+
return { type: 'Buffer', data: Buffer.from((value === null || value === void 0 ? void 0 : value.data) || value).toString('base64') };
|
65
|
+
}
|
66
|
+
return value;
|
67
|
+
},
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
69
|
+
reviver: (_, value) => {
|
70
|
+
if (typeof value === 'object' && !!value && (value.buffer === true || value.type === 'Buffer')) {
|
71
|
+
const val = value.data || value.value;
|
72
|
+
return typeof val === 'string' ? Buffer.from(val, 'base64') : Buffer.from(val || []);
|
73
|
+
}
|
74
|
+
return value;
|
75
|
+
}
|
76
|
+
};
|
77
|
+
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)) || '');
|
78
|
+
exports.getKeyAuthor = getKeyAuthor;
|
79
|
+
const writeRandomPadMax16 = (msg) => {
|
80
|
+
const pad = (0, crypto_1.randomBytes)(1);
|
81
|
+
pad[0] &= 0xf;
|
82
|
+
if (!pad[0]) {
|
83
|
+
pad[0] = 0xf;
|
84
|
+
}
|
85
|
+
return Buffer.concat([msg, Buffer.alloc(pad[0], pad[0])]);
|
86
|
+
};
|
87
|
+
exports.writeRandomPadMax16 = writeRandomPadMax16;
|
88
|
+
const unpadRandomMax16 = (e) => {
|
89
|
+
const t = new Uint8Array(e);
|
90
|
+
if (0 === t.length) {
|
91
|
+
throw new Error('unpadPkcs7 given empty bytes');
|
92
|
+
}
|
93
|
+
var r = t[t.length - 1];
|
94
|
+
if (r > t.length) {
|
95
|
+
throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`);
|
96
|
+
}
|
97
|
+
return new Uint8Array(t.buffer, t.byteOffset, t.length - r);
|
98
|
+
};
|
99
|
+
exports.unpadRandomMax16 = unpadRandomMax16;
|
100
|
+
const encodeWAMessage = (message) => ((0, exports.writeRandomPadMax16)(WAProto_1.proto.Message.encode(message).finish()));
|
101
|
+
exports.encodeWAMessage = encodeWAMessage;
|
102
|
+
const generateRegistrationId = () => {
|
103
|
+
return Uint16Array.from((0, crypto_1.randomBytes)(2))[0] & 16383;
|
104
|
+
};
|
105
|
+
exports.generateRegistrationId = generateRegistrationId;
|
106
|
+
const encodeBigEndian = (e, t = 4) => {
|
107
|
+
let r = e;
|
108
|
+
const a = new Uint8Array(t);
|
109
|
+
for (let i = t - 1; i >= 0; i--) {
|
110
|
+
a[i] = 255 & r;
|
111
|
+
r >>>= 8;
|
112
|
+
}
|
113
|
+
return a;
|
114
|
+
};
|
115
|
+
exports.encodeBigEndian = encodeBigEndian;
|
116
|
+
const toNumber = (t) => ((typeof t === 'object' && t) ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0);
|
117
|
+
exports.toNumber = toNumber;
|
118
|
+
/** unix timestamp of a date in seconds */
|
119
|
+
const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000);
|
120
|
+
exports.unixTimestampSeconds = unixTimestampSeconds;
|
121
|
+
const debouncedTimeout = (intervalMs = 1000, task) => {
|
122
|
+
let timeout;
|
123
|
+
return {
|
124
|
+
start: (newIntervalMs, newTask) => {
|
125
|
+
task = newTask || task;
|
126
|
+
intervalMs = newIntervalMs || intervalMs;
|
127
|
+
timeout && clearTimeout(timeout);
|
128
|
+
timeout = setTimeout(() => task === null || task === void 0 ? void 0 : task(), intervalMs);
|
129
|
+
},
|
130
|
+
cancel: () => {
|
131
|
+
timeout && clearTimeout(timeout);
|
132
|
+
timeout = undefined;
|
133
|
+
},
|
134
|
+
setTask: (newTask) => task = newTask,
|
135
|
+
setInterval: (newInterval) => intervalMs = newInterval
|
136
|
+
};
|
137
|
+
};
|
138
|
+
exports.debouncedTimeout = debouncedTimeout;
|
139
|
+
const delay = (ms) => (0, exports.delayCancellable)(ms).delay;
|
140
|
+
exports.delay = delay;
|
141
|
+
const delayCancellable = (ms) => {
|
142
|
+
const stack = new Error().stack;
|
143
|
+
let timeout;
|
144
|
+
let reject;
|
145
|
+
const delay = new Promise((resolve, _reject) => {
|
146
|
+
timeout = setTimeout(resolve, ms);
|
147
|
+
reject = _reject;
|
148
|
+
});
|
149
|
+
const cancel = () => {
|
150
|
+
clearTimeout(timeout);
|
151
|
+
reject(new boom_1.Boom('Cancelled', {
|
152
|
+
statusCode: 500,
|
153
|
+
data: {
|
154
|
+
stack
|
155
|
+
}
|
156
|
+
}));
|
157
|
+
};
|
158
|
+
return { delay, cancel };
|
159
|
+
};
|
160
|
+
exports.delayCancellable = delayCancellable;
|
161
|
+
async function promiseTimeout(ms, promise) {
|
162
|
+
if (!ms) {
|
163
|
+
return new Promise(promise);
|
164
|
+
}
|
165
|
+
const stack = new Error().stack;
|
166
|
+
// Create a promise that rejects in <ms> milliseconds
|
167
|
+
const { delay, cancel } = (0, exports.delayCancellable)(ms);
|
168
|
+
const p = new Promise((resolve, reject) => {
|
169
|
+
delay
|
170
|
+
.then(() => reject(new boom_1.Boom('Timed Out', {
|
171
|
+
statusCode: Types_1.DisconnectReason.timedOut,
|
172
|
+
data: {
|
173
|
+
stack
|
174
|
+
}
|
175
|
+
})))
|
176
|
+
.catch(err => reject(err));
|
177
|
+
promise(resolve, reject);
|
178
|
+
})
|
179
|
+
.finally(cancel);
|
180
|
+
return p;
|
181
|
+
}
|
182
|
+
exports.promiseTimeout = promiseTimeout;
|
183
|
+
// inspired from whatsmeow code
|
184
|
+
// https://github.com/tulir/whatsmeow/blob/64bc969fbe78d31ae0dd443b8d4c80a5d026d07a/send.go#L42
|
185
|
+
const generateMessageIDV2 = (userId) => {
|
186
|
+
const data = Buffer.alloc(8 + 20 + 16);
|
187
|
+
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)));
|
188
|
+
if (userId) {
|
189
|
+
const id = (0, WABinary_1.jidDecode)(userId);
|
190
|
+
if (id === null || id === void 0 ? void 0 : id.user) {
|
191
|
+
data.write(id.user, 8);
|
192
|
+
data.write('@c.us', 8 + id.user.length);
|
193
|
+
}
|
194
|
+
}
|
195
|
+
const random = (0, crypto_1.randomBytes)(16);
|
196
|
+
random.copy(data, 28);
|
197
|
+
const hash = (0, crypto_1.createHash)('sha256').update(data).digest();
|
198
|
+
return '3EB0' + hash.toString('hex').toUpperCase().substring(0, 18);
|
199
|
+
};
|
200
|
+
exports.generateMessageIDV2 = generateMessageIDV2;
|
201
|
+
// generate a random ID to attach to a message
|
202
|
+
const generateMessageID = () => '3EB0' + (0, crypto_1.randomBytes)(18).toString('hex').toUpperCase();
|
203
|
+
exports.generateMessageID = generateMessageID;
|
204
|
+
function bindWaitForEvent(ev, event) {
|
205
|
+
return async (check, timeoutMs) => {
|
206
|
+
let listener;
|
207
|
+
let closeListener;
|
208
|
+
await (promiseTimeout(timeoutMs, (resolve, reject) => {
|
209
|
+
closeListener = ({ connection, lastDisconnect }) => {
|
210
|
+
if (connection === 'close') {
|
211
|
+
reject((lastDisconnect === null || lastDisconnect === void 0 ? void 0 : lastDisconnect.error)
|
212
|
+
|| new boom_1.Boom('Connection Closed', { statusCode: Types_1.DisconnectReason.connectionClosed }));
|
213
|
+
}
|
214
|
+
};
|
215
|
+
ev.on('connection.update', closeListener);
|
216
|
+
listener = async (update) => {
|
217
|
+
if (await check(update)) {
|
218
|
+
resolve();
|
219
|
+
}
|
220
|
+
};
|
221
|
+
ev.on(event, listener);
|
222
|
+
})
|
223
|
+
.finally(() => {
|
224
|
+
ev.off(event, listener);
|
225
|
+
ev.off('connection.update', closeListener);
|
226
|
+
}));
|
227
|
+
};
|
228
|
+
}
|
229
|
+
exports.bindWaitForEvent = bindWaitForEvent;
|
230
|
+
const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update');
|
231
|
+
exports.bindWaitForConnectionUpdate = bindWaitForConnectionUpdate;
|
232
|
+
const printQRIfNecessaryListener = (ev, logger) => {
|
233
|
+
ev.on('connection.update', async ({ qr }) => {
|
234
|
+
if (qr) {
|
235
|
+
const QR = await Promise.resolve().then(() => __importStar(require('qrcode-terminal'))).then(m => m.default || m)
|
236
|
+
.catch(() => {
|
237
|
+
logger.error('QR code terminal not added as dependency');
|
238
|
+
});
|
239
|
+
QR === null || QR === void 0 ? void 0 : QR.generate(qr, { small: true });
|
240
|
+
}
|
241
|
+
});
|
242
|
+
};
|
243
|
+
exports.printQRIfNecessaryListener = printQRIfNecessaryListener;
|
244
|
+
/**
|
245
|
+
* utility that fetches latest baileys version from the master branch.
|
246
|
+
* Use to ensure your WA connection is always on the latest version
|
247
|
+
*/
|
248
|
+
const fetchLatestBaileysVersion = async (options = {}) => {
|
249
|
+
const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/baileys-version.json';
|
250
|
+
try {
|
251
|
+
const result = await axios_1.default.get(URL, {
|
252
|
+
...options,
|
253
|
+
responseType: 'json'
|
254
|
+
});
|
255
|
+
return {
|
256
|
+
version: result.data.version,
|
257
|
+
isLatest: true
|
258
|
+
};
|
259
|
+
}
|
260
|
+
catch (error) {
|
261
|
+
return {
|
262
|
+
version: baileys_version_json_1.version,
|
263
|
+
isLatest: false,
|
264
|
+
error
|
265
|
+
};
|
266
|
+
}
|
267
|
+
};
|
268
|
+
exports.fetchLatestBaileysVersion = fetchLatestBaileysVersion;
|
269
|
+
/**
|
270
|
+
* A utility that fetches the latest web version of whatsapp.
|
271
|
+
* Use to ensure your WA connection is always on the latest version
|
272
|
+
*/
|
273
|
+
const fetchLatestWaWebVersion = async (options) => {
|
274
|
+
try {
|
275
|
+
const { data } = await axios_1.default.get('https://web.whatsapp.com/sw.js', {
|
276
|
+
...options,
|
277
|
+
responseType: 'json'
|
278
|
+
});
|
279
|
+
const regex = /\\?"client_revision\\?":\s*(\d+)/;
|
280
|
+
const match = data.match(regex);
|
281
|
+
if (!(match === null || match === void 0 ? void 0 : match[1])) {
|
282
|
+
return {
|
283
|
+
version: baileys_version_json_1.version,
|
284
|
+
isLatest: false,
|
285
|
+
error: {
|
286
|
+
message: 'Could not find client revision in the fetched content'
|
287
|
+
}
|
288
|
+
};
|
289
|
+
}
|
290
|
+
const clientRevision = match[1];
|
291
|
+
return {
|
292
|
+
version: [2, 3000, +clientRevision],
|
293
|
+
isLatest: true
|
294
|
+
};
|
295
|
+
}
|
296
|
+
catch (error) {
|
297
|
+
return {
|
298
|
+
version: baileys_version_json_1.version,
|
299
|
+
isLatest: false,
|
300
|
+
error
|
301
|
+
};
|
302
|
+
}
|
303
|
+
};
|
304
|
+
exports.fetchLatestWaWebVersion = fetchLatestWaWebVersion;
|
305
|
+
/** unique message tag prefix for MD clients */
|
306
|
+
const generateMdTagPrefix = () => {
|
307
|
+
const bytes = (0, crypto_1.randomBytes)(4);
|
308
|
+
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
|
309
|
+
};
|
310
|
+
exports.generateMdTagPrefix = generateMdTagPrefix;
|
311
|
+
const STATUS_MAP = {
|
312
|
+
'sender': WAProto_1.proto.WebMessageInfo.Status.SERVER_ACK,
|
313
|
+
'played': WAProto_1.proto.WebMessageInfo.Status.PLAYED,
|
314
|
+
'read': WAProto_1.proto.WebMessageInfo.Status.READ,
|
315
|
+
'read-self': WAProto_1.proto.WebMessageInfo.Status.READ
|
316
|
+
};
|
317
|
+
/**
|
318
|
+
* Given a type of receipt, returns what the new status of the message should be
|
319
|
+
* @param type type from receipt
|
320
|
+
*/
|
321
|
+
const getStatusFromReceiptType = (type) => {
|
322
|
+
const status = STATUS_MAP[type];
|
323
|
+
if (typeof type === 'undefined') {
|
324
|
+
return WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK;
|
325
|
+
}
|
326
|
+
return status;
|
327
|
+
};
|
328
|
+
exports.getStatusFromReceiptType = getStatusFromReceiptType;
|
329
|
+
const CODE_MAP = {
|
330
|
+
conflict: Types_1.DisconnectReason.connectionReplaced
|
331
|
+
};
|
332
|
+
/**
|
333
|
+
* Stream errors generally provide a reason, map that to a baileys DisconnectReason
|
334
|
+
* @param reason the string reason given, eg. "conflict"
|
335
|
+
*/
|
336
|
+
const getErrorCodeFromStreamError = (node) => {
|
337
|
+
const [reasonNode] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
|
338
|
+
let reason = (reasonNode === null || reasonNode === void 0 ? void 0 : reasonNode.tag) || 'unknown';
|
339
|
+
const statusCode = +(node.attrs.code || CODE_MAP[reason] || Types_1.DisconnectReason.badSession);
|
340
|
+
if (statusCode === Types_1.DisconnectReason.restartRequired) {
|
341
|
+
reason = 'restart required';
|
342
|
+
}
|
343
|
+
return {
|
344
|
+
reason,
|
345
|
+
statusCode
|
346
|
+
};
|
347
|
+
};
|
348
|
+
exports.getErrorCodeFromStreamError = getErrorCodeFromStreamError;
|
349
|
+
const getCallStatusFromNode = ({ tag, attrs }) => {
|
350
|
+
let status;
|
351
|
+
switch (tag) {
|
352
|
+
case 'offer':
|
353
|
+
case 'offer_notice':
|
354
|
+
status = 'offer';
|
355
|
+
break;
|
356
|
+
case 'terminate':
|
357
|
+
if (attrs.reason === 'timeout') {
|
358
|
+
status = 'timeout';
|
359
|
+
}
|
360
|
+
else {
|
361
|
+
//fired when accepted/rejected/timeout/caller hangs up
|
362
|
+
status = 'terminate';
|
363
|
+
}
|
364
|
+
break;
|
365
|
+
case 'reject':
|
366
|
+
status = 'reject';
|
367
|
+
break;
|
368
|
+
case 'accept':
|
369
|
+
status = 'accept';
|
370
|
+
break;
|
371
|
+
default:
|
372
|
+
status = 'ringing';
|
373
|
+
break;
|
374
|
+
}
|
375
|
+
return status;
|
376
|
+
};
|
377
|
+
exports.getCallStatusFromNode = getCallStatusFromNode;
|
378
|
+
const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: ';
|
379
|
+
const getCodeFromWSError = (error) => {
|
380
|
+
var _a, _b, _c;
|
381
|
+
let statusCode = 500;
|
382
|
+
if ((_a = error === null || error === void 0 ? void 0 : error.message) === null || _a === void 0 ? void 0 : _a.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
|
383
|
+
const code = +(error === null || error === void 0 ? void 0 : error.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length));
|
384
|
+
if (!Number.isNaN(code) && code >= 400) {
|
385
|
+
statusCode = code;
|
386
|
+
}
|
387
|
+
}
|
388
|
+
else if (
|
389
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
390
|
+
((_b = error === null || error === void 0 ? void 0 : error.code) === null || _b === void 0 ? void 0 : _b.startsWith('E'))
|
391
|
+
|| ((_c = error === null || error === void 0 ? void 0 : error.message) === null || _c === void 0 ? void 0 : _c.includes('timed out'))) { // handle ETIMEOUT, ENOTFOUND etc
|
392
|
+
statusCode = 408;
|
393
|
+
}
|
394
|
+
return statusCode;
|
395
|
+
};
|
396
|
+
exports.getCodeFromWSError = getCodeFromWSError;
|
397
|
+
/**
|
398
|
+
* Is the given platform WA business
|
399
|
+
* @param platform AuthenticationCreds.platform
|
400
|
+
*/
|
401
|
+
const isWABusinessPlatform = (platform) => {
|
402
|
+
return platform === 'smbi' || platform === 'smba';
|
403
|
+
};
|
404
|
+
exports.isWABusinessPlatform = isWABusinessPlatform;
|
405
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
406
|
+
function trimUndefined(obj) {
|
407
|
+
for (const key in obj) {
|
408
|
+
if (typeof obj[key] === 'undefined') {
|
409
|
+
delete obj[key];
|
410
|
+
}
|
411
|
+
}
|
412
|
+
return obj;
|
413
|
+
}
|
414
|
+
exports.trimUndefined = trimUndefined;
|
415
|
+
const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ';
|
416
|
+
function bytesToCrockford(buffer) {
|
417
|
+
let value = 0;
|
418
|
+
let bitCount = 0;
|
419
|
+
const crockford = [];
|
420
|
+
for (const element of buffer) {
|
421
|
+
value = (value << 8) | (element & 0xff);
|
422
|
+
bitCount += 8;
|
423
|
+
while (bitCount >= 5) {
|
424
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31));
|
425
|
+
bitCount -= 5;
|
426
|
+
}
|
427
|
+
}
|
428
|
+
if (bitCount > 0) {
|
429
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31));
|
430
|
+
}
|
431
|
+
return crockford.join('');
|
432
|
+
}
|
433
|
+
exports.bytesToCrockford = bytesToCrockford;
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
2
|
+
import { proto } from '../../WAProto';
|
3
|
+
import { Chat, Contact } from '../Types';
|
4
|
+
export declare const downloadHistory: (msg: proto.Message.IHistorySyncNotification, options: AxiosRequestConfig<{}>) => Promise<proto.HistorySync>;
|
5
|
+
export declare const processHistoryMessage: (item: proto.IHistorySync) => {
|
6
|
+
chats: Chat[];
|
7
|
+
contacts: Contact[];
|
8
|
+
messages: proto.IWebMessageInfo[];
|
9
|
+
syncType: proto.HistorySync.HistorySyncType;
|
10
|
+
progress: number | null | undefined;
|
11
|
+
};
|
12
|
+
export declare const downloadAndProcessHistorySyncNotification: (msg: proto.Message.IHistorySyncNotification, options: AxiosRequestConfig<{}>) => Promise<{
|
13
|
+
chats: Chat[];
|
14
|
+
contacts: Contact[];
|
15
|
+
messages: proto.IWebMessageInfo[];
|
16
|
+
syncType: proto.HistorySync.HistorySyncType;
|
17
|
+
progress: number | null | undefined;
|
18
|
+
}>;
|
19
|
+
export declare const getHistoryMsg: (message: proto.IMessage) => proto.Message.IHistorySyncNotification | null | undefined;
|
@@ -0,0 +1,94 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getHistoryMsg = exports.downloadAndProcessHistorySyncNotification = exports.processHistoryMessage = exports.downloadHistory = void 0;
|
4
|
+
const util_1 = require("util");
|
5
|
+
const zlib_1 = require("zlib");
|
6
|
+
const WAProto_1 = require("../../WAProto");
|
7
|
+
const Types_1 = require("../Types");
|
8
|
+
const WABinary_1 = require("../WABinary");
|
9
|
+
const generics_1 = require("./generics");
|
10
|
+
const messages_1 = require("./messages");
|
11
|
+
const messages_media_1 = require("./messages-media");
|
12
|
+
const inflatePromise = (0, util_1.promisify)(zlib_1.inflate);
|
13
|
+
const downloadHistory = async (msg, options) => {
|
14
|
+
const stream = await (0, messages_media_1.downloadContentFromMessage)(msg, 'md-msg-hist', { options });
|
15
|
+
const bufferArray = [];
|
16
|
+
for await (const chunk of stream) {
|
17
|
+
bufferArray.push(chunk);
|
18
|
+
}
|
19
|
+
let buffer = Buffer.concat(bufferArray);
|
20
|
+
// decompress buffer
|
21
|
+
buffer = await inflatePromise(buffer);
|
22
|
+
const syncData = WAProto_1.proto.HistorySync.decode(buffer);
|
23
|
+
return syncData;
|
24
|
+
};
|
25
|
+
exports.downloadHistory = downloadHistory;
|
26
|
+
const processHistoryMessage = (item) => {
|
27
|
+
var _a, _b, _c;
|
28
|
+
const messages = [];
|
29
|
+
const contacts = [];
|
30
|
+
const chats = [];
|
31
|
+
switch (item.syncType) {
|
32
|
+
case WAProto_1.proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP:
|
33
|
+
case WAProto_1.proto.HistorySync.HistorySyncType.RECENT:
|
34
|
+
case WAProto_1.proto.HistorySync.HistorySyncType.FULL:
|
35
|
+
case WAProto_1.proto.HistorySync.HistorySyncType.ON_DEMAND:
|
36
|
+
for (const chat of item.conversations) {
|
37
|
+
contacts.push({ id: chat.id, name: chat.name || undefined });
|
38
|
+
const msgs = chat.messages || [];
|
39
|
+
delete chat.messages;
|
40
|
+
delete chat.archived;
|
41
|
+
delete chat.muteEndTime;
|
42
|
+
delete chat.pinned;
|
43
|
+
for (const item of msgs) {
|
44
|
+
const message = item.message;
|
45
|
+
messages.push(message);
|
46
|
+
if (!((_a = chat.messages) === null || _a === void 0 ? void 0 : _a.length)) {
|
47
|
+
// keep only the most recent message in the chat array
|
48
|
+
chat.messages = [{ message }];
|
49
|
+
}
|
50
|
+
if (!message.key.fromMe && !chat.lastMessageRecvTimestamp) {
|
51
|
+
chat.lastMessageRecvTimestamp = (0, generics_1.toNumber)(message.messageTimestamp);
|
52
|
+
}
|
53
|
+
if ((message.messageStubType === Types_1.WAMessageStubType.BIZ_PRIVACY_MODE_TO_BSP
|
54
|
+
|| message.messageStubType === Types_1.WAMessageStubType.BIZ_PRIVACY_MODE_TO_FB)
|
55
|
+
&& ((_b = message.messageStubParameters) === null || _b === void 0 ? void 0 : _b[0])) {
|
56
|
+
contacts.push({
|
57
|
+
id: message.key.participant || message.key.remoteJid,
|
58
|
+
verifiedName: (_c = message.messageStubParameters) === null || _c === void 0 ? void 0 : _c[0],
|
59
|
+
});
|
60
|
+
}
|
61
|
+
}
|
62
|
+
if ((0, WABinary_1.isJidUser)(chat.id) && chat.readOnly && chat.archived) {
|
63
|
+
delete chat.readOnly;
|
64
|
+
}
|
65
|
+
chats.push({ ...chat });
|
66
|
+
}
|
67
|
+
break;
|
68
|
+
case WAProto_1.proto.HistorySync.HistorySyncType.PUSH_NAME:
|
69
|
+
for (const c of item.pushnames) {
|
70
|
+
contacts.push({ id: c.id, notify: c.pushname });
|
71
|
+
}
|
72
|
+
break;
|
73
|
+
}
|
74
|
+
return {
|
75
|
+
chats,
|
76
|
+
contacts,
|
77
|
+
messages,
|
78
|
+
syncType: item.syncType,
|
79
|
+
progress: item.progress
|
80
|
+
};
|
81
|
+
};
|
82
|
+
exports.processHistoryMessage = processHistoryMessage;
|
83
|
+
const downloadAndProcessHistorySyncNotification = async (msg, options) => {
|
84
|
+
const historyMsg = await (0, exports.downloadHistory)(msg, options);
|
85
|
+
return (0, exports.processHistoryMessage)(historyMsg);
|
86
|
+
};
|
87
|
+
exports.downloadAndProcessHistorySyncNotification = downloadAndProcessHistorySyncNotification;
|
88
|
+
const getHistoryMsg = (message) => {
|
89
|
+
var _a;
|
90
|
+
const normalizedContent = !!message ? (0, messages_1.normalizeMessageContent)(message) : undefined;
|
91
|
+
const anyHistoryMsg = (_a = normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.protocolMessage) === null || _a === void 0 ? void 0 : _a.historySyncNotification;
|
92
|
+
return anyHistoryMsg;
|
93
|
+
};
|
94
|
+
exports.getHistoryMsg = getHistoryMsg;
|
@@ -0,0 +1,17 @@
|
|
1
|
+
export * from './generics';
|
2
|
+
export * from './decode-wa-message';
|
3
|
+
export * from './messages';
|
4
|
+
export * from './messages-media';
|
5
|
+
export * from './validate-connection';
|
6
|
+
export * from './crypto';
|
7
|
+
export * from './signal';
|
8
|
+
export * from './noise-handler';
|
9
|
+
export * from './history';
|
10
|
+
export * from './chat-utils';
|
11
|
+
export * from './lt-hash';
|
12
|
+
export * from './auth-utils';
|
13
|
+
export * from './baileys-event-stream';
|
14
|
+
export * from './use-multi-file-auth-state';
|
15
|
+
export * from './link-preview';
|
16
|
+
export * from './event-buffer';
|
17
|
+
export * from './process-message';
|
@@ -0,0 +1,33 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./generics"), exports);
|
18
|
+
__exportStar(require("./decode-wa-message"), exports);
|
19
|
+
__exportStar(require("./messages"), exports);
|
20
|
+
__exportStar(require("./messages-media"), exports);
|
21
|
+
__exportStar(require("./validate-connection"), exports);
|
22
|
+
__exportStar(require("./crypto"), exports);
|
23
|
+
__exportStar(require("./signal"), exports);
|
24
|
+
__exportStar(require("./noise-handler"), exports);
|
25
|
+
__exportStar(require("./history"), exports);
|
26
|
+
__exportStar(require("./chat-utils"), exports);
|
27
|
+
__exportStar(require("./lt-hash"), exports);
|
28
|
+
__exportStar(require("./auth-utils"), exports);
|
29
|
+
__exportStar(require("./baileys-event-stream"), exports);
|
30
|
+
__exportStar(require("./use-multi-file-auth-state"), exports);
|
31
|
+
__exportStar(require("./link-preview"), exports);
|
32
|
+
__exportStar(require("./event-buffer"), exports);
|
33
|
+
__exportStar(require("./process-message"), exports);
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
2
|
+
import { WAMediaUploadFunction, WAUrlInfo } from '../Types';
|
3
|
+
import { ILogger } from './logger';
|
4
|
+
export type URLGenerationOptions = {
|
5
|
+
thumbnailWidth: number;
|
6
|
+
fetchOpts: {
|
7
|
+
/** Timeout in ms */
|
8
|
+
timeout: number;
|
9
|
+
proxyUrl?: string;
|
10
|
+
headers?: AxiosRequestConfig<{}>['headers'];
|
11
|
+
};
|
12
|
+
uploadImage?: WAMediaUploadFunction;
|
13
|
+
logger?: ILogger;
|
14
|
+
};
|
15
|
+
/**
|
16
|
+
* Given a piece of text, checks for any URL present, generates link preview for the same and returns it
|
17
|
+
* Return undefined if the fetch failed or no URL was found
|
18
|
+
* @param text first matched URL in text
|
19
|
+
* @returns the URL info required to generate link preview
|
20
|
+
*/
|
21
|
+
export declare const getUrlInfo: (text: string, opts?: URLGenerationOptions) => Promise<WAUrlInfo | undefined>;
|