-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
122 lines (109 loc) · 2.61 KB
/
server.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
//node packages
const axios = require("axios");
const crypto = require("crypto");
require("dotenv").config();
//local packages
const {
app: {
client: {
conversations: { history },
chat: { postMessage }
}
}
} = require("./utilities/bolt.js");
//globals
const RPIALERT_URL =
process.env.RPIALERT_URL || "https://alert.rpi.edu/alerts.js";
const ALERTS_CHANNEL = process.env.ALERTS_CHANNEL;
const USER_TOKEN = process.env.SLACK_USER_TOKEN;
//package config
let oldHash;
const parseAlertData = body => {
return body
.substring(
body.indexOf("alert_content = ") + 17,
body.indexOf("alert_default = ") - 3
)
.trim();
};
const rpialert = async () => {
const { data } = await axios.get(RPIALERT_URL);
let text = parseAlertData(data);
if (text === "") {
return;
}
let hash = crypto
.createHash("md5")
.update(text)
.digest("hex");
if (hash !== oldHash) {
postMessage({
token: USER_TOKEN,
channel: ALERTS_CHANNEL,
unfurl_links: false,
text: "RPI ALERT - <!channel>",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `<!channel>\nAn RPIAlert has been posted:\n>>>${text}`
}
},
{
type: "context",
elements: [
{
type: "image",
image_url:
"https://emoji.slack-edge.com/T351C3UGL/rpi/a494ab4a33755c38.png",
alt_text: "rpi_logo"
},
{
type: "mrkdwn",
text: " More info: https://alert.rpi.edu"
},
{
type: "plain_text",
text: hash
}
]
}
]
});
}
oldHash = hash;
};
const getOldHash = async () => {
let latest = Date.now();
for (;;) {
let results = await history({
token: USER_TOKEN,
channel: ALERTS_CHANNEL,
latest: latest
});
let messages = results.messages.filter(
x => x.subtype === "bot_message" && x.text === "RPI ALERT - <!channel>"
);
if (messages.length > 0) {
// Old style message, does not have hash, allow repost to have it add the hash
if (!messages[0].blocks[1].elements[2]) {
break;
}
oldHash = messages[0].blocks[1].elements[2].text;
break;
} else {
if (results.has_more) {
latest = results.messages[results.messages.length - 1].ts;
} else {
// Could not find a previous bot message
break;
}
}
}
};
getOldHash().then(() => {
setInterval(function() {
rpialert();
}, 10000);
});