-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwhatsapp.js
55 lines (46 loc) · 1.54 KB
/
whatsapp.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
const puppeteer = require('puppeteer');
const app = async () => {
try {
const browser = await puppeteer.launch({
headless: false,
ignoreDefaultArgs: ['--enable-automation'],
});
const page = await browser.newPage();
await page._client.send('Emulation.clearDeviceMetricsOverride');
// setting user agent
await page.setUserAgent(
`Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36`
);
await page.goto(`https://web.whatsapp.com/`);
// SEARCHES USER BY TITLE
await page.waitForSelector(`span[title="Your_contact"]`); //Enter you contac name here
await delay(5000);
// selecting contact
const contactName = 'Contact_name'; //Contact Name
await page.click(`span[title='${contactName}']`);
// focus on msgbar
const editor = await page.$(`div[data-tab="6"]`);
await editor.focus();
// number of messages you want to send
const amountOfMsg = 100;
// automate msg
for (let i = 0; i < amountOfMsg; i++) {
await page.evaluate(() => {
// write msg you want to send
const message = `Sample text to your contact`; //Enter message
document.execCommand('InsertText', false, message);
});
await page.waitForSelector("span[data-testid='send']");
await page.click("span[data-testid='send']");
await delay(500);
}
} catch (error) {
console.log(error);
}
};
function delay(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
app();