-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
140 lines (114 loc) · 5.94 KB
/
server.ts
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
import "https://deno.land/x/dotenv@v2.0.0/load.ts";
import { Application, Router, Status} from "https://deno.land/x/oak/mod.ts";
import { App as SlackApp } from "https://deno.land/x/slack_bolt@1.0.0/mod.ts";
import * as util from "./utilities.ts";
import * as messageFactory from "./message.ts";
const SLACK_TOKEN = Deno.env.get("SLACK_BOT_TOKEN");
const DISPATCH_CHANNEL = Deno.env.get("DISPATCH_CHANNEL");
const RESPONDING_CHANNEL = Deno.env.get("RESPONDING_CHANNEL");
const AUTH_TOKEN = Deno.env.get("AUTH_TOKEN");
const slackApp = new SlackApp({
signingSecret: Deno.env.get("SLACK_SIGNING_SECRET"),
token: SLACK_TOKEN,
ignoreSelf:true,
});
function authorize_request(request, ctx): boolean {
if (request == null){
ctx.response.body = "No authentication token provided.\n";
console.log("No authentication token provided.");
ctx.response.type = "text/plain";
ctx.response.status = Status.BadRequest;
return false;
}
else if (request.token != AUTH_TOKEN){
ctx.response.body = "Invalid authentication token provided.\n";
console.log("Invalid authentication token provided.");
ctx.response.type = "text/plain";
ctx.response.status = Status.Forbidden;
return false;
}
else
return true;
}
const app = new Application();
const router = new Router();
router.post("/dispatch", async (ctx) => {
const req = await ctx.request.body().value;
if (!authorize_request(req, ctx)) return;
let dateTime = util.makeDate();
let dispatchBlock = messageFactory.generateDispatchMessage(dateTime);
let slackMessage = {token: SLACK_TOKEN, blocks: dispatchBlock, text: "RPI Ambulance Dispatched on " + util.makeDate()};
let respondingAttachment;
let dispatchAttachment;
let crewNeeded = await util.crewNeeded();
if(typeof crewNeeded == "boolean" && !crewNeeded)
respondingAttachment = dispatchAttachment = messageFactory.dutyCrewAttachment;
else if(typeof crewNeeded == "boolean" && crewNeeded) {
let toneTest = util.toneTest();
let text = toneTest ? "Likely to be the weekly pager test." : "A crew is needed";
respondingAttachment = messageFactory.generateRespondingAttachment(text, toneTest ? messageFactory.Color.warning : messageFactory.Color.danger, toneTest, true);
dispatchAttachment = toneTest ? messageFactory.generateRespondingAttachment(text, toneTest ? messageFactory.Color.warning : messageFactory.Color.danger, toneTest, false) : dispatchAttachment;
}
else
respondingAttachment = messageFactory.generateRespondingAttachment(crewNeeded, messageFactory.Color.danger, false, true);
let dispatchResult = await slackApp.client.chat.postMessage({...slackMessage,
channel: DISPATCH_CHANNEL,
attachments: dispatchAttachment});
let respondingResult = await slackApp.client.chat.postMessage({...slackMessage,
channel: RESPONDING_CHANNEL,
attachments: respondingAttachment,});
ctx.response.body = "Tones Recieved\n";
console.log("Tones Recieved");
});
router.post("/long-tone", async (ctx) => {
const req = await ctx.request.body().value;
if (!authorize_request(req, ctx)) return;
let dateTime = util.makeDate();
let longtoneMessage = messageFactory.generateLongtoneMessage(dateTime);
let attachment = util.longtoneTest() ? messageFactory.generateLongtoneTestAttachment("Likely to be the weekly all-call test.") : "";
let slackMessage = {token: SLACK_TOKEN,
blocks: longtoneMessage,
attachments: attachment,
text: "Rensslaer County longtone on " + util.makeDate()};
let dispatchResult = await slackApp.client.chat.postMessage({...slackMessage, channel: DISPATCH_CHANNEL});
let respondingResult = await slackApp.client.chat.postMessage({...slackMessage, channel: RESPONDING_CHANNEL});
ctx.response.body = "Long Tone Recieved\n";
console.log("Long Tone Recieved");
});
router.post("/slack-response", async (ctx) => {
const reqBody = await ctx.request.body().value;
const payload = JSON.parse(reqBody.get("payload"));
let userID = payload.user.id;
let userInfo = await slackApp.client.users.info({token: SLACK_TOKEN, user:userID});
let firstName = userInfo.user.profile.first_name;
let lastName = userInfo.user.profile.last_name;
const maxResponseTime = Deno.env.get("RESPONSE_MINUTES") * 60 * 1000;
const dispatchTime = new Date(payload.message.ts * 1000);
const responseTime = new Date(payload.actions[0].action_ts * 1000);
let statusMessage = {token: SLACK_TOKEN,
channel: RESPONDING_CHANNEL};
let actionID = payload.actions[0].action_id;
if(responseTime - dispatchTime > maxResponseTime){
statusMessage.user = userID;
statusMessage.text = "Your response was logged too long after the dispatch was recieved.";
}
else if(actionID == "responding"){
statusMessage.attachments = messageFactory.generateResponderStatus(firstName, lastName, true);
}
else if(actionID == "not_responding"){
statusMessage.attachments = messageFactory.generateResponderStatus(firstName, lastName, false);
}
let statusResult;
if(responseTime - dispatchTime > maxResponseTime)
statusResult = await slackApp.client.chat.Ephemeral(statusMessage);
else{
statusResult = await slackApp.client.chat.postMessage(statusMessage);
console.log(firstName.charAt(0) + ". " + lastName + " is " + actionID);
}
});
app.use(router.routes());
app.use(router.allowedMethods());
app.addEventListener("listen", () => {
console.log(`AmIResponding is listening on port ${Deno.env.get("PORT")}`);
});
await app.listen({ port: Deno.env.get("PORT")});