This repository has been archived by the owner on Dec 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
287 lines (249 loc) · 11.8 KB
/
commands.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
// TODO inherit config from app.js
var config = process.env.BOT_CONFIG ? JSON.parse(process.env.BOT_CONFIG) : require('./config.js');
var blockspring = require('blockspring');
var url = require("url"),
http = require("http"),
https = require("https"),
querystring = require("querystring"),
Discourse = require("discourse-api"),
discourse = new Discourse(config.DISCOURSE_URL, config.DISCOURSE_API_KEY, "system"),
og = require("open-graph");
var Cleverbot = require("./cleverbot"),
cleverbot = new Cleverbot();
var Clear = require('codeday-clear'),
S5 = require('s5'),
clear = new Clear(config.CLEAR_API_TOKEN, config.CLEAR_API_SECRET),
s5 = new S5(config.S5_API_TOKEN, config.S5_API_SECRET);
var countdown = {
channel: "C03QGP7PW",
message: null,
interval: null,
js: require("./lib/countdown.js")
};
var every = require('every-moment');
function getEvangelist(msg, args, channel, username, bot){
clear.getRegionByWebName(args.join("").toLowerCase(), function(region){
if(!region){bot.sendMessage("That event doesn't exist!", channel);return}
clear.getEventById(region.current_event.id, function(event){
if(!event.evangelist){bot.sendMessage("There isn't an evangelist for this event!", channel);return}
var message = "Here's the Evangelist for CodeDay " + event.region_name + ":";
message += "\ns5 username: " + event.evangelist.username;
message += "\nFirst name: " + event.evangelist.first_name;
message += "\nLast name: " + event.evangelist.last_name;
message += "\nEmail: " + event.evangelist.email;
message += "\nPhone: " + event.evangelist.phone;
bot.sendMessage(message, channel);
});
});
}
function getRegionalManager(msg, args, channel, username, bot){
clear.getRegionByWebName(args.join("").toLowerCase(), function(region){
if(!region){bot.sendMessage("That event doesn't exist!", channel);return}
clear.getEventById(region.current_event.id, function(event){
if(!event.evangelist){bot.sendMessage("There isn't an RM for this event!", channel);return}
var message = "Here's the Regional Manager for CodeDay " + event.region_name + ":";
message += "\ns5 username: " + event.manager.username;
message += "\nFirst name: " + event.manager.first_name;
message += "\nLast name: " + event.manager.last_name;
message += "\nEmail: " + event.manager.email;
message += "\nPhone: " + event.manager.phone;
bot.sendMessage(message, channel);
});
});
}
module.exports = function(bot, slack){
// function updateCodeDay(){
// var codeDay = new Date();
//
// codeDay.setTime(1432407600*1000);
//
// slack._apiCall("channels.setTopic", {topic: countdown.js(codeDay, null, countdown.js.DAYS).toString() + " until CodeDay!", channel: "C024H3105"});
// }
// setTimeout(updateCodeDay, 5000);
// every(1, 'day', updateCodeDay);
bot.addCommand("s4 countdown", "Show a countdown to CodeDay!", function(msg, args, channel, username){
var cd = slack.getChannelGroupOrDMByID(countdown.channel);
if(args[0] === "stop"){
cd.send("Stopping countdown...");
clearInterval(countdown.interval);
slack._apiCall("chat.delete", {ts: countdown.message, channel: countdown.channel});
}else{
bot.sendMessage("Countdown started in <#codedaycountdown>.", channel);
var codeDay = new Date();
codeDay.setTime(1432407600*1000);
var codeDayEast = new Date();
codeDayEast.setTime((1432407600*1000)+(3600000*3));
var codeDayCentral = new Date();
codeDayCentral.setTime((1432407600*1000)+(3600000*2));
var codeDayMountain = new Date();
codeDayMountain.setTime((1432407600*1000)+(3600000*1));
cd.send("[countdown_start]");
countdown.interval = setInterval(function(){
if(countdown.message){
// console.log("Tick " + countdown.message);
var text = "East: " + countdown.js(codeDayEast).toString() + "\n" +
"West: " + countdown.js(codeDay).toString() + "\n" +
"Central: " + countdown.js(codeDayCentral).toString() + "\n" +
"Mountain: " + countdown.js(codeDayMountain).toString();
slack._apiCall("chat.update", {ts: countdown.message, channel: countdown.channel, text: text});
}
}, 1000);
}
});
bot.addCommand("s4 help", "Show this help.", function(msg, args, channel, username){
var message = "I'm s4, the StudentRND Self-Operating Slack System. Here's what I can do:";
for(var i in bot.commands){
var command = bot.commands[i];
message += "\n" + command.trigger + " - " + command.help;
}
bot.sendMessage(message, channel);
});
bot.addCommand("s4 ready", "Ready.", function(msg, args, channel, username){
bot.sendMessage("Ready.", channel);
});
bot.addCommand("s4 whois", "Show s5 information of specified user.", function(msg, args, channel, username){
if(args[0] === "me"){args[0] = username}
var keys = [
"First name",
"Last name",
"Email",
"Phone"
];
s5.getUser(args[0], function(user){
var message = "Here's `" + user.username + "`:" +
"\nhttps://s5.studentrnd.org/photo/" + user.username + "_128.jpg";
for(var i in keys){
var key = keys[i];
if(user[key.replace(/ /gi, "_").toLowerCase()]){
message += "\n" + key + ": " + user[key.replace(/ /gi, "_").toLowerCase()];
}
}
bot.sendMessage(message, channel);
});
});
bot.addCommand("s4 registrations", "Get number of registrations for specified CodeDay.", function(msg, args, channel, username){
clear.getRegionByWebName(args[0], function(region){
if(!region){bot.sendMessage("That event doesn't exist!");return}
clear.getEventById(region.current_event.id, function(event){
var registrations = (parseInt(event.registration_info.max) - parseInt(event.registration_info.remaining));
var message = "CodeDay " + event.region_name + " has " + registrations + " registrations.";
bot.sendMessage(message, channel);
});
});
});
bot.addCommand("s4 rm", "Get Regional Manager for specified event.", function(msg, args, channel, username){
getRegionalManager(msg, args, channel, username, bot);
});
bot.addCommand("s4 evangelist", "Get Evangelist for specified event.", function(msg, args, channel, username){
getEvangelist(msg, args, channel, username, bot);
});
bot.addCommand("s4 regions", "Get regions that can be used for `s4 rm`, `s4 evangelist`, and `s4 registrations`.", function(msg, args, channel, username){
clear.getRegions(function(regions){
var message = "Available regions:";
for(var i in regions){
var region = regions[i];
message += "\n" + region.webname;
}
bot.sendMessage(message, channel);
});
});
bot.addCommand("s4 lookup", "Look up a person by their first name, last name, and email. Format: `[first] [last] [email]`", function(msg, args, channel, username){
blockspring.runParsed("b5bb470b4082254bf538a7bacac3f0cb", { "first_name": args[0], "last_name": args[1], "domain": args[3], "RAPPORTIVE_TOKEN ": null }, { api_key: "br_2046_7608f4f217ab050f599d945f7199f98da91d482e"}, function(res) {
bot.sendMessage(JSON.stringify(res));
});
});
bot.addCommand("s4 post", "Post to the StudentRND Community. Format: `[title]: [url] in category [category name]`", function(msg, args, channel, username){
var urlRegex = /[-a-zA-Z0-9@:%_\+.~#?&\/\/=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)?/gi;
discourse.get("/users/" + username + ".json", {}, function(err, data){
if(typeof(data) === "string") data = JSON.parse(data);
if(data.error_type && data.error_type === "not_found"){
bot.sendMessage("You aren't a member of the Community!", channel);
}else{
try{
var content = msg.split("in category")[0].trim(),
wantedCategory = msg.split("in category")[1].trim().toLowerCase(),
url = content.match(urlRegex)[0],
title = content.replace(urlRegex, "").replace(":", "").replace("<", "").replace(">", "").trim();
}catch(e){
bot.sendMessage("I couldn't understand you, please use this command like `[title]: [url] in category [category name]`.", channel);
return;
}
bot.sendMessage("Looking up category...", channel);
discourse.get("/categories.json", {}, function(err, data){
if(typeof(data) === "string") data = JSON.parse(data);
var postCategory;
data.category_list.categories.forEach(function(category){
if(category.name.toLowerCase().indexOf(wantedCategory) !== -1){
postCategory = category;
}
});
if(postCategory){
bot.sendMessage("I found a category named \"" + postCategory.name + "\". Is this what you wanted?", channel);
bot.setIntent(username, channel, function(message, channel, username){
if(message.toLowerCase().indexOf("y") === 0){
bot.sendMessage("Alright, I'll post it. Hold on...", channel);
og(url, function(err, metadata){
var body = url + "\n\n";
if(metadata && metadata.title) body += "# " + metadata.title + "\n";
if(metadata && metadata.description) body += metadata.description + "\n\n";
body += "_(posted automatically via [s4](https://git.io/s4); @" + username + " should add some details!)_";
discourse.post("/posts", {api_username: username, title: title, raw: body}, function(err, data){
if(typeof(data) === "string") data = JSON.parse(data);
if(data.topic_id){
discourse.put("/t/" + data.topic_id, {category_id: postCategory.id}, function(err){
bot.sendMessage("Done! Here's your post: " + config.DISCOURSE_URL + "/t/" + data.topic_id, channel);
});
}
});
});
return true;
}else if(message.toLowerCase().indexOf("n") === 0){
bot.sendMessage("Okay, I won't post it.", channel);
return true;
}else{
bot.sendMessage("Sorry, I didn't understand you. Please say yes or no.", channel);
return false;
}
});
}else{
bot.sendMessage("Sorry, I couldn't find that category.", channel);
}
});
}
});
});
bot.addTrigger(/(regional manager|rm|evangelist) for ([A-z ]+)/gi, function(msg, matches, channel, username){
if(matches[2]){
matches[2] = matches[2].replace(/CodeDay/gi, "").trim();
switch(matches[1].toLowerCase()){
case "regional manager":
getRegionalManager(msg, matches[2].split(" "), channel, username, bot);
break;
case "rm":
getRegionalManager(msg, matches[2].split(" "), channel, username, bot);
break;
case "evangelist":
getEvangelist(msg, matches[2].split(" "), channel, username, bot);
break;
}
}else{
bot.sendMessage("To search for an Evangelist/RM, use either `s4 rm [region]` or `s4 evangelist [region]`.", channel);
}
});
bot.on('unknownResponse', function(msg, channel, username, extra){
if(msg.indexOf("s4") === 0){
cleverbot.write(msg.substr(3).trim(), channel, function(d){
bot.sendMessage(d.message.replace(/Cleverbot/gi, "s4"), channel);
});
}
});
slack.on('open', function(){
slack.ws.on('message', function(message){
message = JSON.parse(message);
if(message.text && message.text === "[countdown_start]" && message.ok){
console.log("Countdown message ts: " + message.ts);
countdown.message = message.ts;
}
});
});
};