This repository has been archived by the owner on Mar 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_bot.js
229 lines (217 loc) · 8.25 KB
/
slack_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
if (!process.env.token) {
console.log('Error: Specify token in environment');
process.exit(1);
}
var Botkit = require('./lib/Botkit.js');
var os = require('os');
var controller = Botkit.slackbot({
debug: true,
});
var bot = controller.spawn({ token: process.env.token }).startRTM();
var Jobcan = require('./beat-gripper/Jobcan.js');
var jobcan = new Jobcan(process.env.apiKey, process.env.apiJobcanUrl, process.env.apiJobcanClient);
var UserStorage = require('./beat-gripper/UserStorage.js');
var userStorage = new UserStorage(process.env.MONGODB_URI, process.env.secret, process.env.scheme);
var ReplyMessages = require('./messages.js');
controller.hears(['出勤'], 'direct_message,direct_mention,mention', function(bot, message) {
userStorage.get(message.user, function(err, users) {
if (users.length === 0) {
bot.reply(message, 'I do not know your password yet!');
bot.reply(message, 'Say `my password is ***`');
} else if (!users[0].jobcanUser) {
bot.reply(message, 'I do not know your userid yet!');
bot.reply(message, 'Say `my user id is ***`');
} else {
var password = users[0].password;
var jobcanUser = users[0].jobcanUser;
jobcan.attend(jobcanUser, password, function(error, response, body) {
switch (response.statusCode){
case 400:
if (body.errorMessage.match(/working/)) {
bot.reply(message, ReplyMessages.random(ReplyMessages.AlreadyAttend));
} else if (body.errorMessage.match(/resting/)) {
bot.reply(message, ReplyMessages.random(ReplyMessages.AlreadyLeft));
}
bot.reply(message, body.errorMessage);
break;
case 401:
bot.reply(message, body.errorMessage);
break;
case 504:
bot.reply(message, ReplyMessages.random(ReplyMessages.Error));
break;
case 200:
bot.api.reactions.add({ timestamp: message.ts, channel: message.channel, name: 'robot_face' },
function(err) {
if (err) bot.botkit.log('Failed to add emoji reaction :(', err);
});
break;
default:
bot.reply(message, 'unhandled error occurred.');
break;
}
});
}
});
});
controller.hears(['退勤'], 'direct_message,direct_mention,mention', function(bot, message) {
userStorage.get(message.user, function(err, users) {
if (users.length === 0) {
bot.reply(message, 'I do not know your password yet!');
bot.reply(message, 'Say `my password is ***`');
} else if (!users[0].jobcanUser) {
bot.reply(message, 'I do not know your user id yet!');
bot.reply(message, 'Say `my user id is ***`');
} else {
var password = users[0].password;
var jobcanUser = users[0].jobcanUser;
jobcan.leave(jobcanUser, password, function (error, response, body) {
switch (response.statusCode) {
case 400:
if (body.errorMessage.match(/having_breakfast/)) {
bot.reply(message, ReplyMessages.random(ReplyMessages.NotYet));
} else if (body.errorMessage.match(/resting/)) {
bot.reply(message, ReplyMessages.random(ReplyMessages.AlreadyLeft));
}
bot.reply(message, body.errorMessage);
break;
case 401:
bot.reply(message, body.errorMessage);
break;
case 504:
bot.reply(message, ReplyMessages.random(ReplyMessages.Error));
break;
case 200:
bot.api.reactions.add({timestamp: message.ts, channel: message.channel, name: 'robot_face'},
function (err) {
if (err) bot.botkit.log('Failed to add emoji reaction :(', err);
});
break;
default:
bot.reply(message, 'unhandled error occurred.');
break;
}
});
}
});
});
controller.hears(['my password is (.*)'], 'direct_message,direct_mention,mention', function(bot, message) {
var password = message.match[1];
userStorage.get(message.user, function(err, users) {
if (users.length === 0) {
userStorage.set(message.user, password, function(err, results) {
bot.reply(message, 'Got it. I remember your password is ' + password);
});
} else {
userStorage.update(message.user, password, function(err, results) {
bot.reply(message, 'Got it. I remember your password is ' + password);
});
}
});
});
controller.hears(['my user id is (.*)'], 'direct_message,direct_mention,mention', function(bot, message) {
var jobcanUser = message.match[1];
userStorage.get(message.user, function(err, users) {
if (users.length === 0) {
userStorage.setJobcan(message.user, jobcanUser, function(err, results) {
bot.reply(message, 'Got it. I remember your jobcan user is ' + jobcanUser);
});
} else {
userStorage.updateJobcan(message.user, jobcanUser, function(err, results) {
bot.reply(message, 'Got it. I remember your jobcan user is ' + jobcanUser);
});
}
});
});
controller.hears(['what is my password'], 'direct_message,direct_mention,mention', function(bot, message) {
userStorage.get(message.user, function(err, users) {
if (users.length !== 0) {
bot.reply(message, 'Your password is ' + users[0].password);
} else {
bot.startConversation(message, function(err, convo) {
if (!err) {
convo.say('I do not know your password yet!');
convo.ask('What is your password?', function(response, convo) {
convo.ask('Your password is `' + response.text + '`?', [
{
pattern: 'yes',
callback: function(response, convo) {
convo.next();
}
},
{
pattern: 'no',
callback: function(response, convo) {
convo.stop();
}
},
{
default: true,
callback: function(response, convo) {
convo.repeat();
convo.next();
}
}
]);
convo.next();
}, {'key': 'password'});
convo.on('end', function(convo) {
if (convo.status == 'completed') {
bot.reply(message, 'OK! I will update my dossier...');
userStorage.get(message.user, function(err, user) {
var password = convo.extractResponse('password');
if (users.length === 0) {
userStorage.set(message.user, password, function(err, results) {
console.log(results);
bot.reply(message, 'Got it. I remember your password is ' + password);
});
} else {
userStorage.update(message.user, password, function(err, results) {
console.log(results);
bot.reply(message, 'Got it. I remember your password is ' + password);
});
}
});
} else {
bot.reply(message, 'OK, nevermind!');
}
});
}
});
}
});
});
controller.hears(['ありがとう', 'サンキュー', 'thanks', 'thank you', 'thx'],
'direct_message,direct_mention,mention', function(bot, message) {
bot.reply(message, ReplyMessages.random(ReplyMessages.ThankYou));
}
);
controller.hears(['uptime', 'identify yourself', 'who are you', 'what is your name'],
'direct_message,direct_mention,mention', function(bot, message) {
var hostname = os.hostname();
var uptime = formatUptime(process.uptime());
bot.reply(message,
':robot_face: I am a bot named <@' + bot.identity.name +
'>. I have been running for ' + uptime + ' on ' + hostname + '.');
}
);
controller.hears('', 'direct_message,direct_mention,mention', function(bot, message) {
bot.reply(message, ReplyMessages.random(ReplyMessages.NothingToDo));
}
);
function formatUptime(uptime) {
var unit = 'second';
if (uptime > 60) {
uptime = uptime / 60;
unit = 'minute';
}
if (uptime > 60) {
uptime = uptime / 60;
unit = 'hour';
}
if (uptime != 1) {
unit = unit + 's';
}
uptime = uptime + ' ' + unit;
return uptime;
}