-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (64 loc) · 2.83 KB
/
script.js
File metadata and controls
80 lines (64 loc) · 2.83 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
const puppeteer = require('puppeteer-core');
const readline = require('readline');
const inputFieldPath = "[aria-label='Type a message']";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function askQuestion(query) {
return new Promise((resolve) => rl.question(query, resolve));
}
async function start() {
let message = await askQuestion('What message do you want to send on WhatsApp? ');
let times = await askQuestion('How many times do you want to send the message? ');
let gap = await askQuestion('What should be the gap between messages (in seconds)? ');
let notify = await askQuestion('Notfiy the user After sending the messages? [Y/N] ');
main(message, times, gap, notify);
}
async function main(message, times, gap, notify) {
const options = {
headless: false,
executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
};
try {
const browser = await puppeteer.launch(options);
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800 });
console.log("Browser launched & created a new page. \nGoing to [whats app]]!");
await page.goto('http://web.whatsapp.com');
// Wait for the user to press Enter
const ans= await askQuestion("Select the User then Press Enter to continue...");
if(ans !== ''){
console.log(`Key Pressed : ${ans} \nBrowser is going to close!`);
await browser.close();
return;
}
console.log("Message is going to start in ")
for (let i = 5; i >= 0; i--) {
console.log(`${i} seconds`)
await new Promise(resolve => setTimeout(resolve, 1000));
}
console.log(`Run a loop to send message ${times} times: ${message}`);
for (let i = 1; i <= times; i++) {
console.log("Typing our text in the input field");
await page.type(inputFieldPath, message);
await page.keyboard.press('Enter');
console.log(`Message sent: ${i} times.`);
await new Promise(resolve => setTimeout(resolve, gap * 1000));
}
if (notify.toLowerCase() === 'y') {
console.log("Send one last message to notify it's just a funny project!");
await page.type(inputFieldPath, `Dear, Don't be angry please. It's a funny project & I chose you as one of my experimental user.`, { delay: 100 });
await page.keyboard.press('Enter');
await new Promise(resolve => setTimeout(resolve, 2000));
}
console.log("Message Send Completed Successfully! Closing browser");
await browser.close();
} catch (error) {
console.error("Oops! Something is going wrong [Error Occurred]");
console.error(error);
} finally {
rl.close();
}
}
start();