-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (108 loc) · 3.34 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const https = require('https');
const { env } = require('process');
const DEBUG = env.DEBUG;
const SECONDS = env.SECONDS || 10
const CITIES = env.CITIES?.length ? env.CITIES.split(',') : null;
const DEMO = env.DEMO === "true" || false;
const log = (args) => {
if (DEBUG) console.log(args);
}
const slackNotificationOptions = {
hostname: 'hooks.slack.com',
path: '/services/' + env.TOKENS,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const sendSlackNotification = (text) => {
const postData = JSON.stringify({
text: text
});
const req = https.request(slackNotificationOptions, res => {
log(`Slack status code: ${res.statusCode}`);
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
log(data);
});
});
req.on('error', error => {
console.error(error);
});
log("Sending slack message");
req.write(postData);
req.end();
}
const orefOptions = {
hostname: 'www.oref.org.il',
path: '/WarningMessages/alert/alerts.json',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0',
'Referer': 'https://www.oref.org.il/'
}
};
const checkNotification = () => {
if (DEMO) {
const jsonData = {
"id": "133281935730000000",
"cat": "1",
"title": "בדיקה - ירי רקטות וטילים",
"data": [
"בת-ים",
"פלמחים",
"ראשון לציון - מערב",
"תירוש"
],
"desc": "זוהי אינה התראה אמיתית"
};
sendSlackNotification(`*${jsonData.title}*\n\n>${jsonData.data.join('\n>')}\n\n${jsonData.desc}`);
return;
}
const req = https.request(orefOptions, res => {
log(`Pikud ha oref status code: ${res.statusCode}`);
if (res.statusCode !== 200) {
return;
}
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
log("Data:");
log(data);
if (data && data?.trim().length) {
const jsonData = JSON.parse(data);
if (jsonData.title && jsonData.data) {
let sendAlert = false;
if (CITIES) {
CITIES.forEach(city => {
if (jsonData.data.includes(city)) {
sendAlert = true;
}
})
} else {
sendAlert = true;
}
if (sendAlert) {
sendSlackNotification(`*${jsonData.title}*\n\n${jsonData.data.join('\n')}\n\n\`${jsonData.desc}\``);
return;
}
}
} else {
log("No notifications")
}
});
});
req.on('error', error => {
error(error);
});
req.end();
};
console.log(`=== Start pulling Red Alert events every ${SECONDS} seconds ===`)
if(DEMO)console.log(`=!!! Demo mode is on !!!=`)
setInterval(checkNotification, SECONDS * 1000);