-
Notifications
You must be signed in to change notification settings - Fork 0
/
gary.js
302 lines (252 loc) · 9.45 KB
/
gary.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
if (!process.env.TOKEN) {
console.log('Error: Specify TOKEN in environment');
process.exit(1);
}
if (!process.env.MONGOPASS) {
console.log('Error: Specify MONGOPASS in environment');
process.exit(1);
}
var Botkit = require('botkit');
var os = require('os');
var _ = require('lodash');
var mongoUri = `mongodb+srv://gary:${process.env.MONGOPASS}@garyville-forjf.mongodb.net/test?retryWrites=true`
var storage = require('botkit-storage-mongo')({ mongoUri: mongoUri, tables: ['learns'] });
var controller = Botkit.slackbot({
debug: true,
storage: storage,
});
var bot = controller.spawn({
token: process.env.TOKEN
}).startRTM();
const LEARN = 'learn';
const UNLEARN = 'unlearn';
controller.hears(['(.*)'], 'mention', function(bot, message) {
console.log('gary hear');
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'gary',
}, function(err, res) {
if (err) {
bot.botkit.log('Failed to add emoji reaction :(', err);
}
});
controller.storage.users.get(message.user, function(err, user) {
if (user && user.name) {
bot.reply(message, 'Hello ' + user.name + '!!');
} else {
bot.reply(message, 'Meow.');
}
});
});
controller.hears([/^\?(\w+)\s+(\w+)\s*(.+)?/], 'direct_message,ambient', function(bot, message) {
if (message.match[1] === LEARN && message.match.length > 3) {
learn(bot, message);
} else if (message.match[1] === UNLEARN) {
unlearn(bot, message);
} else {
fetch(bot, message);
}
});
controller.hears([/^\?(\w+)\s*/], 'direct_message,ambient', function(bot, message) {
// I don't think this gate is required.
if (message.match[1] !== LEARN && message.match[1] !== UNLEARN) {
fetch(bot, message);
}
});
controller.hears(['call me (.*)', 'my name is (.*)'], 'direct_message,direct_mention,mention', function(bot, message) {
var name = message.match[1];
controller.storage.users.get(message.user, function(err, user) {
if (!user) {
user = {
id: message.user,
};
}
user.name = name;
controller.storage.users.save(user, function(err, id) {
bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.');
});
});
});
controller.hears(['what is my name', 'who am i'], 'direct_message,direct_mention,mention', function(bot, message) {
controller.storage.users.get(message.user, function(err, user) {
if (user && user.name) {
bot.reply(message, 'Your name is ' + user.name);
} else {
bot.startConversation(message, function(err, convo) {
if (!err) {
convo.say('I do not know your name yet!');
convo.ask('What should I call you?', function(response, convo) {
convo.ask('You want me to call you `' + response.text + '`?', [{
pattern: 'yes',
callback: function(response, convo) {
// since no further messages are queued after this,
// the conversation will end naturally with status == 'completed'
convo.next();
}
},
{
pattern: 'no',
callback: function(response, convo) {
// stop the conversation. this will cause it to end with status == 'stopped'
convo.stop();
}
},
{
default: true,
callback: function(response, convo) {
convo.repeat();
convo.next();
}
}
]);
convo.next();
}, { 'key': 'nickname' }); // store the results in a field called nickname
convo.on('end', function(convo) {
if (convo.status == 'completed') {
bot.reply(message, 'OK! I will update my dossier...');
controller.storage.users.get(message.user, function(err, user) {
if (!user) {
user = {
id: message.user,
};
}
user.name = convo.extractResponse('nickname');
controller.storage.users.save(user, function(err, id) {
bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.');
});
});
} else {
// this happens if the conversation ended prematurely for some reason
bot.reply(message, 'OK, nevermind!');
}
});
}
});
}
});
});
controller.hears(['shutdown'], 'direct_message,direct_mention,mention', function(bot, message) {
bot.startConversation(message, function(err, convo) {
convo.ask('Are you sure you want me to shutdown?', [{
pattern: bot.utterances.yes,
callback: function(response, convo) {
convo.say('Bye!');
convo.next();
setTimeout(function() {
process.exit();
}, 3000);
}
},
{
pattern: bot.utterances.no,
default: true,
callback: function(response, convo) {
convo.say('*Phew!*');
convo.next();
}
}
]);
});
});
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 + '.');
});
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;
}
function learn(bot, message) {
var id = message.match[2],
value = message.match[3];
controller.storage.learns.get(id, function(error, currentValue) {
if (error) {
currentValue = [];
} else {
try {
currentValue = currentValue.value;
} catch (e) {
console.error('#learn JSON.parse ', e);
bot.reply(message, 'Uh-oh no no!');
}
}
var newValue = currentValue.concat([value]);
controller.storage.learns.save({ id: id, value: newValue }).then(function() {
bot.reply(message, 'Okay, learned "' + value + '" to ' + id + '.');
});
});
}
function fetch(bot, message) {
var id = message.match[1],
response_id = message.match[2];
console.log('getting ' + id + ',' + response_id);
controller.storage.learns.get(id, function(error, value) {
console.log(value, error);
if (error) {
bot.reply(message, "Sorry, I don't know anything about \"" + id + ".\"");
return;
}
var responses = [];
try {
responses = value.value;
} catch (e) {
console.error('#fetch JSON.parse ', e);
bot.reply(message, 'Uh-oh no no!');
}
if (isNaN(response_id)) {
bot.reply(message, _.sample(responses));
} else {
bot.reply(message, responses[response_id - 1]);
}
});
}
function unlearn(bot, message) {
var id = message.match[2],
response_id = message.match[3];
if (typeof response_id === 'undefined') {
bot.reply(message, "Sorry, I need to know what to forget about \"" + id + ".\"");
return;
}
controller.storage.learns.get(id, function(error, value) {
if (error) {
bot.reply(message, "Sorry, I can't unlearn things that I don't know!");
return;
}
var responses = [];
try {
responses = value.value;
} catch (e) {
console.error('#unlearn JSON.parse ', e);
bot.reply(message, 'Uh-oh no no!');
return;
}
if (isNaN(response_id)) {
responses = responses.filter(function(response) {
return response !== response_id;
});
} else {
responses.splice(response_id - 1, 1);
}
controller.storage.learns.save({ id: id, value: responses }).then(function() {
bot.reply(message, "Okay, I've forgotten that about \"" + id + ".\"");
});
});
}