-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
73 lines (55 loc) · 2.12 KB
/
main.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
const { Client } = require('whatsapp-web.js')
const qrcode = require('qrcode-terminal')
const fs = require('fs')
const csv = require('csv-parser')
const { phoneNumberFormatter } = require('./formatnumber')
const client = new Client()
const csvPhoneNumbersPath = './data/target_batch.csv';
const targetPhoneNumbers = [];
const messageToSend = `
Halo, terimakasih sudah mendaftar acara Lotengdev Meetup #3,
berikut ini link Zoom untuk acara nanti pada *Sabtu, 27 Januari, Pukul 20.30 WITA*.
Join Zoom Meeting
https://telkomsel.zoom.us/j/96378472133?pwd=RCt5RzVWQVI2MnpOVlg1WWkvZzNUZz09
Meeting ID: 963 7847 2133
Passcode: lotengdev
Sampai jumpa di acara 👋.
*[Sent by Wah aBot]*
`;
// loop counter
var counter = 0;
client.on("qr", (qr) => {
console.log("Scan cepat QR Code ini buat login WA:")
qrcode.generate(qr, { small: true });
})
client.on("ready", () => {
console.log("👌 WA Client udah ready bos!")
// baca file CSV
fs.createReadStream(csvPhoneNumbersPath)
.pipe(csv())
.on('data', (data) => targetPhoneNumbers.push(phoneNumberFormatter(data['No-whatsapp'])))
.on('end', () => {
//console.log(targetPhoneNumbers);
console.log("Total imported phone numbers:", targetPhoneNumbers.length);
sendBatchMessage(targetPhoneNumbers, messageToSend);
});
})
function sendBatchMessage(phoneNumbers, message){
// jalankan fungsi dengan interval 5 detik,
// buat antisipasi biar tidak kena banned
setTimeout(() => {
if(counter < phoneNumbers.length){
console.log(`Mengirim pesan ke-${counter + 1}...`);
client.sendMessage(phoneNumbers[counter], message).then(response => {
console.log(`✅ Pesan ke-${counter} terkirim`)
})
// recursive call
sendBatchMessage(phoneNumbers, message);
counter++;
if(counter >= targetPhoneNumbers.length){
console.log("🎉 Semua pesan terkirim!")
}
}
}, 5000);
}
client.initialize()