-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (55 loc) · 1.6 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
const { openBrowser, goto, $, deleteCookies } = require("taiko");
const axios = require('axios');
; (async () => {
try {
await openBrowser({
host: "localhost",
port: 9223,
headless: false,
});
await deleteCookies()
await goto(process.env.URL);
const textFound = (await $(process.env.CSS_SELECTOR).text()).trim()
console.log(`Text found:`, JSON.stringify(textFound))
if (textFound === process.env.UNWANTED_TEXT) {
console.log('Unwanted text found. Site did not change. Exiting...')
process.exit(0)
}
console.log(`notifying`)
await notify(textFound)
process.exit(0)
} catch (e) {
console.error(e)
process.exit(1)
}
})();
async function notify(textFound) {
try {
switch (process.env.NOTIFY_VIA) {
case 'opsgenie':
await notifyViaOpsGenie(textFound)
break;
case 'slack':
await notifyViaSlack(textFound)
break
default:
throw new Error('Missing NOTIFY_VIA, OPSGENIE_API_KEY or SLACK_WEBHOOK_URL. Exiting')
}
} catch (e) {
throw new Error('Error triggering alert: ' + JSON.stringify(e.response?.data ?? e.message))
}
}
async function notifyViaOpsGenie(textFound) {
await axios.post('https://api.eu.opsgenie.com/v2/alerts', {
message: `website changed! Text found was: ${textFound}`
}, {
headers: {
Authorization: `GenieKey ${process.env.OPSGENIE_API_KEY}`
}
})
}
async function notifyViaSlack(textFound) {
await axios.post(process.env.SLACK_WEBHOOK_URL, {
text: `Change detected on ${process.env.URL}! Text found was: ${textFound}`
})
}