-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathpair.js
161 lines (136 loc) · 5.84 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const { giftedid } = require('./id');
const express = require('express');
const fs = require('fs');
let router = express.Router();
const pino = require("pino");
const { Storage } = require("megajs");
const {
default: Gifted_Tech,
useMultiFileAuthState,
delay,
makeCacheableSignalKeyStore,
Browsers
} = require("@whiskeysockets/baileys");
// Function to generate a random Mega ID
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}`;
}
// Function to upload credentials to Mega
async function uploadCredsToMega(credsPath) {
try {
const storage = await new Storage({
email: '[email protected]', // Your Mega A/c Email Here
password: '' // Your Mega A/c Password Here
}).ready;
console.log('Mega storage initialized.');
if (!fs.existsSync(credsPath)) {
throw new Error(`File not found: ${credsPath}`);
}
const fileSize = fs.statSync(credsPath).size;
const uploadResult = await storage.upload({
name: `${randomMegaId()}.json`,
size: fileSize
}, fs.createReadStream(credsPath)).complete;
console.log('Session successfully uploaded to Mega.');
const fileNode = storage.files[uploadResult.nodeId];
const megaUrl = await fileNode.link();
console.log(`Session Url: ${megaUrl}`);
return megaUrl;
} catch (error) {
console.error('Error uploading to Mega:', error);
throw error;
}
}
// Function to remove a file
function removeFile(FilePath) {
if (!fs.existsSync(FilePath)) return false;
fs.rmSync(FilePath, { recursive: true, force: true });
}
// Router to handle pairing code generation
router.get('/', async (req, res) => {
const id = giftedid();
let num = req.query.number;
async function GIFTED_PAIR_CODE() {
const { state, saveCreds } = await useMultiFileAuthState('./temp/' + id);
try {
let Gifted = Gifted_Tech({
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 (!Gifted.authState.creds.registered) {
await delay(1500);
num = num.replace(/[^0-9]/g, '');
const code = await Gifted.requestPairingCode(num);
console.log(`Your Code: ${code}`);
if (!res.headersSent) {
res.send({ code });
}
}
Gifted.ev.on('creds.update', saveCreds);
Gifted.ev.on("connection.update", async (s) => {
const { connection, lastDisconnect } = s;
if (connection === "open") {
await delay(5000);
const filePath = __dirname + `/temp/${id}/creds.json`;
if (!fs.existsSync(filePath)) {
console.error("File not found:", filePath);
return;
}
const megaUrl = await uploadCredsToMega(filePath);
const sid = megaUrl.includes("https://mega.nz/file/")
? 'Gifted~' + megaUrl.split("https://mega.nz/file/")[1]
: 'Error: Invalid URL';
console.log(`Session ID: ${sid}`);
const session = await Gifted.sendMessage(Gifted.user.id, { text: sid });
const GIFTED_TEXT = `
*✅sᴇssɪᴏɴ ɪᴅ ɢᴇɴᴇʀᴀᴛᴇᴅ✅*
______________________________
╔════◇
║『 𝐘𝐎𝐔'𝐕𝐄 𝐂𝐇𝐎𝐒𝐄𝐍 𝐆𝐈𝐅𝐓𝐄𝐃 𝐌𝐃 』
║ You've Completed the First Step
║ to Deploy a Whatsapp Bot.
╚══════════════╝
╔═════◇
║ 『••• 𝗩𝗶𝘀𝗶𝘁 𝗙𝗼𝗿 𝗛𝗲𝗹𝗽 •••』
║❒ 𝐓𝐮𝐭𝐨𝐫𝐢𝐚𝐥: _youtube.com/@giftedtechnexus_
║❒ 𝐎𝐰𝐧𝐞𝐫: _https://t.me/mouricedevs_
║❒ 𝐑𝐞𝐩𝐨: _https://github.com/mouricedevs/giiftedmd_
║❒ 𝐖𝐚𝐂𝐡𝐚𝐧𝐧𝐞𝐥: _https://whatsapp.com/channel/0029VaYauR9ISTkHTj4xvi1l_
║ 💜💜💜
╚══════════════╝
𝗚𝗜𝗙𝗧𝗘𝗗-𝗠𝗗 𝗩𝗘𝗥𝗦𝗜𝗢𝗡 5.𝟬.𝟬
______________________________
Use your Session ID Above to Deploy your Bot.
Check on YouTube Channel for Deployment Procedure(Ensure you have Github Account and Billed Heroku Account First.)
Don't Forget To Give Star⭐ To My Repo`;
await Gifted.sendMessage(Gifted.user.id, { text: GIFTED_TEXT }, { quoted: session });
await delay(100);
await Gifted.ws.close();
return removeFile('./temp/' + id);
} else if (connection === "close" && lastDisconnect && lastDisconnect.error && lastDisconnect.error.output.statusCode !== 401) {
await delay(10000);
GIFTED_PAIR_CODE();
}
});
} catch (err) {
console.error("Service Has Been Restarted:", err);
removeFile('./temp/' + id);
if (!res.headersSent) {
res.send({ code: "Service is Currently Unavailable" });
}
}
}
await GIFTED_PAIR_CODE();
});
module.exports = router;