-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
256 lines (232 loc) · 7.7 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
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
const express = require("express");
const bodyParser = require('body-parser');
const database = require("./database.js");
const helpers = require("./helpers.js");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/', (req, res) => {
let query = req.body.text;
if(req.body.token != process.env.TOKEN && !req.body.team_domain){
res.end('3rd party api requests not allowed... creepy!');
return;
}
database.checkTeam(req.body.team_id, (err) => {
if(err == 404){
res.end('Your team is not authorised, do it <https://goo.gl/MidmzM|here>');
}
else if(err){
res.end('Service seems to be down');
}
else{
if(query.startsWith("set")){
let topic = query.substr(4);
if(topic.length > 28){
res.end("Topic is too long");
return;
}
if(topic.length === 0){
res.end("Topic is needed");
return;
}
if(req.body.channel_name != 'privategroup'){
res.end('The channel is public, already easy to join via channel list');
return;
}
// if channel exists in db, get token using getChannel, to prevent multiple user authentications for same channel.
database.getChannel(req.body.team_domain, req.body.channel_id, (err, channel) => {
if(err === 404){
// Channel doesnt exist in db. need to give link to add-to-slack
database.getToken(req.body.team_id, req.body.user_id, (err, AUTH_TOKEN) => {
// Hasn't authorised
if(err === 404){
res.json({
attachments: [
{
color: "#7353BA",
text:"You need to authorize us for seeing your private channels and inviting people\n"+
"Please do it <https://goo.gl/MidmzM|here>\n"
}
]
});
res.end();
return;
}
//Has authorised
helpers.addToDB(AUTH_TOKEN, topic, req, res);
});
return;
}
// Channel already exists, use the token
const AUTH_TOKEN = channel['auth-key'];
helpers.addToDB(AUTH_TOKEN, topic, req, res);
});
}
else if(query.startsWith("show-all")){
database.queryAll(req.body.team_domain, (groups)=>{
let channelList = '';
let channelTopics = '';
let fallback = `Channels\t-\tTopic`;
groups.forEach((group)=>{
channelList += `${group["channel-name"]}\n`;
channelTopics += `${group.topic}\n`;
fallback += `${group["channel-name"]}\t-\t${group.topic}`;
});
res.json({
attachments: [{
color: "#7353BA",
fallback: fallback,
text: "Showing all Study groups:",
fields: [
{
"title": "Channel",
"value": channelList,
"short": true
},
{
"title": "Topic",
"value": channelTopics,
"short": true
}
]
}]
});
res.end();
});
}
else if(query.startsWith("find")){
let topic = query.substr(5);
database.queryTopic(req.body.team_domain, topic, (groups) => {
if(groups.length == 0){
res.json({
text: "Topic named " + topic + " not found"
});
res.end();
return;
}
let channelList = '';
let channelTopics = '';
let fallback = `Channels\t-\tTopic`;
groups.forEach((group)=>{
channelList += `${group["channel-name"]}\n`;
channelTopics += `${group.topic}\n`;
fallback += `${group["channel-name"]}\t-\t${group.topic}`;
});
res.json({
attachments: [{
color: "#7353BA",
fallback: fallback,
text: "Showing all Study groups:",
fields: [
{
"title": "Channel",
"value": channelList,
"short": true
},
{
"title": "Topic",
"value": channelTopics,
"short": true
}
]
}]
});
res.end();
});
}
else if(query.startsWith("add-to")){
let channelname = query.substr(7);
//get channel_id and auth-key from db using getChannelInfo.
database.getChannelInfo(req.body.team_domain, channelname, (err, group) => {
//add user to group, if group exists in the database
if(group){
helpers.slack('groups.invite',
{
token: group['auth-key'],
channel: group["channel-id"],
user: req.body.user_id
}).then((group) => {
group = JSON.parse(group);
if(group["already_in_group"]){
res.end("You are already in the group");
}
else if (group.ok){
res.end("Successfully added you to the group :) ");
}
else {
if (req.body.channel_name !== "privategroup"){
res.json({
response_type: 'in_channel',
attachments: [
{
colour: "#7353BA",
text: "<@"+req.body.user_id+"> wants to join `#"+channelname+"` but authorisation for team has expired\n"+
"Tell the members of the channel to use `/sns set` to reauthorise",
mrkdwn_in: ["text"]
}
]
});
res.end();
}
else{
res.end("Could not add you to the group :( Contact group members. ")
}
}
});
}
else{
res.json({text: "Group not found in database."});
res.end();
}
});
}
else{
res.json({
"response_type": "ephemeral",
"text": "How to use /sns",
"attachments":[
{
color: "#7353BA",
"text": "To set a topic, use `/sns set [topicname]`"+
"\nTo get a list of all private channels, use `/sns show-all`"+
"\nTo find a private channel belonging to a particular topic, use `/sns find [topicname]`"+
"\nTo add studybot to a private channel, use `/sns add-to [channelname]`",
mrkdwn_in: ["text"]
}
]});
res.end();
}
}
})
});
app.get('/', (req, res) => {
// Show a cute slack button
res.end(`<a href="https://goo.gl/MidmzM">
<img alt="Add to Slack" height="40" width="139"
src="https://platform.slack-edge.com/img/add_to_slack.png"
srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x,
https://platform.slack-edge.com/img/[email protected] 2x" />
</a>`)
});
app.get('/auth', (req, res) => {
// Prepare Data for Slack Auth
let data = {
client_id: process.env.SLACK_CLIENT_ID,
client_secret: process.env.SLACK_CLIENT_SECRET,
code: req.query.code
};
// POST the data to slack access endpoint
helpers.slack('oauth.access', data)
.then((body) => {
// Slack User Token
let pBody = JSON.parse(body)
let token = pBody.access_token;
let user = pBody.user_id;
let team_id = pBody.team_id;
let team_name = pBody.team_name;
// Meanwhile store the {team -> token}
database.storeToken(team_name, team_id, user, token);
res.redirect(`https://pankaja-shree.github.io/sns-splash/redirect.html`);
}).catch(res.end);
})
app.listen(process.env.PORT||"8080");