zalo-api-sdk 1.0.0 → 1.1.1
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/dist/apis/loginQR.d.ts +1 -0
- package/dist/apis/loginQR.js +90 -82
- package/dist/cjs/apis/loginQR.cjs +90 -82
- package/package.json +12 -62
- package/.tmp-cache/_update-notifier-last-checked +0 -0
package/dist/apis/loginQR.d.ts
CHANGED
package/dist/apis/loginQR.js
CHANGED
|
@@ -37,7 +37,7 @@ async function loadLoginPage(ctx) {
|
|
|
37
37
|
}
|
|
38
38
|
async function getLoginInfo(ctx, version) {
|
|
39
39
|
const form = new URLSearchParams();
|
|
40
|
-
form.append("continue", "https://zalo.me/
|
|
40
|
+
form.append("continue", "https://chat.zalo.me/");
|
|
41
41
|
form.append("v", version);
|
|
42
42
|
return await request(ctx, "https://id.zalo.me/account/logininfo", {
|
|
43
43
|
headers: {
|
|
@@ -51,33 +51,7 @@ async function getLoginInfo(ctx, version) {
|
|
|
51
51
|
"sec-fetch-dest": "empty",
|
|
52
52
|
"sec-fetch-mode": "cors",
|
|
53
53
|
"sec-fetch-site": "same-origin",
|
|
54
|
-
Referer: "https://id.zalo.me/account?continue=https%3A%2F%
|
|
55
|
-
"Referrer-Policy": "strict-origin-when-cross-origin",
|
|
56
|
-
},
|
|
57
|
-
body: form,
|
|
58
|
-
method: "POST",
|
|
59
|
-
})
|
|
60
|
-
.then((res) => res.json())
|
|
61
|
-
.catch(logger(ctx).error);
|
|
62
|
-
}
|
|
63
|
-
async function verifyClient(ctx, version) {
|
|
64
|
-
const form = new URLSearchParams();
|
|
65
|
-
form.append("type", "device");
|
|
66
|
-
form.append("continue", "https://zalo.me/pc");
|
|
67
|
-
form.append("v", version);
|
|
68
|
-
return await request(ctx, "https://id.zalo.me/account/verify-client", {
|
|
69
|
-
headers: {
|
|
70
|
-
accept: "*/*",
|
|
71
|
-
"accept-language": "vi-VN,vi;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-US;q=0.6,en;q=0.5",
|
|
72
|
-
"content-type": "application/x-www-form-urlencoded",
|
|
73
|
-
priority: "u=1, i",
|
|
74
|
-
"sec-ch-ua": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
|
|
75
|
-
"sec-ch-ua-mobile": "?0",
|
|
76
|
-
"sec-ch-ua-platform": '"Windows"',
|
|
77
|
-
"sec-fetch-dest": "empty",
|
|
78
|
-
"sec-fetch-mode": "cors",
|
|
79
|
-
"sec-fetch-site": "same-origin",
|
|
80
|
-
Referer: "https://id.zalo.me/account?continue=https%3A%2F%2Fzalo.me%2Fpc",
|
|
54
|
+
Referer: "https://id.zalo.me/account?continue=https%3A%2F%2Fchat.zalo.me%2F",
|
|
81
55
|
"Referrer-Policy": "strict-origin-when-cross-origin",
|
|
82
56
|
},
|
|
83
57
|
body: form,
|
|
@@ -88,7 +62,9 @@ async function verifyClient(ctx, version) {
|
|
|
88
62
|
}
|
|
89
63
|
async function generate(ctx, version) {
|
|
90
64
|
const form = new URLSearchParams();
|
|
91
|
-
|
|
65
|
+
// 1 = client tu ve QR (luong thiet bi tin cay cua Zalo Web), 0 = dung anh QR may chu tra
|
|
66
|
+
form.append("cliGenQR", "0");
|
|
67
|
+
form.append("continue", "https://chat.zalo.me/");
|
|
92
68
|
form.append("v", version);
|
|
93
69
|
return await request(ctx, "https://id.zalo.me/account/authen/qr/generate", {
|
|
94
70
|
headers: {
|
|
@@ -102,7 +78,7 @@ async function generate(ctx, version) {
|
|
|
102
78
|
"sec-fetch-dest": "empty",
|
|
103
79
|
"sec-fetch-mode": "cors",
|
|
104
80
|
"sec-fetch-site": "same-origin",
|
|
105
|
-
Referer: "https://id.zalo.me/account?continue=https%3A%2F%
|
|
81
|
+
Referer: "https://id.zalo.me/account?continue=https%3A%2F%2Fchat.zalo.me%2F",
|
|
106
82
|
"Referrer-Policy": "strict-origin-when-cross-origin",
|
|
107
83
|
},
|
|
108
84
|
body: form,
|
|
@@ -138,7 +114,7 @@ async function waitingScan(ctx, version, code, signal) {
|
|
|
138
114
|
method: "POST",
|
|
139
115
|
signal,
|
|
140
116
|
})
|
|
141
|
-
.then((res) => res.json())
|
|
117
|
+
.then((res) => (res.status == 502 || res.status == 504 ? { error_code: 8 } : res.json()))
|
|
142
118
|
.then((data) => {
|
|
143
119
|
if (data.error_code == 8) {
|
|
144
120
|
return waitingScan(ctx, version, code, signal);
|
|
@@ -153,8 +129,6 @@ async function waitingScan(ctx, version, code, signal) {
|
|
|
153
129
|
async function waitingConfirm(ctx, version, code, signal) {
|
|
154
130
|
const form = new URLSearchParams();
|
|
155
131
|
form.append("code", code);
|
|
156
|
-
form.append("gToken", "");
|
|
157
|
-
form.append("gAction", "CONFIRM_QR");
|
|
158
132
|
form.append("continue", "https://chat.zalo.me/");
|
|
159
133
|
form.append("v", version);
|
|
160
134
|
logger(ctx).info("Please confirm on your phone");
|
|
@@ -177,7 +151,7 @@ async function waitingConfirm(ctx, version, code, signal) {
|
|
|
177
151
|
method: "POST",
|
|
178
152
|
signal,
|
|
179
153
|
})
|
|
180
|
-
.then((res) => res.json())
|
|
154
|
+
.then((res) => (res.status == 502 || res.status == 504 ? { error_code: 8 } : res.json()))
|
|
181
155
|
.then((data) => {
|
|
182
156
|
if (data.error_code == 8) {
|
|
183
157
|
return waitingConfirm(ctx, version, code, signal);
|
|
@@ -189,8 +163,8 @@ async function waitingConfirm(ctx, version, code, signal) {
|
|
|
189
163
|
logger(ctx).error(e);
|
|
190
164
|
});
|
|
191
165
|
}
|
|
192
|
-
async function checkSession(ctx) {
|
|
193
|
-
return await request(ctx, "https://id.zalo.me/account/checksession?continue=https%3A%2F%2Fchat.zalo.me%2Findex.html", {
|
|
166
|
+
async function checkSession(ctx, url) {
|
|
167
|
+
return await request(ctx, url !== null && url !== void 0 ? url : "https://id.zalo.me/account/checksession?continue=https%3A%2F%2Fchat.zalo.me%2Findex.html", {
|
|
194
168
|
headers: {
|
|
195
169
|
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
|
|
196
170
|
"accept-language": "vi-VN,vi;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-US;q=0.6,en;q=0.5",
|
|
@@ -233,11 +207,26 @@ export async function loginQR(ctx, options, callback) {
|
|
|
233
207
|
ctx.cookie = new CookieJar();
|
|
234
208
|
ctx.userAgent = options.userAgent;
|
|
235
209
|
return new Promise(async (resolve, reject) => {
|
|
236
|
-
var _a;
|
|
210
|
+
var _a, _b;
|
|
211
|
+
// Khai bao truoc moi callback: abort() co the duoc goi ngay trong callback QRCodeGenerated
|
|
212
|
+
const controller = new AbortController();
|
|
213
|
+
let qrTimeout = null;
|
|
214
|
+
function clearQrTimeout() {
|
|
215
|
+
if (qrTimeout) {
|
|
216
|
+
clearTimeout(qrTimeout);
|
|
217
|
+
qrTimeout = null;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
237
220
|
try {
|
|
238
221
|
function retry() {
|
|
222
|
+
controller.abort();
|
|
223
|
+
clearQrTimeout();
|
|
239
224
|
return resolve(loginQR(ctx, options, callback));
|
|
240
225
|
}
|
|
226
|
+
function abort() {
|
|
227
|
+
controller.abort();
|
|
228
|
+
clearQrTimeout();
|
|
229
|
+
}
|
|
241
230
|
if (ctx.options.logging)
|
|
242
231
|
console.log();
|
|
243
232
|
const loginVersion = await loadLoginPage(ctx);
|
|
@@ -245,37 +234,12 @@ export async function loginQR(ctx, options, callback) {
|
|
|
245
234
|
return reject(new ZaloApiError("Cannot get API login version"));
|
|
246
235
|
logger(ctx).info("Got login version:", loginVersion);
|
|
247
236
|
await getLoginInfo(ctx, loginVersion);
|
|
248
|
-
await verifyClient(ctx, loginVersion);
|
|
249
237
|
const qrGenResult = await generate(ctx, loginVersion);
|
|
250
238
|
if (!qrGenResult || !qrGenResult.data)
|
|
251
239
|
return reject(new ZaloApiError(`Unable to generate QRCode\nResponse: ${JSON.stringify(qrGenResult, null, 2)}`));
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
type: LoginQRCallbackEventType.QRCodeGenerated,
|
|
256
|
-
data: Object.assign(Object.assign({}, qrGenResult.data), { image: qrGenResult.data.image.replace(/^data:image\/png;base64,/, "") }),
|
|
257
|
-
actions: {
|
|
258
|
-
async saveToFile(qrPath) {
|
|
259
|
-
var _a;
|
|
260
|
-
if (qrPath === void 0) { qrPath = (_a = options.qrPath) !== null && _a !== void 0 ? _a : "qr.png"; }
|
|
261
|
-
await saveQRCodeToFile(qrPath, qrData.image.replace(/^data:image\/png;base64,/, ""));
|
|
262
|
-
logger(ctx).info("Scan the QR code at", `'${qrPath}'`, "to proceed with login");
|
|
263
|
-
},
|
|
264
|
-
retry,
|
|
265
|
-
abort() {
|
|
266
|
-
controller.abort();
|
|
267
|
-
},
|
|
268
|
-
},
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
else {
|
|
272
|
-
const qrPath = (_a = options.qrPath) !== null && _a !== void 0 ? _a : "qr.png";
|
|
273
|
-
await saveQRCodeToFile(qrPath, qrData.image.replace(/^data:image\/png;base64,/, ""));
|
|
274
|
-
logger(ctx).info("Scan the QR code at", `'${qrPath}'`, "to proceed with login");
|
|
275
|
-
}
|
|
276
|
-
const controller = new AbortController();
|
|
277
|
-
const timeout = setTimeout(() => {
|
|
278
|
-
controller.abort();
|
|
240
|
+
let qrData = qrGenResult.data;
|
|
241
|
+
function handleQrTimeout() {
|
|
242
|
+
abort();
|
|
279
243
|
logger(ctx).info("QR expired!");
|
|
280
244
|
if (callback) {
|
|
281
245
|
callback({
|
|
@@ -292,32 +256,67 @@ export async function loginQR(ctx, options, callback) {
|
|
|
292
256
|
else {
|
|
293
257
|
resolve(loginQR(ctx, options));
|
|
294
258
|
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
259
|
+
}
|
|
260
|
+
let scanResult;
|
|
261
|
+
// Zalo co the yeu cau quet lan hai: tra ma QR moi (status 4) -> hien ma moi va cho quet tiep
|
|
262
|
+
while (true) {
|
|
263
|
+
const currentQr = qrData;
|
|
264
|
+
if (callback) {
|
|
265
|
+
callback({
|
|
266
|
+
type: LoginQRCallbackEventType.QRCodeGenerated,
|
|
267
|
+
data: Object.assign(Object.assign({}, currentQr), { image: currentQr.image.replace(/^data:image\/png;base64,/, "") }),
|
|
268
|
+
actions: {
|
|
269
|
+
async saveToFile(qrPath) {
|
|
270
|
+
var _a;
|
|
271
|
+
if (qrPath === void 0) { qrPath = (_a = options.qrPath) !== null && _a !== void 0 ? _a : "qr.png"; }
|
|
272
|
+
await saveQRCodeToFile(qrPath, currentQr.image.replace(/^data:image\/png;base64,/, ""));
|
|
273
|
+
logger(ctx).info("Scan the QR code at", `'${qrPath}'`, "to proceed with login");
|
|
274
|
+
},
|
|
275
|
+
retry,
|
|
276
|
+
abort,
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
const qrPath = (_a = options.qrPath) !== null && _a !== void 0 ? _a : "qr.png";
|
|
282
|
+
await saveQRCodeToFile(qrPath, currentQr.image.replace(/^data:image\/png;base64,/, ""));
|
|
283
|
+
logger(ctx).info("Scan the QR code at", `'${qrPath}'`, "to proceed with login");
|
|
284
|
+
}
|
|
285
|
+
if (controller.signal.aborted)
|
|
286
|
+
return resolve(null);
|
|
287
|
+
clearQrTimeout();
|
|
288
|
+
qrTimeout = setTimeout(handleQrTimeout, 100000);
|
|
289
|
+
scanResult = await waitingScan(ctx, loginVersion, currentQr.code, controller.signal);
|
|
290
|
+
if (!scanResult || !scanResult.data)
|
|
291
|
+
return resolve(null);
|
|
292
|
+
if (scanResult.data.status == 5)
|
|
293
|
+
return reject(new ZaloApiError(`QRCode login failed.\nResponse: ${JSON.stringify(scanResult, null, 2)}`));
|
|
294
|
+
if (scanResult.data.status != 4)
|
|
295
|
+
break;
|
|
296
|
+
const { code, token, image } = scanResult.data;
|
|
297
|
+
if (!code || !token || !image)
|
|
298
|
+
return reject(new ZaloApiError(`A second scan is required but no QR code was returned.\nResponse: ${JSON.stringify(scanResult, null, 2)}`));
|
|
299
|
+
logger(ctx).info("A second scan is required");
|
|
300
|
+
qrData = Object.assign(Object.assign({}, qrData), { code, token, image });
|
|
301
|
+
}
|
|
299
302
|
if (callback) {
|
|
300
303
|
callback({
|
|
301
304
|
type: LoginQRCallbackEventType.QRCodeScanned,
|
|
302
305
|
data: scanResult.data,
|
|
303
306
|
actions: {
|
|
304
307
|
retry,
|
|
305
|
-
abort
|
|
306
|
-
controller.abort();
|
|
307
|
-
},
|
|
308
|
+
abort,
|
|
308
309
|
},
|
|
309
310
|
});
|
|
310
311
|
}
|
|
311
|
-
|
|
312
|
-
|
|
312
|
+
// Da quet: het han 100 giay khong cat ngang buoc xac nhan nua
|
|
313
|
+
clearQrTimeout();
|
|
314
|
+
if (controller.signal.aborted)
|
|
313
315
|
return resolve(null);
|
|
314
|
-
const
|
|
315
|
-
if (!
|
|
316
|
+
const confirmResult = await waitingConfirm(ctx, loginVersion, qrData.code, controller.signal);
|
|
317
|
+
if (!confirmResult)
|
|
316
318
|
return resolve(null);
|
|
317
|
-
if (confirmResult.error_code ==
|
|
318
|
-
logger(ctx).info("Successfully logged into the account", scanResult.data.display_name);
|
|
319
|
-
}
|
|
320
|
-
else if (confirmResult.error_code == -13) {
|
|
319
|
+
if (confirmResult.error_code == -13) {
|
|
321
320
|
if (callback) {
|
|
322
321
|
callback({
|
|
323
322
|
type: LoginQRCallbackEventType.QRCodeDeclined,
|
|
@@ -338,21 +337,30 @@ export async function loginQR(ctx, options, callback) {
|
|
|
338
337
|
}
|
|
339
338
|
return;
|
|
340
339
|
}
|
|
341
|
-
else {
|
|
340
|
+
else if (confirmResult.error_code == -14) {
|
|
341
|
+
// Ma het han o phia Zalo truoc khi xac nhan
|
|
342
|
+
return handleQrTimeout();
|
|
343
|
+
}
|
|
344
|
+
else if (confirmResult.error_code != 0) {
|
|
342
345
|
return reject(new ZaloApiError(`An error has occurred.\nResponse: ${JSON.stringify(confirmResult, null, 2)}`));
|
|
343
346
|
}
|
|
347
|
+
const checkSessionResult = await checkSession(ctx, (_b = confirmResult.data) === null || _b === void 0 ? void 0 : _b.url);
|
|
348
|
+
if (!checkSessionResult)
|
|
349
|
+
return resolve(null);
|
|
350
|
+
logger(ctx).info("Successfully logged into the account", scanResult.data.display_name);
|
|
344
351
|
const userInfo = await getUserInfo(ctx);
|
|
345
352
|
if (!userInfo || !userInfo.data)
|
|
346
353
|
return reject(new ZaloApiError("Can't get account info"));
|
|
347
354
|
if (!userInfo.data.logged)
|
|
348
355
|
return reject(new ZaloApiError("Can't login"));
|
|
349
|
-
clearTimeout(timeout);
|
|
350
356
|
resolve({
|
|
351
357
|
cookies: ctx.cookie.toJSON().cookies,
|
|
352
358
|
userInfo: userInfo.data.info,
|
|
353
359
|
});
|
|
354
360
|
}
|
|
355
361
|
catch (error) {
|
|
362
|
+
controller.abort();
|
|
363
|
+
clearQrTimeout();
|
|
356
364
|
reject(error);
|
|
357
365
|
}
|
|
358
366
|
});
|
|
@@ -40,7 +40,7 @@ async function loadLoginPage(ctx) {
|
|
|
40
40
|
}
|
|
41
41
|
async function getLoginInfo(ctx, version) {
|
|
42
42
|
const form = new URLSearchParams();
|
|
43
|
-
form.append("continue", "https://zalo.me/
|
|
43
|
+
form.append("continue", "https://chat.zalo.me/");
|
|
44
44
|
form.append("v", version);
|
|
45
45
|
return await utils.request(ctx, "https://id.zalo.me/account/logininfo", {
|
|
46
46
|
headers: {
|
|
@@ -54,33 +54,7 @@ async function getLoginInfo(ctx, version) {
|
|
|
54
54
|
"sec-fetch-dest": "empty",
|
|
55
55
|
"sec-fetch-mode": "cors",
|
|
56
56
|
"sec-fetch-site": "same-origin",
|
|
57
|
-
Referer: "https://id.zalo.me/account?continue=https%3A%2F%
|
|
58
|
-
"Referrer-Policy": "strict-origin-when-cross-origin",
|
|
59
|
-
},
|
|
60
|
-
body: form,
|
|
61
|
-
method: "POST",
|
|
62
|
-
})
|
|
63
|
-
.then((res) => res.json())
|
|
64
|
-
.catch(utils.logger(ctx).error);
|
|
65
|
-
}
|
|
66
|
-
async function verifyClient(ctx, version) {
|
|
67
|
-
const form = new URLSearchParams();
|
|
68
|
-
form.append("type", "device");
|
|
69
|
-
form.append("continue", "https://zalo.me/pc");
|
|
70
|
-
form.append("v", version);
|
|
71
|
-
return await utils.request(ctx, "https://id.zalo.me/account/verify-client", {
|
|
72
|
-
headers: {
|
|
73
|
-
accept: "*/*",
|
|
74
|
-
"accept-language": "vi-VN,vi;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-US;q=0.6,en;q=0.5",
|
|
75
|
-
"content-type": "application/x-www-form-urlencoded",
|
|
76
|
-
priority: "u=1, i",
|
|
77
|
-
"sec-ch-ua": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
|
|
78
|
-
"sec-ch-ua-mobile": "?0",
|
|
79
|
-
"sec-ch-ua-platform": '"Windows"',
|
|
80
|
-
"sec-fetch-dest": "empty",
|
|
81
|
-
"sec-fetch-mode": "cors",
|
|
82
|
-
"sec-fetch-site": "same-origin",
|
|
83
|
-
Referer: "https://id.zalo.me/account?continue=https%3A%2F%2Fzalo.me%2Fpc",
|
|
57
|
+
Referer: "https://id.zalo.me/account?continue=https%3A%2F%2Fchat.zalo.me%2F",
|
|
84
58
|
"Referrer-Policy": "strict-origin-when-cross-origin",
|
|
85
59
|
},
|
|
86
60
|
body: form,
|
|
@@ -91,7 +65,9 @@ async function verifyClient(ctx, version) {
|
|
|
91
65
|
}
|
|
92
66
|
async function generate(ctx, version) {
|
|
93
67
|
const form = new URLSearchParams();
|
|
94
|
-
|
|
68
|
+
// 1 = client tu ve QR (luong thiet bi tin cay cua Zalo Web), 0 = dung anh QR may chu tra
|
|
69
|
+
form.append("cliGenQR", "0");
|
|
70
|
+
form.append("continue", "https://chat.zalo.me/");
|
|
95
71
|
form.append("v", version);
|
|
96
72
|
return await utils.request(ctx, "https://id.zalo.me/account/authen/qr/generate", {
|
|
97
73
|
headers: {
|
|
@@ -105,7 +81,7 @@ async function generate(ctx, version) {
|
|
|
105
81
|
"sec-fetch-dest": "empty",
|
|
106
82
|
"sec-fetch-mode": "cors",
|
|
107
83
|
"sec-fetch-site": "same-origin",
|
|
108
|
-
Referer: "https://id.zalo.me/account?continue=https%3A%2F%
|
|
84
|
+
Referer: "https://id.zalo.me/account?continue=https%3A%2F%2Fchat.zalo.me%2F",
|
|
109
85
|
"Referrer-Policy": "strict-origin-when-cross-origin",
|
|
110
86
|
},
|
|
111
87
|
body: form,
|
|
@@ -141,7 +117,7 @@ async function waitingScan(ctx, version, code, signal) {
|
|
|
141
117
|
method: "POST",
|
|
142
118
|
signal,
|
|
143
119
|
})
|
|
144
|
-
.then((res) => res.json())
|
|
120
|
+
.then((res) => (res.status == 502 || res.status == 504 ? { error_code: 8 } : res.json()))
|
|
145
121
|
.then((data) => {
|
|
146
122
|
if (data.error_code == 8) {
|
|
147
123
|
return waitingScan(ctx, version, code, signal);
|
|
@@ -156,8 +132,6 @@ async function waitingScan(ctx, version, code, signal) {
|
|
|
156
132
|
async function waitingConfirm(ctx, version, code, signal) {
|
|
157
133
|
const form = new URLSearchParams();
|
|
158
134
|
form.append("code", code);
|
|
159
|
-
form.append("gToken", "");
|
|
160
|
-
form.append("gAction", "CONFIRM_QR");
|
|
161
135
|
form.append("continue", "https://chat.zalo.me/");
|
|
162
136
|
form.append("v", version);
|
|
163
137
|
utils.logger(ctx).info("Please confirm on your phone");
|
|
@@ -180,7 +154,7 @@ async function waitingConfirm(ctx, version, code, signal) {
|
|
|
180
154
|
method: "POST",
|
|
181
155
|
signal,
|
|
182
156
|
})
|
|
183
|
-
.then((res) => res.json())
|
|
157
|
+
.then((res) => (res.status == 502 || res.status == 504 ? { error_code: 8 } : res.json()))
|
|
184
158
|
.then((data) => {
|
|
185
159
|
if (data.error_code == 8) {
|
|
186
160
|
return waitingConfirm(ctx, version, code, signal);
|
|
@@ -192,8 +166,8 @@ async function waitingConfirm(ctx, version, code, signal) {
|
|
|
192
166
|
utils.logger(ctx).error(e);
|
|
193
167
|
});
|
|
194
168
|
}
|
|
195
|
-
async function checkSession(ctx) {
|
|
196
|
-
return await utils.request(ctx, "https://id.zalo.me/account/checksession?continue=https%3A%2F%2Fchat.zalo.me%2Findex.html", {
|
|
169
|
+
async function checkSession(ctx, url) {
|
|
170
|
+
return await utils.request(ctx, url !== null && url !== void 0 ? url : "https://id.zalo.me/account/checksession?continue=https%3A%2F%2Fchat.zalo.me%2Findex.html", {
|
|
197
171
|
headers: {
|
|
198
172
|
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
|
|
199
173
|
"accept-language": "vi-VN,vi;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-US;q=0.6,en;q=0.5",
|
|
@@ -236,11 +210,26 @@ async function loginQR(ctx, options, callback) {
|
|
|
236
210
|
ctx.cookie = new toughCookie.CookieJar();
|
|
237
211
|
ctx.userAgent = options.userAgent;
|
|
238
212
|
return new Promise(async (resolve, reject) => {
|
|
239
|
-
var _a;
|
|
213
|
+
var _a, _b;
|
|
214
|
+
// Khai bao truoc moi callback: abort() co the duoc goi ngay trong callback QRCodeGenerated
|
|
215
|
+
const controller = new AbortController();
|
|
216
|
+
let qrTimeout = null;
|
|
217
|
+
function clearQrTimeout() {
|
|
218
|
+
if (qrTimeout) {
|
|
219
|
+
clearTimeout(qrTimeout);
|
|
220
|
+
qrTimeout = null;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
240
223
|
try {
|
|
241
224
|
function retry() {
|
|
225
|
+
controller.abort();
|
|
226
|
+
clearQrTimeout();
|
|
242
227
|
return resolve(loginQR(ctx, options, callback));
|
|
243
228
|
}
|
|
229
|
+
function abort() {
|
|
230
|
+
controller.abort();
|
|
231
|
+
clearQrTimeout();
|
|
232
|
+
}
|
|
244
233
|
if (ctx.options.logging)
|
|
245
234
|
console.log();
|
|
246
235
|
const loginVersion = await loadLoginPage(ctx);
|
|
@@ -248,37 +237,12 @@ async function loginQR(ctx, options, callback) {
|
|
|
248
237
|
return reject(new ZaloApiError.ZaloApiError("Cannot get API login version"));
|
|
249
238
|
utils.logger(ctx).info("Got login version:", loginVersion);
|
|
250
239
|
await getLoginInfo(ctx, loginVersion);
|
|
251
|
-
await verifyClient(ctx, loginVersion);
|
|
252
240
|
const qrGenResult = await generate(ctx, loginVersion);
|
|
253
241
|
if (!qrGenResult || !qrGenResult.data)
|
|
254
242
|
return reject(new ZaloApiError.ZaloApiError(`Unable to generate QRCode\nResponse: ${JSON.stringify(qrGenResult, null, 2)}`));
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
type: exports.LoginQRCallbackEventType.QRCodeGenerated,
|
|
259
|
-
data: Object.assign(Object.assign({}, qrGenResult.data), { image: qrGenResult.data.image.replace(/^data:image\/png;base64,/, "") }),
|
|
260
|
-
actions: {
|
|
261
|
-
async saveToFile(qrPath) {
|
|
262
|
-
var _a;
|
|
263
|
-
if (qrPath === void 0) { qrPath = (_a = options.qrPath) !== null && _a !== void 0 ? _a : "qr.png"; }
|
|
264
|
-
await saveQRCodeToFile(qrPath, qrData.image.replace(/^data:image\/png;base64,/, ""));
|
|
265
|
-
utils.logger(ctx).info("Scan the QR code at", `'${qrPath}'`, "to proceed with login");
|
|
266
|
-
},
|
|
267
|
-
retry,
|
|
268
|
-
abort() {
|
|
269
|
-
controller.abort();
|
|
270
|
-
},
|
|
271
|
-
},
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
else {
|
|
275
|
-
const qrPath = (_a = options.qrPath) !== null && _a !== void 0 ? _a : "qr.png";
|
|
276
|
-
await saveQRCodeToFile(qrPath, qrData.image.replace(/^data:image\/png;base64,/, ""));
|
|
277
|
-
utils.logger(ctx).info("Scan the QR code at", `'${qrPath}'`, "to proceed with login");
|
|
278
|
-
}
|
|
279
|
-
const controller = new AbortController();
|
|
280
|
-
const timeout = setTimeout(() => {
|
|
281
|
-
controller.abort();
|
|
243
|
+
let qrData = qrGenResult.data;
|
|
244
|
+
function handleQrTimeout() {
|
|
245
|
+
abort();
|
|
282
246
|
utils.logger(ctx).info("QR expired!");
|
|
283
247
|
if (callback) {
|
|
284
248
|
callback({
|
|
@@ -295,32 +259,67 @@ async function loginQR(ctx, options, callback) {
|
|
|
295
259
|
else {
|
|
296
260
|
resolve(loginQR(ctx, options));
|
|
297
261
|
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
262
|
+
}
|
|
263
|
+
let scanResult;
|
|
264
|
+
// Zalo co the yeu cau quet lan hai: tra ma QR moi (status 4) -> hien ma moi va cho quet tiep
|
|
265
|
+
while (true) {
|
|
266
|
+
const currentQr = qrData;
|
|
267
|
+
if (callback) {
|
|
268
|
+
callback({
|
|
269
|
+
type: exports.LoginQRCallbackEventType.QRCodeGenerated,
|
|
270
|
+
data: Object.assign(Object.assign({}, currentQr), { image: currentQr.image.replace(/^data:image\/png;base64,/, "") }),
|
|
271
|
+
actions: {
|
|
272
|
+
async saveToFile(qrPath) {
|
|
273
|
+
var _a;
|
|
274
|
+
if (qrPath === void 0) { qrPath = (_a = options.qrPath) !== null && _a !== void 0 ? _a : "qr.png"; }
|
|
275
|
+
await saveQRCodeToFile(qrPath, currentQr.image.replace(/^data:image\/png;base64,/, ""));
|
|
276
|
+
utils.logger(ctx).info("Scan the QR code at", `'${qrPath}'`, "to proceed with login");
|
|
277
|
+
},
|
|
278
|
+
retry,
|
|
279
|
+
abort,
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
const qrPath = (_a = options.qrPath) !== null && _a !== void 0 ? _a : "qr.png";
|
|
285
|
+
await saveQRCodeToFile(qrPath, currentQr.image.replace(/^data:image\/png;base64,/, ""));
|
|
286
|
+
utils.logger(ctx).info("Scan the QR code at", `'${qrPath}'`, "to proceed with login");
|
|
287
|
+
}
|
|
288
|
+
if (controller.signal.aborted)
|
|
289
|
+
return resolve(null);
|
|
290
|
+
clearQrTimeout();
|
|
291
|
+
qrTimeout = setTimeout(handleQrTimeout, 100000);
|
|
292
|
+
scanResult = await waitingScan(ctx, loginVersion, currentQr.code, controller.signal);
|
|
293
|
+
if (!scanResult || !scanResult.data)
|
|
294
|
+
return resolve(null);
|
|
295
|
+
if (scanResult.data.status == 5)
|
|
296
|
+
return reject(new ZaloApiError.ZaloApiError(`QRCode login failed.\nResponse: ${JSON.stringify(scanResult, null, 2)}`));
|
|
297
|
+
if (scanResult.data.status != 4)
|
|
298
|
+
break;
|
|
299
|
+
const { code, token, image } = scanResult.data;
|
|
300
|
+
if (!code || !token || !image)
|
|
301
|
+
return reject(new ZaloApiError.ZaloApiError(`A second scan is required but no QR code was returned.\nResponse: ${JSON.stringify(scanResult, null, 2)}`));
|
|
302
|
+
utils.logger(ctx).info("A second scan is required");
|
|
303
|
+
qrData = Object.assign(Object.assign({}, qrData), { code, token, image });
|
|
304
|
+
}
|
|
302
305
|
if (callback) {
|
|
303
306
|
callback({
|
|
304
307
|
type: exports.LoginQRCallbackEventType.QRCodeScanned,
|
|
305
308
|
data: scanResult.data,
|
|
306
309
|
actions: {
|
|
307
310
|
retry,
|
|
308
|
-
abort
|
|
309
|
-
controller.abort();
|
|
310
|
-
},
|
|
311
|
+
abort,
|
|
311
312
|
},
|
|
312
313
|
});
|
|
313
314
|
}
|
|
314
|
-
|
|
315
|
-
|
|
315
|
+
// Da quet: het han 100 giay khong cat ngang buoc xac nhan nua
|
|
316
|
+
clearQrTimeout();
|
|
317
|
+
if (controller.signal.aborted)
|
|
316
318
|
return resolve(null);
|
|
317
|
-
const
|
|
318
|
-
if (!
|
|
319
|
+
const confirmResult = await waitingConfirm(ctx, loginVersion, qrData.code, controller.signal);
|
|
320
|
+
if (!confirmResult)
|
|
319
321
|
return resolve(null);
|
|
320
|
-
if (confirmResult.error_code ==
|
|
321
|
-
utils.logger(ctx).info("Successfully logged into the account", scanResult.data.display_name);
|
|
322
|
-
}
|
|
323
|
-
else if (confirmResult.error_code == -13) {
|
|
322
|
+
if (confirmResult.error_code == -13) {
|
|
324
323
|
if (callback) {
|
|
325
324
|
callback({
|
|
326
325
|
type: exports.LoginQRCallbackEventType.QRCodeDeclined,
|
|
@@ -341,21 +340,30 @@ async function loginQR(ctx, options, callback) {
|
|
|
341
340
|
}
|
|
342
341
|
return;
|
|
343
342
|
}
|
|
344
|
-
else {
|
|
343
|
+
else if (confirmResult.error_code == -14) {
|
|
344
|
+
// Ma het han o phia Zalo truoc khi xac nhan
|
|
345
|
+
return handleQrTimeout();
|
|
346
|
+
}
|
|
347
|
+
else if (confirmResult.error_code != 0) {
|
|
345
348
|
return reject(new ZaloApiError.ZaloApiError(`An error has occurred.\nResponse: ${JSON.stringify(confirmResult, null, 2)}`));
|
|
346
349
|
}
|
|
350
|
+
const checkSessionResult = await checkSession(ctx, (_b = confirmResult.data) === null || _b === void 0 ? void 0 : _b.url);
|
|
351
|
+
if (!checkSessionResult)
|
|
352
|
+
return resolve(null);
|
|
353
|
+
utils.logger(ctx).info("Successfully logged into the account", scanResult.data.display_name);
|
|
347
354
|
const userInfo = await getUserInfo(ctx);
|
|
348
355
|
if (!userInfo || !userInfo.data)
|
|
349
356
|
return reject(new ZaloApiError.ZaloApiError("Can't get account info"));
|
|
350
357
|
if (!userInfo.data.logged)
|
|
351
358
|
return reject(new ZaloApiError.ZaloApiError("Can't login"));
|
|
352
|
-
clearTimeout(timeout);
|
|
353
359
|
resolve({
|
|
354
360
|
cookies: ctx.cookie.toJSON().cookies,
|
|
355
361
|
userInfo: userInfo.data.info,
|
|
356
362
|
});
|
|
357
363
|
}
|
|
358
364
|
catch (error) {
|
|
365
|
+
controller.abort();
|
|
366
|
+
clearQrTimeout();
|
|
359
367
|
reject(error);
|
|
360
368
|
}
|
|
361
369
|
});
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zalo-api-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Hien Nguyen Juno",
|
|
6
6
|
"email": "hiennguyen270995@gmail.com",
|
|
7
7
|
"url": "https://github.com/hiennguyen270995"
|
|
8
8
|
},
|
|
9
9
|
"license": "MIT",
|
|
10
|
-
"description": "fork từ zalo-api-final - API Zalo cá nhân hoàn chỉnh cho JavaScript. Xây dựng node n8n, backend quản lý nhiều tài khoản Zalo, phần mềm bán hàng, quản trị Zalo cho công ty tăng doanh số. Gửi tin nhắn, tạo nhóm, quản lý bạn bè, chặn/bỏ chặn người dùng, chấp nhận lời mời kết bạn, nhận webhook, lấy danh sách bạn bè, quản lý nhóm và tự động hóa chat Zalo. Hỗ trợ multi-account management cho doanh nghiệp. Thư viện Zalo API cuối cùng và tốt nhất.",
|
|
10
|
+
"description": "fork, maintain trực tiếp từ zalo-api-final - API Zalo cá nhân hoàn chỉnh cho JavaScript. Xây dựng node n8n, backend quản lý nhiều tài khoản Zalo, phần mềm bán hàng, quản trị Zalo cho công ty tăng doanh số. Gửi tin nhắn, tạo nhóm, quản lý bạn bè, chặn/bỏ chặn người dùng, chấp nhận lời mời kết bạn, nhận webhook, lấy danh sách bạn bè, quản lý nhóm và tự động hóa chat Zalo. Hỗ trợ multi-account management cho doanh nghiệp. Thư viện Zalo API cuối cùng và tốt nhất.",
|
|
11
11
|
"main": "dist/index.js",
|
|
12
12
|
"type": "module",
|
|
13
13
|
"exports": {
|
|
@@ -29,80 +29,30 @@
|
|
|
29
29
|
},
|
|
30
30
|
"repository": {
|
|
31
31
|
"type": "git",
|
|
32
|
-
"url": "git+https://github.com/
|
|
32
|
+
"url": "git+https://github.com/bach205/zalo-api.git"
|
|
33
33
|
},
|
|
34
|
-
"homepage": "https://github.com/
|
|
34
|
+
"homepage": "https://github.com/bach205/zalo-api",
|
|
35
35
|
"bugs": {
|
|
36
|
-
"url": "https://github.com/
|
|
36
|
+
"url": "https://github.com/bach205/zalo-api/issues"
|
|
37
37
|
},
|
|
38
38
|
"engines": {
|
|
39
39
|
"node": ">=18.0.0"
|
|
40
40
|
},
|
|
41
41
|
"keywords": [
|
|
42
|
-
"zalo-api-
|
|
42
|
+
"zalo-api-sdk",
|
|
43
43
|
"zalo",
|
|
44
44
|
"zalo-api",
|
|
45
|
-
"api-zalo",
|
|
46
45
|
"zalo-chat",
|
|
47
|
-
"
|
|
48
|
-
"zalo-
|
|
49
|
-
"
|
|
46
|
+
"zalo-bot",
|
|
47
|
+
"zalo-automation",
|
|
48
|
+
"zalo cá nhân",
|
|
50
49
|
"n8n-node",
|
|
51
|
-
"
|
|
52
|
-
"xay-dung-node-n8n",
|
|
53
|
-
"xây dựng node n8n",
|
|
54
|
-
"backend-zalo",
|
|
55
|
-
"quan-ly-nhieu-tai-khoan",
|
|
56
|
-
"quản lý nhiều tài khoản",
|
|
50
|
+
"webhook",
|
|
57
51
|
"multi-account",
|
|
58
|
-
"
|
|
59
|
-
"quan-ly-tai-khoan-zalo",
|
|
60
|
-
"quản lý tài khoản zalo",
|
|
61
|
-
"phan-mem-ban-hang",
|
|
62
|
-
"phần mềm bán hàng",
|
|
63
|
-
"ban-hang-zalo",
|
|
64
|
-
"bán hàng zalo",
|
|
65
|
-
"quan-tri-zalo",
|
|
66
|
-
"quản trị zalo",
|
|
67
|
-
"zalo-doanh-nghiep",
|
|
68
|
-
"zalo doanh nghiệp",
|
|
69
|
-
"tang-doanh-so",
|
|
70
|
-
"tăng doanh số",
|
|
71
|
-
"zalo-marketing",
|
|
72
|
-
"marketing-zalo",
|
|
73
|
-
"zalo-automation",
|
|
74
|
-
"tu-dong-hoa-zalo",
|
|
52
|
+
"quản lý nhiều tài khoản",
|
|
75
53
|
"tự động hóa zalo",
|
|
76
|
-
"gui-tin-nhan",
|
|
77
|
-
"gửi tin nhắn",
|
|
78
|
-
"tao-nhom",
|
|
79
|
-
"tạo nhóm",
|
|
80
|
-
"quan-ly-ban-be",
|
|
81
|
-
"quản lý bạn bè",
|
|
82
|
-
"chan-nguoi-dung",
|
|
83
|
-
"chặn người dùng",
|
|
84
|
-
"bo-chan",
|
|
85
|
-
"bỏ chặn",
|
|
86
|
-
"chap-nhan-ban-be",
|
|
87
|
-
"chấp nhận bạn bè",
|
|
88
|
-
"loi-moi-ket-ban",
|
|
89
|
-
"lời mời kết bạn",
|
|
90
|
-
"webhook",
|
|
91
|
-
"danh-sach-ban-be",
|
|
92
|
-
"danh sách bạn bè",
|
|
93
|
-
"quan-ly-nhom",
|
|
94
|
-
"quản lý nhóm",
|
|
95
|
-
"zalo-bot",
|
|
96
|
-
"bot-zalo",
|
|
97
54
|
"chatbot",
|
|
98
|
-
"
|
|
99
|
-
"final",
|
|
100
|
-
"cuoi-cung",
|
|
101
|
-
"hoan-chinh",
|
|
102
|
-
"business",
|
|
103
|
-
"enterprise",
|
|
104
|
-
"sales",
|
|
105
|
-
"revenue"
|
|
55
|
+
"bot zalo"
|
|
106
56
|
],
|
|
107
57
|
"dependencies": {
|
|
108
58
|
"crypto-js": "^4.2.0",
|
|
File without changes
|