-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
301 lines (271 loc) · 10.2 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
var express = require('express');
var app = express();
var info = require('./var.js');
var request = require('request');
var slack = require('express-slack');
const bodyParser = require('body-parser');
const axios = require("axios");
var air_channel = 'G6XGMATUP'; //#responding
var dispatch_channel = 'GAG3D0EBF'; //#dispatch
// var air_channel = 'C71B0PRDW'; //#development_scratch
// var rpialert_channel = 'C71B0PRDW'; //#development_scratch
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/slack', slack({
scope: info.scope,
token: info.token,
store: 'data.json',
client_id: info.client_id,
client_secret: info.client_secret
}));
function compareTime(hr, min, direction) {
var now = new Date();
nowhr = now.getHours();
nowmin = now.getMinutes();
if (direction == "lt") {
return (nowhr < hr) || ((nowhr == hr) && (nowmin < min));
} else if (direction == "gt") {
return (nowhr > hr) || ((nowhr == hr) && (nowmin > min));
}
}
function makeDate() {
var now = new Date();
return [
now.getFullYear(),
'-',
now.getMonth() + 1 < 10 ? "0" + (now.getMonth() + 1) : (now.getMonth() + 1),
'-',
now.getDate() < 10 ? "0" + (now.getDate()) : (now.getDate()),
' at ',
now.getHours() < 10 ? "0" + (now.getHours()) : (now.getHours()),
':',
now.getMinutes() < 10 ? "0" + (now.getMinutes()) : (now.getMinutes()),
':',
now.getSeconds() < 10 ? "0" + (now.getSeconds()) : (now.getSeconds())
].join('');
}
app.post('/tmd_slack_notification', function(req, res) {
if (req.body.verification == info.verification_email) {
const areOos = () => {
const { data: oos } = axios.get(`${info.oos_url}?token=${info.oos_token}`);
return oos == 1;
};
var air_message = "";
var dispatch_message = "";
if ((compareTime(05, 55, "gt") && compareTime(18, 05, "lt")) || areOos()) {
air_message = {
unfurl_links: true,
channel: air_channel,
token: info.token,
"attachments": [
{
"text": "RPI Ambulance dispatched on " + makeDate(),
"fallback": req.body.dispatch,
"callback_id": "responding",
"color": "#F35A00",
"attachment_type": "default",
"fields": [
{
"title": req.body.dispatch,
"value": "Are you responding?",
"short": true
}
],
"actions": [
{
"name": "status",
"text": "Yes",
"style": "danger",
"type": "button",
"value": "yes"
},
{
"name": "status",
"text": "No",
"type": "button",
"value": "no"
}
]
}
]
};
dispatch_message = {
unfurl_links: false,
channel: dispatch_channel,
token: info.token,
"text": "RPI Ambulance dispatched on " + makeDate(),
"fallback": req.body.dispatch,
"attachments": [
{
"title": req.body.dispatch,
"short": true
}
]
};
} else {
air_message = {
unfurl_links: true,
channel: air_channel,
token: info.token,
"attachments": [
{
"text": "RPI Ambulance dispatched on " + makeDate(),
"fallback": req.body.dispatch,
"callback_id": "responding",
"color": "#F35A00",
"attachment_type": "default",
"fields": [
{
"title": req.body.dispatch,
"value": "Night crew call. No response is needed.",
"short": true
}
]
}
]
};
dispatch_message = {
unfurl_links: false,
channel: dispatch_channel,
token: info.token,
"text": "RPI Ambulance dispatched on " + makeDate(),
"fallback": req.body.dispatch,
"attachments": [
{
"text": "RPI Ambulance dispatched on " + makeDate(),
"fallback": req.body.dispatch,
"callback_id": "responding",
"color": "#F35A00",
"attachment_type": "default",
"fields": [
{
"title": req.body.dispatch,
"value": "Night crew call. No response is needed.",
"short": true
}
]
}
]
};
}
slack.send('chat.postMessage', air_message);
slack.send('chat.postMessage', dispatch_message);
res.status(200).send(req.body.dispatch);
} else {
res.status(401).send();
}
});
app.post('/tmd_slack_notification_long', function(req, res) {
if (req.body.verification == info.verification_email) {
var air_message = {
unfurl_links: true,
channel: air_channel,
token: info.token,
"attachments": [
{
"text": "Rensslaer County longtone on " + makeDate(),
"fallback": req.body.dispatch,
"callback_id": "responding",
"color": "#F35A00",
"attachment_type": "default",
"fields": [
{
"title": req.body.dispatch,
"value": "",
"short": true
}
]
}
]
};
var dispatch_message = {
unfurl_links: true,
channel: dispatch_channel,
token: info.token,
"attachments": [
{
"text": "Rensslaer County longtone on " + makeDate(),
"fallback": req.body.dispatch,
"callback_id": "responding",
"color": "#F35A00",
"attachment_type": "default",
"fields": [
{
"title": req.body.dispatch,
"value": "",
"short": true
}
]
}
]
};
slack.send('chat.postMessage', air_message);
slack.send('chat.postMessage', dispatch_message);
res.status(200).send(req.body.dispatch);
} else {
res.status(401).send();
}
});
app.post("/slack_response", function(req, res) {
var strReq = req.body.payload.toString();
var strReq = JSON.parse(strReq);
var maxElapsedTime = 12; //minutes to allow responses
maxElapsedTime *= 60 * 1000;
var messageTime = new Date(strReq.message_ts * 1000);
var actionTime = new Date(strReq.action_ts * 1000);
userID = strReq.user.id;
if ((actionTime - messageTime) < maxElapsedTime) {
request.post({url:'https://slack.com/api/users.info', form: {token:info.token,user:userID}}, function(error, response, body){
var userinfo = response.body.toString();
var userinfo = JSON.parse(userinfo);
abbrname = userinfo.user.profile.first_name.charAt(0).toUpperCase() + ". " + userinfo.user.profile.last_name;
var response_message = "";
if (strReq.actions[0].value == "yes") {
console.log(abbrname + " replied yes");
response_message = {
unfurl_links: true,
channel: air_channel,
token: info.token,
"mrkdwn": true,
"attachments": [
{
"fallback": abbrname + " is RESPONDING",
"text": "*" + abbrname + "*" + " is *RESPONDING*",
"color": "#7CD197",
"mrkdwn_in": ["text"]
}
]
};
} else {
console.log(abbrname + " replied no");
response_message = {
unfurl_links: true,
channel: air_channel,
token: info.token,
"mrkdwn": true,
"attachments": [
{
"fallback": abbrname + " is not responding",
"text": abbrname + " is NOT RESPONDING"
}
]
};
}
res.status(200).send();
slack.send('chat.postMessage', response_message);
});
} else {
var response_message = {
channel: air_channel,
token: info.token,
user: userID,
as_user: true,
text: "Sorry, your response was logged too long after the dispatch went out."
};
res.status(200).send();
slack.send('chat.postEphemeral', response_message);
}
});
app.listen(5939, function () {
console.log('Server up');
});