-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathapp.js
More file actions
287 lines (235 loc) · 10.3 KB
/
Copy pathapp.js
File metadata and controls
287 lines (235 loc) · 10.3 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const chalk = require('chalk');
const fs = require('fs');
const readline = require('readline');
// ==========================================
// CONFIGURATION
// ==========================================
// Set your default country code here (without '+')
// This will replace the leading '0' in phone numbers.
// Example: '62' for Indonesia, '1' for US, '44' for UK, '91' for India
const DEFAULT_COUNTRY_CODE = '62';
// ==========================================
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const question = (query) => new Promise(resolve => rl.question(query, resolve));
class App {
constructor() {
this.client = null;
this.isAuthenticated = false;
}
clearScreen() {
console.clear();
console.log(chalk.cyan.bold('='.repeat(55)));
console.log(chalk.cyan.bold(' WHATSAPP NUMBER FILTER'));
console.log(chalk.cyan.bold('='.repeat(55) + '\n'));
}
async start() {
try {
this.clearScreen();
console.log(chalk.yellow(' Starting WhatsApp Web Client...'));
this.client = new Client({
authStrategy: new LocalAuth({
clientId: 'whatsapp-checker'
}),
puppeteer: {
args: ['--no-sandbox', '--disable-setuid-sandbox']
},
webVersionCache: {
type: 'local'
}
});
this.client.on('qr', (qr) => {
console.log(chalk.cyan('\n📱 Scan the QR code using your primary device:\n'));
qrcode.generate(qr, { small: true });
});
this.client.on('authenticated', () => {
console.log(`${chalk.green('✓')} Authentication successful`);
});
this.client.on('ready', async () => {
this.isAuthenticated = true;
const clientInfo = this.client.info;
this.clearScreen();
console.log(`${chalk.green('✓')} Client connected successfully`);
console.log(`${chalk.cyan('•')} Account : ${chalk.white(clientInfo?.pushname || 'Unknown')}`);
console.log(`${chalk.cyan('•')} Number : ${chalk.white(clientInfo?.wid?.user || 'Unknown')}\n`);
await new Promise(resolve => setTimeout(resolve, 1500));
this.showMenu();
});
this.client.on('auth_failure', (msg) => {
console.log(`\n${chalk.bgRed.white.bold(' ✗ ERROR ')} Authentication failed: ${msg}`);
this.isAuthenticated = false;
});
this.client.on('disconnected', (reason) => {
console.log(`\n${chalk.bgYellow.black.bold(' ! WARNING ')} Client disconnected: ${reason}`);
this.isAuthenticated = false;
console.log(chalk.gray('Type anything and press Enter to exit...'));
});
await this.client.initialize();
} catch (error) {
console.error(`\n${chalk.bgRed.white.bold(' ✗ FATAL ')} Failed to start application:`, error.message);
}
}
async showMenu() {
this.clearScreen();
console.log(chalk.yellow.bold(' MAIN MENU '));
console.log(chalk.gray('-------------------------------------------------------'));
console.log(` ${chalk.cyan.bold('[1]')} Check Numbers from File ${chalk.gray('(numbers.txt)')}`);
console.log(` ${chalk.cyan.bold('[2]')} Check Number Manually`);
console.log(` ${chalk.cyan.bold('[3]')} Logout Device`);
console.log(` ${chalk.cyan.bold('[4]')} Exit Application`);
console.log(chalk.gray('-------------------------------------------------------\n'));
const answer = await question(chalk.white.bold(' ❯ Select menu (1-4): '));
switch (answer.trim()) {
case '1':
await this.checkNumbersTxt();
break;
case '2':
await this.checkManual();
break;
case '3':
await this.logoutDevice();
break;
case '4':
console.log(chalk.green('\n Exiting application. Goodbye!\n'));
await this.client.destroy();
process.exit(0);
break;
default:
console.log(chalk.red('\n✗ Invalid selection! Please try again.'));
await new Promise(resolve => setTimeout(resolve, 1500));
this.showMenu();
}
}
formatNumber(number) {
// Remove all non-numeric characters (like spaces, +, -, etc)
let formattedNumber = number.toString().replace(/\D/g, '');
// If the number starts with '0', replace it with the default country code
if (formattedNumber.startsWith('0')) {
formattedNumber = DEFAULT_COUNTRY_CODE + formattedNumber.substring(1);
}
return formattedNumber;
}
async checkNumbersTxt() {
try {
this.clearScreen();
const filesTxt = 'numbers.txt';
const reportTxt = 'active_numbers.txt';
if (!fs.existsSync(filesTxt)) {
console.log(`${chalk.bgRed.white.bold(' ✗ ERROR ')} File ${chalk.yellow(filesTxt)} not found`);
await question(chalk.gray('\nPress Enter to return to menu...'));
return this.showMenu();
}
const numberRaw = fs.readFileSync(filesTxt, 'utf8');
const numberlist = numberRaw
.replace(/\r/g, ' ')
.replace(/\//g, '')
.replace(/\n/g, '')
.replace(/^\s*/, '')
.split(' ')
.filter(num => num.trim() !== '');
if (numberlist.length === 0) {
console.log(`${chalk.bgYellow.black.bold(' ! WARNING ')} File ${chalk.yellow(filesTxt)} is empty`);
await question(chalk.gray('\nPress Enter to return to menu...'));
return this.showMenu();
}
console.log(chalk.yellow.bold(` CHECKING ${numberlist.length} NUMBERS... \n`));
// Tabular Header
console.log(chalk.cyan.bold(` ${'PHONE NUMBER'.padEnd(22)} | STATUS `));
console.log(chalk.gray('-------------------------------------------------------'));
let activeCount = 0;
let activeNumbers = [];
for (let i = 0; i < numberlist.length; i++) {
const number = numberlist[i];
try {
const formattedNumber = this.formatNumber(number);
const formattedTarget = formattedNumber + '@c.us';
const isRegistered = await this.client.isRegisteredUser(formattedTarget);
if (isRegistered) {
console.log(` ${chalk.white(number.padEnd(22))} | ${chalk.green.bold('ACTIVE')} `);
activeNumbers.push(number);
activeCount++;
} else {
console.log(` ${chalk.gray(number.padEnd(22))} | ${chalk.bgRed.white.bold(' UNREGISTERED ')} `);
}
} catch (error) {
console.log(` ${chalk.red(number.padEnd(22))} | ${chalk.bgRed.white.bold(' ERROR ')} `);
}
if (i < numberlist.length - 1) {
await new Promise(resolve => setTimeout(resolve, 3000));
}
}
console.log(chalk.gray('-------------------------------------------------------'));
if (activeNumbers.length > 0) {
const timeStr = new Date().toLocaleString('id-ID');
let content = `\n--- Report: ${timeStr} ---\n`;
content += activeNumbers.join('\n') + '\n';
fs.appendFileSync(reportTxt, content);
console.log(`\n${chalk.green('✓')} Saved ${chalk.bold(activeCount)} active numbers to ${chalk.cyan(reportTxt)}`);
} else {
console.log(`\n${chalk.yellow('!')} No active numbers found`);
}
console.log(chalk.green('✓ Checking completed.'));
await question(chalk.gray('\nPress Enter to return to menu...'));
this.showMenu();
} catch (error) {
console.error(`\n${chalk.bgRed.white.bold(' ✗ ERROR ')} Error while checking numbers:`, error.message);
await question(chalk.gray('\nPress Enter to return to menu...'));
this.showMenu();
}
}
async checkManual() {
this.clearScreen();
console.log(chalk.yellow.bold(' MANUAL NUMBER CHECK \n'));
const number = await question(
chalk.cyan(' ❯ Enter number ') + chalk.gray('(example: 0812... / 62812...) : ')
);
if (!number || number.trim() === '') {
console.log(chalk.red('\n✗ Number cannot be empty'));
await question(chalk.gray('\nPress Enter to return to menu...'));
return this.showMenu();
}
console.log(chalk.gray('\nChecking number...\n'));
try {
const formattedNumber = this.formatNumber(number);
const formattedTarget = formattedNumber + '@c.us';
const isRegistered = await this.client.isRegisteredUser(formattedTarget);
console.log(chalk.gray('-------------------------------------------------------'));
if (isRegistered) {
console.log(` ${chalk.white(number.padEnd(22))} | ${chalk.green.bold('ACTIVE')} `);
} else {
console.log(` ${chalk.gray(number.padEnd(22))} | ${chalk.bgRed.white.bold(' UNREGISTERED ')} `);
}
console.log(chalk.gray('-------------------------------------------------------'));
} catch (error) {
console.log(chalk.red(`\n✗ ERROR: ${error.message}`));
}
await question(chalk.gray('\nPress Enter to return to menu...'));
this.showMenu();
}
async logoutDevice() {
console.log('');
const confirm = await question(
chalk.red(' ❯ Are you sure you want to logout? You will need to scan the QR code again. (y/n): ')
);
if (confirm.toLowerCase() === 'y') {
try {
console.log(chalk.yellow('\n Logging out...'));
await this.client.logout();
console.log(chalk.green('\n✓ Logout successful. Please restart the application to log in again.\n'));
process.exit(0);
} catch (error) {
console.log(chalk.red(`\n✗ Error while logging out: ${error.message}`));
await question(chalk.gray('\nPress Enter to return to menu...'));
this.showMenu();
}
} else {
this.showMenu();
}
}
}
const whatsappApp = new App();
whatsappApp.start();