-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstalk.js
69 lines (57 loc) · 1.8 KB
/
stalk.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
const puppeteer = require('puppeteer');
const path = require("path");
const { WSAEPROVIDERFAILEDINIT } = require('constants');
const MongoClient = require('mongodb').MongoClient;
const phone_ids = [
// Add phone number IDs with "@c.us" suffix, ex. "[email protected]"
];
const loadDB = async () => {
const client = await MongoClient.connect('mongodb://localhost:27017/stalkbot');
const db = client.db('stalkbot');
return db;
};
(async () => {
// Launch browser
const browser = await puppeteer.launch({
headless: false,
userDataDir: path.join(process.cwd(), "ChromeSession")
});
const page = await browser.newPage();
// Go to WhatsApp Web
await page.goto('https://web.whatsapp.com', {
waitUntil: 'networkidle0',
timeout: 0
});
// Wait until login using QR Code
await page.waitForSelector('*[data-icon=chat]',
{
polling: 1000,
timeout: 0
})
console.log("Logged in!")
// Inject API file
let filepath = path.join(__dirname, "WAPI.js");
await page.addScriptTag({ path: require.resolve(filepath) });
const db = await loadDB();
const activityCollection = db.collection("activity");
setInterval(async () => {
phone_ids.map(async (phoneId) => {
const isOnline = await page.evaluate(async (userID) => {
return await window.WAPI.isUserOnline(userID);
}, phoneId);
if (isOnline)
{
console.log(`${phoneId} is online!`);
}
activityCollection.insertOne(
{
id: phoneId,
isOnline: isOnline,
timestamp: new Date()
}
);
return isOnline;
})
},
20000);
})();