forked from ibolmo/task-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
315 lines (244 loc) · 9.15 KB
/
bot.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
var config = require('./lib/config.js');
var date = require('date.js');
var fecha = require('fecha');
var Keen = require('keen-js');
var client = new Keen({
projectId: config('KEEN_ID'),
writeKey: config('KEEN_WRITE'),
readKey: config('KEEN_READ')
});
ALL = 'direct_message,direct_mention';
TRACK = config('TRACK', true);
var controller = require('./lib/Controller.js');
var storage = controller.storage;
var Channel = require('./lib/Channel.js')(controller);
var Task = require('./lib/Task.js')(controller);
var User = require('./lib/User.js')(controller);
controller.findTeamById(config('TEAM_ID'), function(err, team){
if (!team) controller.saveTeam({
id: TEAM_ID,
createdBy: 'olmo',
url: 'codergv.slack.com',
name: 'Code#RGV'
});
});
controller.hears(['^help'], ALL, function(bot, message) {
bot.reply(message, [
'*Usage*:',
'> @task list',
'> @task add {description} #section [1/1/2016] @name',
'> @task finish|done|complete {id}',
'> @task aid|assist {id}',
'> @task abandon|drop {id}',
'> @task update {id} {description} #section [1/1/2016] @name',
//'> @task note|comment {slug|id|search}',
'> @task help',
'',
'For technical support: [email protected]'
].join('\n'));
});
controller.hears(['^add'], ALL, function(bot, message) {
var task = Task.fromMessage(message);
Channel.findOrCreate(message.channel).then(function(channel){
task.addTo(channel);
channel.save().then(function(){
bot.reply(message, 'Task (id: ' + task.getId() + ') added.');
controller.trigger('task.added', [task]);
if (TRACK) channel.getName().then(function(name){
return client.addEvent('creates', {
channel: name,
task_id: task.getId(),
description: task.getDescription(),
section: task.getSection(),
due: new Date(task.getDue()).toISOString(),
assigned: task.getAssigned().toObject(),
creator: task.getCreator(),
created: task.getCreated(),
updated: task.getUpdated()
});
});
}).done();
return channel;
});
});
controller.hears(['^update'], ALL, function(bot, message) {
var id = (match(message.text, /^update *(\d+)/i)[1] || '').trim();
if (!id) return bot.reply(message, 'You need to provide a task id.');
Channel.findOrCreate(message.channel).then(function(channel){
var tasks = channel.getTasks();
if (!tasks.count()) return bot.reply(message, 'No tasks recorded. "@task help" for usage.');
var task = tasks.find(id);
if (!task) return bot.reply(message, 'Could not find the task, by the id: ' + id + '. Try @task list again.');
var previous = Object.merge({}, task.toObject());
task.setDescription((match(message.text, /^update *\d+ *([^#\[<]+)/i)[1] || '').trim())
task.setSection((match(message.text, /#([\w-]+)/)[1] || '').trim() || 'all')
task.setDue(date((match(message.text, /\[([^\]]+)\]/)[1] || '').trim() || 'in 7 days') * 1);
task.setAssigned(match(message.text, /<@([^>]+)>/ig).map(User.cleanId));
task.setUpdatedBy(message.user);
task.setUpdated(Date.now());
channel.save().then(function(){
bot.reply(message, 'Updated task (' + task.id + ').');
controller.trigger('task.updated', [task]);
if (TRACK) channel.getName().then(function(name){
return client.addEvent('updates', {
channel: name,
task_id: task.getId(),
previous: previous,
current: task.toObject(),
updatedBy: task.getUpdatedBy()
});
});
}).done();
}).done();
});
controller.hears(['^list'], ALL, function(bot, message) {
var filter = !(match(message.text, /(all)$/i)[1] || '').trim()
Channel.findOrCreate(message.channel).then(function(channel){
var tasks = channel.getTasks();
if (!tasks.count()) return bot.reply(message, 'No tasks recorded. "@task help" for usage.');
tasks.sort(function(a, b){
return new Date(a.getDue()) > new Date(b.getDue());
}).forEach(function(task){
var due = fecha.format(new Date(task.getDue()), 'shortDate');
var assigned = task.getAssigned().map(function(user){
return '<@' + user + '>';
}).join(' ');
if (!assigned.length) assigned = '_*none*_';
bot.reply(message, {
text: [
'_(' + task.getId() + ')_ ⋅ *' + due + '* ⋅ ' + assigned + ' (*' + task.getStatus() + '*)',
'> ' + task.getDescription()
].join('\n')
});
});
controller.trigger('task.list');
}).done();
});
controller.hears(['^(finish)|(done)|(complete)'], ALL, function(bot, message) {
var id = (match(message.text, /(\d+)$/i)[1] || '').trim();
if (!id) return bot.reply(message, 'You need to provide a task id.');
Channel.findOrCreate(message.channel).then(function(channel){
var tasks = channel.getTasks();
if (!tasks.count()) return bot.reply(message, 'No tasks recorded. "@task help" for usage.');
var task = tasks.find(id);
if (!task) return bot.reply(message, 'Could not find the task, by the id: ' + id + '. Try @task list again.');
task.setStatus('done');
task.setDoneBy(message.user);
channel.save().then(function(){
bot.reply(message, 'Updated task (' + task.getId() + ').');
controller.trigger('task.done', [task]);
if (TRACK) channel.getName().then(function(name){
client.addEvent('dones', {
channel: name,
task_id: task.getId(),
section: task.getSection(),
due: new Date(task.getDue()).toISOString(),
delta: Date.now() - Number(new Date(task.getDue())),
assigned: task.getAssigned().toObject(),
doneBy: task.getDoneBy()
});
}).done();
}).done();
}).done();
});
controller.hears(['^(aid)|(assists?)|(assign)'], ALL, function(bot, message){
var id = (match(message.text, /(\d+)/i)[1] || '').trim();
if (!id) return bot.reply(message, 'You need to provide a task id.');
var assign = match(message.text, /<@([^>]+)>/ig).map(User.cleanId);
if (!assign.length) assign = [message.user];
Channel.findOrCreate(message.channel).then(function(channel){
var tasks = channel.getTasks();
if (!tasks.count()) return bot.reply(message, 'No tasks recorded. "@task help" for usage.');
var task = tasks.find(id);
if (!task) return bot.reply(message, 'Could not find the task, by the id: ' + id + '. Try @task list again.');
task.getAssigned().add(assign);
channel.save().then(function(){
bot.reply(message, 'Updated task (' + id + ').');
controller.trigger('task.assign', [task]);
if (TRACK) channel.getName().then(function(name){
client.addEvent('assigns', {
channel: name,
task_id: task.getId(),
helper: message.user
});
}).done();
}).done();
}).done();
});
controller.hears(['^(abandon)|(drop)'], ALL, function(bot, message){
var id = (match(message.text, /(\d+)/i)[1] || '').trim();
if (!id) return bot.reply(message, 'You need to provide a task id.');
var assign = match(message.text, /<@([^>]+)>/ig).map(User.cleanId);
if (!assign.length) assign = [message.user];
Channel.findOrCreate(message.channel).then(function(channel){
var tasks = channel.getTasks();
if (!tasks.count()) return bot.reply(message, 'No tasks recorded. "@task help" for usage.');
var task = tasks.find(id);
if (!task) return bot.reply(message, 'Could not find the task, by the id: ' + id + '. Try @task list again.');
task.getAssigned().remove(assign);
channel.save().then(function(){
bot.reply(message, 'Updated task (' + id + ').');
controller.trigger('task.drop', [task]);
if (TRACK) channel.getName().then(function(name){
client.addEvent('abandons', {
channel: name,
task_id: task.getId(),
runaway: message.user
});
}).done();
}).done();
}).done();
});
controller.hears(['uptime'],'direct_message,direct_mention,mention',function(bot, message) {
var uptime = formatUptime(process.uptime());
bot.reply(message,':robot_face: I am a bot named <@' + bot.identity.name + '>. I have been running for ' + uptime + '.');
});
var express = require('express');
var livereload = require('express-livereload');
var app = express();
var http = require('http');
app.locals.moment = require('moment');
livereload(app, {watchDir: 'views'});
app.set('view engine', 'jade');
var COLUMNS = 'id description assigned due creator'.split(' ');
var secure = function(req, res, next){
if (req.query.key == config('SECRET')) next();
else res.redirect('http://www.codergv.org/');
};
app.get('/board', secure, function(req, res){
Channel.all().then(function(channels){
channels.waitForPending().then(function(){
res.render('board', {
channels: channels.toObject(),
KEEN_ID: config('KEEN_ID'),
KEEN_READ: config('KEEN_READ'),
})
}).done();
}).done();
});
app.get('*', function(req, res){
res.redirect('http://www.codergv.org/');
});
http.createServer(app).listen(config('PORT'), config('IP'), function() {
console.log("✔ Express server listening at %s:%d ", config('IP'), config('PORT'));
});
function formatUptime(uptime) {
var unit = 'second';
if (uptime > 60) uptime /= 60, unit = 'minute';
if (uptime > 60) uptime /= 60, unit = 'hour';
if (uptime != 1) unit += 's';
return uptime + ' ' + unit;
}
function match(string, regexp){
return string.match(regexp) || [];
}
Object.merge = function(target, source){
[].slice.call(arguments, 1).forEach(function(source){
for (var key in source) if (key in source) target[key] = source[key];
});
return target;
};
module.exports = {
app: app,
controller: controller
};