This repository was archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (93 loc) · 2.9 KB
/
index.js
File metadata and controls
111 lines (93 loc) · 2.9 KB
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
require("dotenv").config();
const Request = require("request");
const SLACK_URL = process.env.SLACK_URL;
console.log(`[SESSION_BOT] SLACK_URL: ${SLACK_URL}`);
const headers = {
"Content-Type": "application/json"
};
const options = {
uri: SLACK_URL,
method: "POST",
headers: headers,
timeout: 5000
};
const postPayload = {
channel: "#session_updates",
username: "ThatBot",
icon_emoji: ":that:"
};
const buildPayload = slackMessage => {
const payload = Object.assign({}, slackMessage, postPayload);
const reqOpts = Object.assign({ body: JSON.stringify(payload) }, options);
console.log(
`[SESSION_BOT] Formatted Slack Message: \n ${JSON.stringify(payload)}`
);
return reqOpts;
};
exports.sessionUpdated = (req, res) => {
isPost(req, res);
const message = req.body;
const slackMessage = buildSessionUpdatedSlackMessage(message);
const reqOpts = buildPayload(slackMessage);
Request(reqOpts).pipe(res);
};
exports.sessionCancelled = (req, res) => {
isPost(req, res);
const message = req.body;
const slackMessage = buildSessionCancelledSlackMessage(message);
const reqOpts = buildPayload(slackMessage);
Request(reqOpts).pipe(res);
};
const isPost = (req, res) => {
if (req.method !== "POST") {
const error = new Error("Only POST requests are accepted");
console.log(`[SESSION_BOT] Non Post Method Recieved: ${req.method}`);
return res.status(405).send(error);
}
};
const buildSessionUpdatedSlackMessage = sessionUpdated => ({
text: `*${
sessionUpdated.Speaker
}'s* session was just updated. :bacon: :tada:`,
attachments: [
{
fallback: `${sessionUpdated.Speaker} session was just updated.`,
color: "#36a64f",
author_name: sessionUpdated.Speaker,
author_link: sessionUpdated.SpeakerUrl,
title: sessionUpdated.Title,
title_link: sessionUpdated.URL,
text: sessionUpdated.ShortDescription,
fields: [
{
title: sessionUpdated.Room.Changed ? "Room Changed" : "Room",
value: sessionUpdated.Room.Changed
? `${sessionUpdated.Room.Old} -> ${sessionUpdated.Room.Current}`
: sessionUpdated.Room.Current,
short: true
},
{
title: sessionUpdated.Time.Changed ? "Time Changed" : "Time",
value: sessionUpdated.Time.Changed
? `${sessionUpdated.Time.Old} -> ${sessionUpdated.Time.Current}`
: sessionUpdated.Time.Current,
short: true
}
]
}
]
});
const buildSessionCancelledSlackMessage = sessionUpdated => ({
text: `*${sessionUpdated.Title}* was just *cancelled*. :cry:`,
attachments: [
{
fallback: `${sessionUpdated.Title} was just cancelled.`,
color: "#36a64f",
author_name: sessionUpdated.Speaker,
author_link: sessionUpdated.SpeakerUrl,
title: sessionUpdated.Title,
title_link: sessionUpdated.URL,
text: sessionUpdated.ShortDescription
}
]
});