forked from crazy-max/rocketchat-uptimerobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uptimerobot-incoming.js
75 lines (67 loc) · 1.97 KB
/
uptimerobot-incoming.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
/**
* uptimerobot-incoming.js
* Add Uptime Robot notifications via a new WebHook in Rocket.Chat
* @license MIT
* @version 0.2
* @author CrazyMax, https://github.com/crazy-max
* @updated 2018-10-14
* @link https://github.com/crazy-max/rocketchat-uptimerobot
*/
/* globals console, _, s */
const USERNAME = 'Uptime Robot';
const AVATAR_URL = 'https://raw.githubusercontent.com/cyberhiker/rocketchat-uptimerobot/master/res/avatar.png';
const convertAlertDuration = (seconds) => {
let days = Math.floor(seconds / (3600 * 24));
seconds -= days * 3600 * 24;
let hours = Math.floor(seconds / 3600);
seconds -= hours * 3600;
let minutes = Math.floor(seconds / 60);
seconds -= minutes * 60;
let result = "";
if (days > 0) {
result += " " + days + " days";
}
if (hours > 0) {
result += " " + hours + " hours";
}
if (minutes > 0) {
result += " " + minutes + " minutes";
}
if (seconds > 0) {
result += " " + seconds + " seconds";
}
return result;
};
/* exported Script */
class Script {
/**
* @params {object} request
*/
process_incoming_request({ request }) {
let data = request.content;
let attachmentColor = `#A63636`;
let statusText = `DOWN`;
let isUp = data.alertType === `2`;
if (isUp) {
attachmentColor = `#36A64F`;
statusText = `UP`;
}
let attachmentText = `[${data.monitorFriendlyName}](https://uptimerobot.com/dashboard.php#${data.monitorID}) is ${statusText} (${data.monitorURL}).\n`;
if (isUp) {
attachmentText += `It was down for ${convertAlertDuration(data.alertDuration)}`;
} else {
attachmentText += `Reason: ${data.alertDetails}`;
}
return {
content:{
username: USERNAME,
icon_url: AVATAR_URL,
text: `Monitor [${data.monitorFriendlyName}](https://uptimerobot.com/dashboard.php#${data.monitorID}) status has changed.`,
attachments: [{
text: attachmentText,
color: attachmentColor
}]
}
};
}
}