-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpair.js
143 lines (119 loc) · 5.56 KB
/
pair.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const express = require('express');
const fs = require('fs-extra');
const { exec } = require("child_process");
let router = express.Router();
const pino = require("pino");
const { Boom } = require("@hapi/boom");
const MESSAGE = process.env.MESSAGE || `
*SESSION GENERATED SUCCESSFULY* ✅
*Gɪᴠᴇ ᴀ ꜱᴛᴀʀ ᴛᴏ ʀᴇᴘᴏ ꜰᴏʀ ᴄᴏᴜʀᴀɢᴇ* 🌟
https://github.com/GuhailTechInfo/ULTRA-MD
*Sᴜᴘᴘᴏʀᴛ Gʀᴏᴜᴘ ꜰᴏʀ ϙᴜᴇʀʏ* 💭
https://t.me/GlobalBotInc
https://whatsapp.com/channel/0029VagJIAr3bbVBCpEkAM07
*Yᴏᴜ-ᴛᴜʙᴇ ᴛᴜᴛᴏʀɪᴀʟꜱ* 🪄
https://youtube.com/GlobalTechInfo
*ULTRA-MD--WHATTSAPP-BOT* 🥀
`;
const { upload } = require('./mega');
const {
default: makeWASocket,
useMultiFileAuthState,
delay,
makeCacheableSignalKeyStore,
Browsers,
DisconnectReason
} = require("@whiskeysockets/baileys");
// Ensure the directory is empty when the app starts
if (fs.existsSync('./auth_info_baileys')) {
fs.emptyDirSync(__dirname + '/auth_info_baileys');
}
router.get('/', async (req, res) => {
let num = req.query.number;
async function SUHAIL() {
const { state, saveCreds } = await useMultiFileAuthState(`./auth_info_baileys`);
try {
let Smd = makeWASocket({
auth: {
creds: state.creds,
keys: makeCacheableSignalKeyStore(state.keys, pino({ level: "fatal" }).child({ level: "fatal" })),
},
printQRInTerminal: false,
logger: pino({ level: "fatal" }).child({ level: "fatal" }),
browser: Browsers.macOS("Safari"),
});
if (!Smd.authState.creds.registered) {
await delay(1500);
num = num.replace(/[^0-9]/g, '');
const code = await Smd.requestPairingCode(num);
if (!res.headersSent) {
await res.send({ code });
}
}
Smd.ev.on('creds.update', saveCreds);
Smd.ev.on("connection.update", async (s) => {
const { connection, lastDisconnect } = s;
if (connection === "open") {
try {
await delay(10000);
if (fs.existsSync('./auth_info_baileys/creds.json'));
const auth_path = './auth_info_baileys/';
let user = Smd.user.id;
// Define randomMegaId function to generate random IDs
function randomMegaId(length = 6, numberLength = 4) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
const number = Math.floor(Math.random() * Math.pow(10, numberLength));
return `${result}${number}`;
}
// Upload credentials to Mega
const mega_url = await upload(fs.createReadStream(auth_path + 'creds.json'), `${randomMegaId()}.json`);
const Id_session = mega_url.replace('https://mega.nz/file/', '');
const Scan_Id = Id_session;
let msgsss = await Smd.sendMessage(user, { text: Scan_Id });
await Smd.sendMessage(user, { text: MESSAGE }, { quoted: msgsss });
await delay(1000);
try { await fs.emptyDirSync(__dirname + '/auth_info_baileys'); } catch (e) {}
} catch (e) {
console.log("Error during file upload or message send: ", e);
}
await delay(100);
await fs.emptyDirSync(__dirname + '/auth_info_baileys');
}
// Handle connection closures
if (connection === "close") {
let reason = new Boom(lastDisconnect?.error)?.output.statusCode;
if (reason === DisconnectReason.connectionClosed) {
console.log("Connection closed!");
} else if (reason === DisconnectReason.connectionLost) {
console.log("Connection Lost from Server!");
} else if (reason === DisconnectReason.restartRequired) {
console.log("Restart Required, Restarting...");
SUHAIL().catch(err => console.log(err));
} else if (reason === DisconnectReason.timedOut) {
console.log("Connection TimedOut!");
} else {
console.log('Connection closed with bot. Please run again.');
console.log(reason);
await delay(5000);
exec('pm2 restart qasim');
}
}
});
} catch (err) {
console.log("Error in SUHAIL function: ", err);
exec('pm2 restart qasim');
console.log("Service restarted due to error");
SUHAIL();
await fs.emptyDirSync(__dirname + '/auth_info_baileys');
if (!res.headersSent) {
await res.send({ code: "Try After Few Minutes" });
}
}
}
await SUHAIL();
});
module.exports = router;