-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (143 loc) · 4 KB
/
index.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
const express = require("express");
const request = require("request");
const bodyParser = require("body-parser");
const loadMashup = require("./mashup");
const loadTumblr = require("./tumblr");
const clientId = process.env.SLACK_CLIENT_ID;
const clientSecret = process.env.SLACK_CLIENT_SECRET;
const OH_NO_COLOR = "#fe7db5";
const GIT_URL = "https://github.com/bhishp/oh-no-slack";
const app = express();
app.use(express.static("public"));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
const PORT = parseInt(process.env.PORT) || 4390;
app.listen(PORT, () => {
console.log("oh no... we're on port " + PORT);
});
app.get("/", (req, res) => res.redirect(GIT_URL));
// Handling Slack oAuth process
app.get("/oauth", (req, res) => {
if (!req.query.code) {
res.status(500);
res.send({ "Error": "oh no... we need a code to authorize you" });
console.error("oh no... we need a code to authorize you");
return;
}
// call slack's oauth.access
request({
url: "https://slack.com/api/oauth.access",
qs: {
code: req.query.code,
client_id: clientId,
client_secret: clientSecret
},
method: "GET",
}, (accessErr, accessRes, accessBody) => {
if (accessErr) {
res.status(500);
res.send({ "Error": "oh no... there was an error authorizing you" });
console.error("oh no... there was an error calling slack's oauth.access");
console.error(accessErr);
return;
}
const { ok, error } = JSON.parse(accessBody);
if (!ok) {
res.status(500);
res.send({ "Error": "oh no... there was an error authorizing you" });
console.error("oh no... there was an error calling slack's oauth.access");
console.error(error);
return;
}
console.log("oauth.access success");
res.redirect(GIT_URL);
});
});
app.post("/ohno", (req, res) => {
const { text, response_url } = req.body;
res.set("Content-Type", "application/json");
// retrieve a mashup comic from the Glitch app
if (text === "mashup") {
// send initial response
res.send();
loadMashup((err, mashupData) => {
if (err) {
console.error("oh no... an error happened when loading the mashup");
return;
}
console.log("loaded mashup data");
const slackRes = {
response_type: "in_channel",
attachments: mashupData.imageURLs.map((image_url, i) => (
i === 0 ?
{
title: "Keep this comic",
title_link: mashupData.savedURL,
fallback: "oh no... missing title",
image_url,
} :
{
fallback: "oh no... missing panel",
image_url,
}
))
};
request({
url: response_url,
method: "POST",
headers: {
"Content-Type": "application/json",
},
json: slackRes
});
});
return;
}
// retrieve a random comic from the tumblr blog
if (text === "comic") {
// send initial response
res.send();
loadTumblr((err, post) => {
if (err || !post) {
console.error("oh no... an error happened when loading the tumblr comic");
return;
}
console.log("loaded tumblr data");
const {
postUrl,
comicUrl: image_url
} = post;
const slackRes = {
response_type: "in_channel",
attachments: [{
title: postUrl,
title_link: postUrl,
fallback: "oh no... could not load the comic",
image_url,
}]
};
request({
url: response_url,
method: "POST",
headers: {
"Content-Type": "application/json",
},
json: slackRes
});
});
return;
}
// oh no...
res.send(
{
response_type: "in_channel",
attachments: [
{
color: OH_NO_COLOR,
fallback: "oh no...",
image_url: "https://api.tumblr.com/v2/blog/webcomicname/avatar/512"
}
]
}
);
});