forked from TOXIC-DEVIL/Leon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_session.js
130 lines (116 loc) · 4.64 KB
/
string_session.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
const { default: makeWASocket, useMultiFileAuthState, makeInMemoryStore, fetchLatestBaileysVersion, delay } = require('@whiskeysockets/baileys');
const readline = require('readline-sync');
const pino = require('pino');
const colors = require('colors');
const fs = require('fs');
colors.setTheme({
question: ['brightCyan', 'bold'],
option: ['brightYellow', 'italic'],
loading: ['brightWhite', 'italic'],
error: ['brightRed', 'bold'],
success: ['brightGreen', 'bold']
});
let store = makeInMemoryStore({
logger: pino().child({ level: 'silent', stream: 'store' })
});
const centerText = text => {
const width = process.stdout.columns;
const padding = Math.max(0, Math.floor((width - text.length) / 2));
return ' '.repeat(padding) + text;
};
let heading = `
${centerText('======================================')}
${centerText('Leon Installation')}
${centerText('======================================')}
`;
let infoBox = `
${centerText('┌─────────────────────────────────────────────┐')}
${centerText('│ Version: 2.0.0 │')}
${centerText('│ Channel: https://t.me/leonwabot │')}
${centerText('└─────────────────────────────────────────────┘')}
`;
let QRinstructionBox = `
${colors.option(centerText('! ATTENTION !'))}
${colors.option('1. QR Code will be regenerated every 30 seconds.')}
${colors.option('2. Scan the QR code with your WhatsApp > 3 dots > Linked Devices > Link a Device > Scan the QR > Code generated.')}
`;
let PCinstructionBox = `
${colors.option(centerText('! ATTENTION !'))}
${colors.option('1. Pairing code will be generated once.')}
${colors.option('2. Type the pairing code in WhatsApp > 3 dots > Linked Devices > Link a Device > Link with phone number after it is generated.')}
`;
process.stdout.write('\x1B[2J\x1B[0f');
console.log(heading);
while (true) {
console.log(colors.option('[0] Cancel'));
console.log(colors.option('[1] Scan QR Code (2 devices required)'));
console.log(colors.option('[2] Pair with WhatsApp number\n'));
let methodIndex = readline.question(colors.question('Choose a method for string session generation: '));
if (methodIndex == '0') {
console.log(colors.error('Leon installation process terminated!'));
process.exit(0);
}
else if (methodIndex == '1' || methodIndex == '2') {
process.stdout.write('\x1B[2J\x1B[0f');
console.log(heading);
console.log(methodIndex === '1' ? QRinstructionBox : PCinstructionBox);
Leon(methodIndex === '1');
break;
}
else {
console.log(colors.error('Choose a valid method, please enter 1, 2 or 0.\n'));
}
}
try {
fs.rmSync('./session/', { recursive: true, force: true });
} catch {}
async function Leon(qr) {
// let { version } = await fetchLatestBaileysVersion();
let { state, saveCreds } = await useMultiFileAuthState('./session/');
let sock = makeWASocket({
logger: pino({ level: 'silent' }),
printQRInTerminal: qr,
markOnlineOnConnect: false,
browser: ['Ubuntu', 'Chrome', '20.0.04'],
auth: state,
version: [2, 3000, 1015901307],
getMessage: async (key) => {
let jid = jidNormalizedUser(key.remoteJid);
let msg = await store.loadMessage(jid, key.id);
return msg?.message || "";
},
});
store.bind(sock.ev);
if (!qr && !sock.authState.creds.registered) {
let number = readline.question(colors.question('Enter your WhatsApp number with country code (eg: +91xxx): '));
try {
await delay(3000);
var code = await sock.requestPairingCode(number.replace(/\D/g, ''));
} catch (e) {
console.log(e.stack);
console.log(colors.error('Please enter a valid whatsapp number with country code!'));
process.exit(0);
}
console.log(colors.question('Your WhatsApp Pairing Code is:'));
console.log(colors.success(code));
}
sock.ev.on('connection.update', async (update) => {
let { connection } = update;
if (connection === 'open') {
await delay(3000);
let creds = fs.readFileSync('./session/creds.json', 'utf8');
let session = Buffer.from(creds, 'utf8').toString('base64');
await sock.sendMessage(sock.user.id, {
text: session
});
console.log(colors.question('\nYour session is:'));
console.log(session);
console.log(colors.success(`\nSession saved! (${__dirname + '/session/creds.json'})`));
process.exit(1);
} else if (connection === 'close') {
await Leon(qr);
}
});
sock.ev.on('messages.upsert', async () => {});
sock.ev.on('creds.update', saveCreds);
}