-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (56 loc) · 2.18 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
import puppeteer from 'puppeteer';
import fetch from 'node-fetch';
import dotenv from 'dotenv';
dotenv.config();
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
(async () => {
const browser = await puppeteer.launch({headless: true, args: ['--no-sandbox'] });
const page = await browser.newPage();
await page.goto(process.env.SITE_GOTO,{ timeout: 60000 });
await delay(1000);
await page.waitForSelector('.cookie-consent__buttons-button.cookie-consent__buttons__close', { timeout: 60000 });
await delay(1000);
await page.click('.cookie-consent__buttons-button.cookie-consent__buttons__close');
await delay(1000);
await page.type('#login', process.env.SITE_USERNAME);
await delay(1000);
await page.type('#senha', process.env.SITE_PASSWORD);
await delay(1000);
await page.click('#btn-entrar-sistema');
await page.waitForNavigation();
await delay(1000);
const links = await page.evaluate(() => {
const elements = Array.from(document.querySelectorAll('a.btn.btn-primary.btn-block'));
return elements
.filter(element => element.textContent.trim() === 'Acompanhar no mapa')
.map(element => element.href);
});
console.log('Links encontrados:');
if (links.length === 0) {
console.log('Nenhum link encontrado.');
await browser.close();
return;
} else {
links.forEach((link, index) => {
console.log(`${index + 1}: ${link}`);
});
}
const messageBody = `Prezados, bom dia!\n Gostaria de compartilhar os links das rotas dos motoboys:\n${links.join('\n')}`;
const telegramUrl = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`;
const params = {
chat_id: process.env.TELEGRAM_CHAT_ID,
text: messageBody
};
const response = await fetch(telegramUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(params),
});
const data = await response.json();
if (data.ok) {
console.log('Mensagem enviada');
} else {
console.log('ERROR', data.description);
}
await browser.close();
})();