-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_broadcast.js
470 lines (433 loc) · 19.9 KB
/
index_broadcast.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Import lib
const { Client, LocalAuth, MessageMedia } = require('whatsapp-web.js'); //main wa api //Buttons, List
const express = require('express');
const socketIO = require('socket.io'); //koneksi
//const qrcode = require('qrcode-terminal'); //qrcode in terminal
const qrcode = require('qrcode'); //qrcode in html base64
const http = require('http');
const path = require('path');
const fs = require('fs'); //fs session? buat csv
const csv = require('csv-parser'); //csv
//const https = require('https');
const fetch = require('node-fetch');
const { count, time, info } = require('console');
// Init
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
const htmlPath = path.join(__dirname, 'files');
const SESSION_FILE_PATH = './wa-session.json';
// Session
//let sessionCfg;
//if (fs.existsSync(SESSION_FILE_PATH)){
// sessionCfg = require(SESSION_FILE_PATH);
//}
// Send response ke web? --------------
app.use(express.static(htmlPath));
app.get('/', (req,res) => {
res.set('Content-Type', 'text/csv');
//res.sendFile('index.php', { root: __dirname });
//res.status(200).json({
// status: true,
// message: 'Hello world!'
//});
});
//--------------------
// Start
const client = new Client({
webVersionCache: {
remotePath: 'https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.3000.1014192040-alpha.html',
type: 'remote'
//type: 'none'
} ,
puppeteer: {
args: [
'--no-sandbox', //important
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--no-zygote',
'--single-process',
'--disable-gpu',
],
headless: true //false
},
//session:sessionCfg
authStrategy: new LocalAuth()
})
// FUNGSI
const sleep = ms => new Promise(r => setTimeout(r, ms));
//setTimeout(() => { itung=0; kembali=0; obrolc=0; }, 5000);
// Start client WA
//client.initialize();
//CHANGE THESSSSSSS
var noadmin = '628xxx'
var nowabot = '628xxx'
// Koneksi socket io
//socket sebagai koneksi, jdi setiap client yang connect ke socket akan dikirim sebuah pesan misal terkoneksi
//var namafile = 'daftar_try.csv';
io.on('connection', function(socket){
//socket.emit('logmenu', 'Menggunakan file: ',namafile);
socket.emit('logmenu', 'Pilih data csv yang digunakan dulu!');
socket.emit('config', noadmin, nowabot);
// Fitur via message WA
client.on('message', async msg => {
// Cek active or not
if (msg.body == "!ping"){
await client.sendMessage(msg.from, "pong!!!");
}
// Cek registered on WA or not
if (msg.body.startsWith('!cek ')){
let number = msg.body.slice(5);
//number = '628xxx5';
number_details = await client.isRegisteredUser(number);
if (number_details){
//socket.emit('logmenu', 'Nomor '+number+' terdaftar');
await client.sendMessage(msg.from, 'Nomor '+number+' terdaftar');
} else {
//console.log(number_details,'gaal');
await client.sendMessage(msg.from, 'Nomor '+number+' TIDAK terdaftar');
}
await client.sendMessage(msg.from, 'File untuk broadcast yang digunakan: '+namafile);
}
// Cek group WA info
if (msg.body == "!cekgroupinfo"){
let chat = await msg.getChat();
//console.log(chat);
if (chat.isGroup){
//console.log('isgroup');
//await client.sendMessage(msg.from, 'getting info');
await client.sendMessage(msg.from, `
*Group Details*
ID: ${chat.id.user}
Name: ${chat.name}
Participant count: ${chat.participants.length}
`);
}
}
// Add members group WA
if (msg.body === '!addmembers') {
const group = await msg.getChat();
if (group.isGroup){
var datarow = [];
fs.createReadStream('./files/'+namafile)
.pipe(csv())
.on('data', (row) => {
//console.log(typeof row['Nama']);
datarow.push(row);
})
.on('end', async () => {
//console.log('CSV file successfully processed');
//console.log(datarow[0]['Nomor']);
socket.emit('logmenu',"Memasukkan "+datarow.length+" orang ke grup '"+group.name+"' dalam hitungan ke-10");
//sleep(5000);
setTimeout(async () => {
for (let i = 0; i < datarow.length; i++) {
//const element = datarow[i];
let number = datarow[i]['no'];
number = number.includes('@c.us') ? number : `${number}@c.us`;
const result = await group.addParticipants([number],
//{ comment: 'Welcome '+datarow[i]['nama']+'! bentar lagi anda akan di kick' },
{ autoSendInviteV4: true },
{ sleep: 200 });
//socket.emit('logmenu', i+1+'-'+datarow[i]['no']+' telah dikirimin');
socket.emit('logmenu', i+1+'-'+datarow[i]['no']+" | "+result[number].message);
}
//socket.emit('logmenu', "Blast Terkirim ke: "+ (datarow.length - gagalkirim) +" orang\nGagal: "+gagalkirim);
socket.emit('logmenu','Selesaiiiiiiiiiiiii');
}, 10000); //10 s timer delay
});
//end added--------------------------
}
}
// Cek peserta grup lom ada?
// Change file untuk broadcast
if (msg.body == "!changef"){
if (namafile === "daftar_try.csv"){
namafile = "daftar_final.csv"
} else if (namafile === "daftar_final.csv"){
namafile = "daftar_try.csv"
}
await client.sendMessage(msg.from, "Mengubah file untuk broadcast menjadi: ```"+namafile+"```");
socket.emit('logmenu', 'Menggunakan file: ',namafile);
}
// Rawblast
if (msg.body.startsWith('!rawblast ')){
msg.reply("Pesan diterima. Blast berdasarkan file: ```"+namafile+"```");
//const csv = require('csv-parser');
//const fs = require('fs');
var datarow = [];
fs.createReadStream('./files/'+namafile)
.pipe(csv())
.on('data', (row) => {
//console.log(typeof row['Nama']);
datarow.push(row);
})
.on('end', async () => {
//console.log('CSV file successfully processed');
//console.log(datarow[0]['Nomor']);
await client.sendMessage(msg.from, "Mengirim Blast ke "+datarow.length+" orang dalam hitungan ke-10");
//sleep(5000);
setTimeout(async () => {
await client.sendMessage(msg.from, "Started");
socket.emit('logmenu', 'Mulai broadcast (lagi) dengan ',namafile);
let gagalkirim = 0;
for (let i = 0; i < datarow.length; i++) {
// UBAH PESAN INIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
//let pesan = 'Selamat malam '+datarow[i]['nama']+', \nMohon maaf mengganggu';
let number = datarow[i]['no'];
number = number.includes('@c.us') ? number : `${number}@c.us`;
//let chat = await msg.getChat();
//chat.sendSeen();
try {
registered = await client.isRegisteredUser(number);
if (registered){
await client.sendMessage(number, pesan);
socket.emit('logmenu', i+1+'-'+datarow[i]['no']+' telah dikirimin');
//console.log(datarow[i]['no']+' telah dikirimin');
} else {
throw new Error("Mobile no is not registered on Whatsapp")
}
} catch (err) {
socket.emit('logmenu', i+1+'-'+datarow[i]['no']+' terjadi ',err);
//console.log(datarow[i]['no']+' terjadi '+err);
gagalkirim++;
}
}
await client.sendMessage(msg.from, "Blast Terkirim ke "+ (datarow.length - gagalkirim) +" orang\nGagal: "+gagalkirim);
}, 10000); //10 s timer delay
});
//end rawblast--------------------------
}
// Blast
if (msg.body.startsWith('!blast ')){
msg.reply("Pesan diterima. Blast berdasarkan file: ```"+namafile+"```");
//const csv = require('csv-parser');
//const fs = require('fs');
var datarow = [];
fs.createReadStream('./files/'+namafile)
.pipe(csv())
.on('data', (row) => {
//console.log(typeof row['Nama']);
datarow.push(row);
})
.on('end', async () => {
//console.log('CSV file successfully processed');
//console.log(datarow[0]['Nomor']);
await client.sendMessage(msg.from, "Mengirim Blast ke "+datarow.length+" orang dalam hitungan ke-10");
//sleep(5000);
setTimeout(async () => {
await client.sendMessage(msg.from, "Started");
socket.emit('logmenu', 'Mulai broadcast (lagi) dengan ',namafile);
let gagalkirim = 0;
for (let i = 0; i < datarow.length; i++) {
//const element = datarow[i];
let pesan = msg.body.slice(7);
//let pesan = 'Selamat malam '+datarow[i]['nama']+', \nMohon maaf mengganggu';
let number = datarow[i]['no'];
number = number.includes('@c.us') ? number : `${number}@c.us`;
//let chat = await msg.getChat();
//chat.sendSeen();
try {
registered = await client.isRegisteredUser(number);
if (registered){
await client.sendMessage(number, pesan);
socket.emit('logmenu', i+1+'-'+datarow[i]['no']+' telah dikirimin');
//console.log(datarow[i]['no']+' telah dikirimin');
} else {
throw new Error("Mobile no is not registered on Whatsapp")
}
} catch (err) {
socket.emit('logmenu', i+1+'-'+datarow[i]['no']+' terjadi ',err);
//console.log(datarow[i]['no']+' terjadi '+err);
gagalkirim++;
}
}
await client.sendMessage(msg.from, "Blast Terkirim ke "+ (datarow.length - gagalkirim) +" orang\nGagal: "+gagalkirim);
}, 10000); //10 s timer delay
});
//end blast--------------------------
}
}); // end client on message--------------------------
// FITURRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
// Fitur View data
socket.on('viewdata', function() {
var datarow = [];
var j = 0;
fs.createReadStream('./files/'+namafile)
.pipe(csv())
.on('headers', (headers) => {
socket.emit('viewdatar');
for (let i=0; i < headers.length; i++) {
//socket.emit('viewdatah', `| ${headers[i]}`);
j = j+1;
}
})
.on('data', (row) => {
//console.log(typeof row['Nama']);
datarow.push(row);
//socket.emit('viewdatad', 'Size data: '+);
socket.emit('viewdatad', datarow.length+" "+JSON.stringify(row, null, 4));
})
.on('end', async () => {
//console.log('CSV file successfully processed');
//console.log(datarow[0]['Nomor']);
//for (let i = 0; i < datarow.length; i++) {
// UBAH PESAN INIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
//let pesan = 'Selamat malam '+datarow[i]['nama']+', \nMohon maaf mengganggu';
//socket.emit('viewdatar');
//socket.emit('viewdatad', datarow[i]['nama']);
//}
});
});
// Fitur Ceknomor
socket.on('ceknomor', async function(number) {
number_details = await client.isRegisteredUser(number);
if (number_details){
socket.emit('logmenu', 'Nomor '+number+' terdaftar/punya WA');
//await client.sendMessage(msg.from, 'Nomor '+number+' terdaftar');
} else {
//console.log(number_details,'gaal');
socket.emit('logmenu', 'Nomor '+number+' TIDAK terdaftar di WA');
//await client.sendMessage(msg.from, 'Nomor '+number+' TIDAK terdaftar');
}
});
// Fitur Ping
socket.on('ping', async function(number) {
number = number.includes('@c.us') ? number : `${number}@c.us`;
await client.sendMessage(number, 'PING!!!');
socket.emit('logmenu', 'Ping terkirim ke '+number);
});
// Fitur Ganti File
socket.on('gantifile', function(namafileubah){
namafile = namafileubah;
//await client.sendMessage(msg.from, "Mengubah file untuk broadcast menjadi: ```"+namafile+"```");
socket.emit('logmenu', 'Menggunakan file: ',namafile);
})
// Fitur Blast / Rawblast
socket.on('blast', function(pesan, raw, imgpath=false){
var datarow = [];
fs.createReadStream('./files/'+namafile)
.pipe(csv())
.on('data', (row) => {
//console.log(typeof row['Nama']);
datarow.push(row);
})
.on('end', async () => {
//console.log('CSV file successfully processed');
//console.log(datarow[0]['Nomor']);
socket.emit('logmenu',"Mengirim Blast ke "+datarow.length+" orang dalam hitungan ke-10");
socket.emit('logmenu', 'Cek nomor awal: '+datarow[0]['no']);
//sleep(5000);
setTimeout(async () => {
socket.emit('logmenu', 'Mulai blast pesan dengan ',namafile);
let gagalkirim = 0;
for (let i = 0; i < datarow.length; i++) {
//const element = datarow[i];
let number = datarow[i]['no'];
number = number.includes('@c.us') ? number : `${number}@c.us`;
//let chat = await msg.getChat();
//chat.sendSeen();
try {
registered = await client.isRegisteredUser(number);
if (registered){
if (imgpath) {
var media = MessageMedia.fromFilePath('./files/' + imgpath);
await client.sendMessage(number, media);
}
if (raw){
await client.sendMessage(number, eval(pesan));
} else {
await client.sendMessage(number, pesan);
}
socket.emit('logmenu', i+1+'-'+datarow[i]['no']+' telah dikirimin');
//console.log(datarow[i]['no']+' telah dikirimin');
} else {
throw new Error("Mobile no is not registered on Whatsapp")
}
} catch (err) {
socket.emit('logmenu', i+1+'-'+datarow[i]['no']+' terjadi error: ',err.message);
//console.log(datarow[i]['no']+' terjadi '+err);
gagalkirim++;
}
}
socket.emit('logmenu', "Blast Terkirim ke: "+ (datarow.length - gagalkirim) +" orang\nGagal: "+gagalkirim);
}, 10000); //10 s timer delay
});
//end fitur blast--------------------------
})
// Start client WA
socket.emit('message', 'Connecting...');
socket.on('init', async function() {
client.initialize();
socket.emit('message', 'Initializing connection Wabot...'); //send message
});
socket.on('dc', async function() {
client.destroy();
socket.emit('message', 'Wabot disconnected...'); //send message
});
client.on('loading_screen', (percent, message) => {
//console.log('Loading Screen', percent, message);
socket.emit('message', 'Loading '+ percent+"%");
});
// QR
client.on('qr', qr => {
//console.log('QR Received:', qr);
//qrcode.generate(qr, {small: true}); //qrcode terminal
qrcode.toDataURL(qr, (err, url) => {
socket.emit('qr',url);
socket.emit('message','QR Code received, please scan...');
}) //ubah qrcode string ke img html
});
// Otentikasi (ntar kesimpan di session)
/*client.on('authenticated', (session) => {
console.log('AUTHENTICATED', session);
sessionCfg = session;
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function(err) {
if (err){
console.error(err);
}
})
})*/
client.on('authenticated', (session) => {
socket.emit('authenticated','Wabot is authenticated!');
socket.emit('message','Wabot is authenticated!');
//console.log('AUTHENTICATED', session);
})
client.on('auth_failure', msg => {
// Fired if session restore was unsuccessful
//console.error('AUTHENTICATION FAILURE', msg);
socket.emit('message','Error: Authentication failure!');
});
// Tersambung
client.on('ready', () => {
socket.emit('ready','Wabot is ready!');
socket.emit('message','Wabot ('+client.info.wid.user+' / '+client.info.pushname+') is ready!');
//console.log('Client is ready!');
//console.log(client.info.wid.user);
socket.emit('config', noadmin, client.info.wid.user);
//socket.emit('message', 'Nomor '+client.info.wid.user+' ('+client.info.pushname+') telah jadi WABot')
});
// Disconnected
client.on('disconnected', (reason) => {
socket.emit('message','Wabot is disconnected!' + reason);
//console.log('Client was logged out!', reason);
/*fs.unlinkSync(SESSION_FILE_PATH, function(err){
if(err) return console.log(err);
console.log('Session file deleted!');
});*/
client.destroy();
client.initialize();
});
// Error show
process.on('uncaughtException', function(err) {
//console.log('Caught exception: ' + err);
socket.emit('message', 'Caught exception: '+err);
});
});
// Localhost?
server.listen(8000, function(){
console.log('App running on *:' + 8000);
console.log('Please refresh');
});