-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
194 lines (170 loc) · 5.51 KB
/
index.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
const bot = require('venom-bot');
const prompt = require('prompt-sync')({sigint:true});
const process = require('process');
const fs = require('fs');
const readline = require('readline');
const http = require('http')
let data = {
contacts: [],
message: ''
};
function sendMessages(client) {
data.contacts.forEach(function(contact) {
client
.sendText('91' + contact + '@c.us', data.message)
.then((result) => {
console.log(contact + ' : ' + result.text);
})
.catch((err) => {
console.log(contact + ' : ' + err.text);
})
});
}
function createWaGroup(client) {
const gname = prompt('Enter group name:');
console.log(`Creating What's App group:[${gname}]...`);
client.createGroup(gname, [
'919766524512@c.us',
'918007211888@c.us',
'918806812722@c.us'
]).then((result) => {
console.log('WA GROUP SUCCESS: ' + result.text);
})
.catch((err) => {
console.log('WA GROUP CREATION FAILED : ' + err.text);
});
console.log(`$$$ What's App group:[${gname}] created successfully...`);
}
async function readContacts() {
return new Promise(function(resolve, reject) {
var rd = readline.createInterface({
input: fs.createReadStream('./contacts.csv'),
terminal: false
});
rd.on('line', function(line) {
data.contacts.push(line);
console.log('Got conatct : ' + line);
});
rd.on('close', function() {
resolve();
});
});
}
async function validateContacts(contacts) {
return new Promise(function(resolve, reject) {
//check if are 10 digits
contacts.forEach(contact => {
contact = contact.trim();
if (contact.match(/[0-9]{10}/g) == null) {
reject('Not a Valid Contact : ' + contact);
}
});
// remove empty contacts
data.contacts = data.contacts.filter(function(el) {
return el.length === 10
})
resolve();
});
}
async function readMessage() {
return new Promise(function(resolve, reject) {
try {
const message = fs.readFileSync('./message.txt');
data.message = message.toString();
console.log('GOT below msg:\n' + data.message);
resolve();
} catch (err) {
reject(err);
}
});
}
async function initBot() {
return bot.create('n1');
}
async function main() {
try {
let handleRequest = (request, response) => {
response.writeHead(200, {
'Content-Type': 'text/html'
});
fs.readFile('./index.html', null, function (error, data) {
if (error) {
response.writeHead(404);
respone.write('file not found');
} else {
response.write(data);
}
response.end();
});
};
http.createServer(handleRequest).listen(9001);
/*const server = http.createServer((req, res) => {
res.writeHead(200, { 'content-type': 'text/html' })
fs.createReadStream('index.html').pipe(res)
})
server.listen(process.env.PORT || 3000)
console.log('### WELCOME TO OUR APP ###\n');
//const client = await initBot();
let client;
while (true)
{
const choice = prompt('************\n1-InitiateNewBot\n2-Send MSG\n3-Exit\n**********\n\nEnter your Choice:');
//console.log('3-Create Group\n4-Send Contact\n5-Exit\n**********\n\nEnter your Choice:');
console.log(`Your chice is ${choice}`);
ch = Number(choice);
switch(ch)
{
case 1:
console.log('**** Initiating request for new whatsapp number ****');
client = await initBot();
break;
case 2:
console.log('**** SENDING TEXT MESSAGE TO THE USER LIST *****');
console.log('READING CONTACTS from contacts.csv from location:\"' + process.cwd()+'\"');
await readContacts();
console.log('READ CONTACTS SUCCESS..');
console.log('Validating contact numbers');
await validateContacts(data.contacts);
console.log('Validation DONE..');
console.log('Reading Message to be sent from message.txt file from location: \"' + process.cwd()+'\"');
await readMessage();
console.log('Message Read Successfully');
console.log('SENDING MESSAGE TO ALL CONTACTS..');
await sendMessages(client);
console.log('MESSAGE SENT SUCCESSFULLY TO ALL CONTACTS..');
break;
/*case 3:
console.log('****** CREATING WA GROUP *********');
console.log(' ' + client);
await createWaGroup(client);
break;
case 4:
console.log('****** SENDING CONTACT ***********');
// Send contact
await client
.sendContactVcard('919766524512@c.us', '918806812722@c.us', 'Shakti Shah')
.then((result) => {
console.log('Result: ', result); //return object success
})
.catch((erro) => {
console.error('Error when sending: ', erro); //return object error
});
break;
case 3:
console.log('***** EXITING, THANK YOU FOR USING OUR APP! *************');
process.exit(0);
break;
default:
console.log(`Enter correct choice (1 OR 2).. you entered ${ch}`);
break;
}
}*/
} catch(err) {
console.log(err.toString());
console.log('Press any key to exit');
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));
}
}
main();