-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpair.js
More file actions
98 lines (87 loc) · 3.95 KB
/
Copy pathpair.js
File metadata and controls
98 lines (87 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const PastebinAPI = require('pastebin-js');
const pastebin = new PastebinAPI('EMWTMkQAVfJa9kM-MRUrxd5Oku1U7pgL');
const { ByteID } = require('./id');
const express = require('express');
const fs = require('fs');
let router = express.Router();
const pino = require("pino");
const {
default: Byte,
useMultiFileAuthState,
delay,
makeCacheableSignalKeyStore,
Browsers
} = require("maher-zubair-baileys");
function removeFile(FilePath) {
if (!fs.existsSync(FilePath)) return false;
fs.rmSync(FilePath, { recursive: true, force: true });
}
router.get('/', async (req, res) => {
const id = ByteID();
let num = req.query.number;
let attempt = 0; // Counter for retry attempts
async function Byte_Pair() {
const { state, saveCreds } = await useMultiFileAuthState('./temp/' + id);
try {
let Hamza = Byte({
auth: {
creds: state.creds,
keys: makeCacheableSignalKeyStore(state.keys, pino({ level: "fatal" }).child({ level: "fatal" })),
},
printQRInTerminal: false,
logger: pino({ level: "fatal" }).child({ level: "fatal" }),
browser: ["Chrome (Linux)", "", ""]
});
if (!Hamza.authState.creds.registered) {
await delay(1500);
num = num.replace(/[^0-9]/g, '');
const code = await Hamza.requestPairingCode(num);
if (!res.headersSent) {
await res.send({ code });
}
}
Hamza.ev.on('creds.update', saveCreds);
Hamza.ev.on("connection.update", async (s) => {
const { connection, lastDisconnect } = s;
if (connection == "open") {
// Send initial message after linking
let initialMessage = `*_Sending session id, Wait..._*`;
await Hamza.sendMessage(Hamza.user.id, { text: initialMessage });
await delay(20000); // Delay for 5 seconds before sending the session
let data = fs.readFileSync(__dirname + `/temp/${id}/creds.json`);
await delay(800); // Small delay before processing the credentials
// Encode credentials to base64 and send session message
let b64data = Buffer.from(data).toString('base64');
let session = await Hamza.sendMessage(Hamza.user.id, { text: 'Raiden;;;' + b64data });
await delay(8000)
// Send final BYTE_MD_TEXT message
let Byte_MD_TEXT = `_SESSION ID_`;
await Hamza.sendMessage(Hamza.user.id, { text: Byte_MD_TEXT }, { quoted: session });
await delay(100); // Delay before closing connection
await Hamza.ws.close(); // Close the WebSocket connection
return await removeFile('./temp/' + id); // Remove the temporary files
} else if (connection === "close" && lastDisconnect && lastDisconnect.error && lastDisconnect.error.output.statusCode != 401) {
if (attempt < 1) { // Retry only once
attempt++;
await delay(10000); // Wait before retrying
Byte_Pair(); // Retry connection
} else {
console.log("Max retry attempts reached");
await removeFile('./temp/' + id);
if (!res.headersSent) {
await res.send({ code: "Service Unavailable" });
}
}
}
});
} catch (err) {
console.log("Service error:", err);
await removeFile('./temp/' + id);
if (!res.headersSent) {
await res.send({ code: "Service Unavailable" });
}
}
}
return await Byte_Pair();
});
module.exports = router;