Update app.js
Browse files
app.js
CHANGED
|
@@ -25,66 +25,81 @@ let isReconnecting = false;
|
|
| 25 |
|
| 26 |
|
| 27 |
async function startWhatsApp() {
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
console.log(
|
| 57 |
-
|
| 58 |
);
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
statusCode === DisconnectReason.loggedOut;
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
"π§Ή Session invalid. Removing auth folder..."
|
| 68 |
-
);
|
| 69 |
-
|
| 70 |
-
setTimeout(() => {
|
| 71 |
-
startWhatsApp();
|
| 72 |
-
}, 3000);
|
| 73 |
-
|
| 74 |
-
return;
|
| 75 |
-
}
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
}
|
| 89 |
}
|
| 90 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
async function startWhatsApp() {
|
| 28 |
+
try {
|
| 29 |
+
|
| 30 |
+
sock.ev.on("creds.update", saveCreds);
|
| 31 |
+
|
| 32 |
+
sock.ev.on("connection.update", async (update) => {
|
| 33 |
+
const {
|
| 34 |
+
connection,
|
| 35 |
+
lastDisconnect,
|
| 36 |
+
qr
|
| 37 |
+
} = update;
|
| 38 |
+
|
| 39 |
+
console.log("con", connection);
|
| 40 |
+
console.log("qr", qr);
|
| 41 |
+
console.log("------------");
|
| 42 |
+
if (qr) {
|
| 43 |
+
console.log("\nπ± Scan QR Code:\n");
|
| 44 |
+
qrcode.generate(qr, { small: true });
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
if (connection === "open") {
|
| 48 |
+
console.log("β
WhatsApp Connected");
|
| 49 |
+
isReconnecting = false;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
if (connection === "close") {
|
| 53 |
+
const statusCode =
|
| 54 |
+
lastDisconnect?.error?.output?.statusCode;
|
| 55 |
+
|
| 56 |
+
console.log(
|
| 57 |
+
`β Connection closed. Status: ${statusCode}`
|
| 58 |
+
);
|
| 59 |
+
|
| 60 |
+
const shouldLogout =
|
| 61 |
+
statusCode === 401 ||
|
| 62 |
+
statusCode === 405 ||
|
| 63 |
+
statusCode === DisconnectReason.loggedOut;
|
| 64 |
+
|
| 65 |
+
if (shouldLogout) {
|
| 66 |
console.log(
|
| 67 |
+
"π§Ή Session invalid. Removing auth folder..."
|
| 68 |
);
|
| 69 |
|
| 70 |
+
setTimeout(() => {
|
| 71 |
+
startWhatsApp();
|
| 72 |
+
}, 3000);
|
|
|
|
| 73 |
|
| 74 |
+
return;
|
| 75 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
if (!isReconnecting) {
|
| 78 |
+
isReconnecting = true;
|
| 79 |
|
| 80 |
+
console.log(
|
| 81 |
+
"π Reconnecting in 5 seconds..."
|
| 82 |
+
);
|
| 83 |
|
| 84 |
+
setTimeout(() => {
|
| 85 |
+
isReconnecting = false;
|
| 86 |
+
startWhatsApp();
|
| 87 |
+
}, 5000);
|
|
|
|
| 88 |
}
|
| 89 |
+
}
|
| 90 |
+
});
|
| 91 |
+
}
|
| 92 |
+
};
|
| 93 |
+
app.get("/", (req, res) => {
|
| 94 |
+
res.json({
|
| 95 |
+
status: "running"
|
| 96 |
+
});
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
app.listen(PORT, () => {
|
| 100 |
+
console.log(
|
| 101 |
+
`π Server running on port ${PORT}`
|
| 102 |
+
);
|
| 103 |
+
|
| 104 |
+
startWhatsApp();
|
| 105 |
+
});
|