forked from UncannyBingo/easy-peasy-slash-command-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
243 lines (209 loc) · 7.79 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
const Botkit = require('botkit');
const mongoose = require('mongoose');
const moment = require('moment');
require('moment-duration-format');
const helpers = require('./helpers.js');
/**
* Botkit config
*/
if (!process.env.CLIENT_ID ||
!process.env.CLIENT_SECRET ||
!process.env.PORT ||
!process.env.VERIFICATION_TOKEN) {
console.log('Error: Specify CLIENT_ID, CLIENT_SECRET, VERIFICATION_TOKEN and PORT in environment');
process.exit(1);
}
var config = {};
if (process.env.MONGOLAB_URI) {
const BotkitStorage = require('botkit-storage-mongo');
config = {
storage: BotkitStorage({ mongoUri: process.env.MONGOLAB_URI })
};
}
else {
config = {
json_file_store: './db_slackbutton_slash_command/'
};
}
const controller = Botkit.slackbot(config).configureSlackApp(
{
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
scopes: ['commands']
}
);
/**
* Mongo config
*/
mongoose.connect('mongodb://localhost/test');
const TimeSchema = new mongoose.Schema({
time: Date,
type: String,
userId: String
});
const Time = mongoose.model('Time', TimeSchema);
/**
* Login
*/
controller.setupWebserver(process.env.PORT, function (err, webserver) {
controller.createWebhookEndpoints(controller.webserver);
controller.createOauthEndpoints(controller.webserver, function (err, req, res) {
if (err) {
res.status(500).send('ERROR: ' + err);
}
else {
res.send('Success!');
}
});
});
/**
* Commands
*/
controller.on('slash_command', function (slashCommand, message) {
// Check token match
if (message.token !== process.env.VERIFICATION_TOKEN) {
return false;
}
// Check for `work` command
if (message.command === '/devworkbot' || message.command === '/workbot') {
const timeNow = new Date();
var timeStart;
// Command `start`
if (message.text === 'start') {
timeStart = moment();
// Checking if start time for current day already exists
var todayStart = new Date();
todayStart.setHours(0, 0, 0, 0);
var todayEnd = new Date();
todayEnd.setHours(23, 59, 59, 999);
Time.find({
type: 'start',
time: {
'$gte': todayStart,
'$lte': todayEnd
}
}).exec(function(err, times) {
if (err) {
// TODO: output proper error
console.log('I got some error!');
}
// Refusing inserting time started
else if (times.length) {
slashCommand.replyPublic(message,
'This is odd, haven\'t you already started to work today?'
);
}
// Inserting time started
else {
Time.create({
time: timeStart, // NOTE: date will be written as ISO format in database
type: 'start',
userId: message.user_id
}, function(err, time) {
if (err) {
// TODO: output proper error
console.log('I got some error!');
}
else {
slashCommand.replyPublic(message,
'Started working time: *' + moment(timeStart).format('dddd, DD.MM.YYYY [at] HH:mm') + '*.\n' +
'Time to get work done, we need to make some money.'
);
}
});
}
});
}
// Command `status`
else if (message.text === 'status') {
Time.find({ userId: message.user_id })
.sort({ _id: -1 }).limit(1)
.exec(function(err, times) {
if (err) {
// TODO: output proper error
console.log('I got some error!');
}
else {
const timeStart = times[0].time;
// TODO: output proper message if more than 8 hours have passed by
slashCommand.replyPublic(message,
'Remaining working time: *' + helpers.timeRemain(timeStart, timeNow) + '*.\n' +
'Oh, the time flies so fast.'
);
}
});
}
// Command `end`
else if (message.text === 'end') {
Time.find({userId: message.user_id})
.sort({_id: -1}).limit(1)
.exec(function (err, times) {
if (err) {
// TODO: output proper error
console.log('I got some error!');
}
else {
const timeStart = times[0].time;
Time.create({
time: timeNow, // NOTE: date will be written as ISO format in database
type: 'end',
userId: message.user_id
}, function (err, time) {
if (err) {
// TODO: output proper error
console.log('I got some error!');
}
else {
slashCommand.replyPublic(message,
'Ended working time: *' + moment(timeNow).format('dddd, DD.MM.YYYY [at] HH:mm') + '*.\n' +
'Total time: *' + helpers.timeDuration(timeStart, timeNow) + '*.\n' +
'Well, tomorrow is another day. Good job!'
);
}
});
}
});
}
// Command `list`
else if (message.text === 'list') {
Time.find({ userId: message.user_id })
.sort({ time: 1 })
.exec(function(err, times) {
if (err) {
// TODO: output proper error
console.log('I got some error!');
}
else {
const mapper = {
start: 'Start:',
end: 'End: '
};
var output = 'Listing all recorded times\n\n';
output += '```';
times.forEach(function(time) {
output += mapper[time.type] + ' ' +
moment(time.time).format('ddd, DD.MM.YY, HH:mm') + ' | ' +
time._id + ' ' + '\n';
});
output += '```';
slashCommand.replyPublic(message, output);
}
});
}
// Command `help`
else if (message.text === 'help') {
slashCommand.replyPublic(message,
'Here are commands you can use:\n\n' +
'`/workbot start` sets start of working time.\n' +
'`/workbot status` shows remaining working time.\n' +
'`/workbot end` sets end of working time.\n' +
'`/workbot list` shows all working times.'
);
}
else {
slashCommand.replyPublic(message,
'I\'m afraid I don\'t know how to ' + message.command + ' yet.'
);
}
}
});