-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathburritoBobBot.js
317 lines (271 loc) · 9.18 KB
/
burritoBobBot.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
316
317
'use strict';
const Slackbots = require('slackbots');
const UserModel = require('./lib/User.js');
const Utils = require('./lib/Utilities');
const express = require('express');
/* Variables */
var orderInProgress = false;
var orderStartedBy = null;
var orders = [];
var options = ['vegetarian', 'ham', 'bacon', 'sausage', 'chorizo'];
var users = [];
var user = null;
/* Configure Express */
const app = express();
app.get('/', (req, res) => res.send('Keep me running!'));
app.listen(process.env.PORT || 3000, () => console.log('App is running'));
const bot = new Slackbots({
token: process.env.BOT_API_KEY,
name: process.env.BOT_NAME || 'burritobob'
});
bot.on('start', function() {
console.log('started');
bot.getUsers().then(_users => {
users = _users.members;
console.log('users loaded');
});
_loadBotUser();
});
bot.on('message', function(message) {
const messageUser = _getUserById(message.user);
if (messageUser) {
console.log('messageUser', messageUser.name);
}
if (Utils.isChatMessage(message) && Utils.isDirectMessage(message) && !Utils.isFromBurritoBobBot(message)) {
if (orderInProgress) {
var _userOrder = _findUserOrder(message.user);
switch (_userOrder.order.step) {
case 1:
_processStep1(message);
break;
case 2:
_processStep2(message);
break;
case 3:
_processStep3(message);
break;
case 4:
_processStep4(message);
break;
case 5:
_processStep5(message);
break;
default:
bot.postMessageToUser(messageUser.name, 'An order is already in progress, please be patient.');
}
} else {
if (Utils.isMentioningBurritos(message)) {
_startOrder(messageUser);
}
}
}
});
/**
* Starts the ordering process
* @param {object} originalMessage
* @private
*/
const _startOrder = function (messageUser) {
bot.postMessageToUser(messageUser.name, "I will ask everyone what they want and get back to you in a minute.").then(function () {
// get a list of users so the bot can start the order process with each of them
var _users = [];
users.forEach(_user => {
_users.push(new UserModel(_user));
});
// set globals
orderInProgress = true;
orderStartedBy = messageUser;
orders = _users;
_startStep1(); // kick off the order process
setTimeout(function () { // send orders after 10 minutes
_getOrders(messageUser.name);
}, 60000 * 10);
});
};
/**
* Retrieves all of the orders and sends them to the user who started the process
* @param {object} user
* @private
*/
const _getOrders = function (_user) {
var completedOrders = [];
var inProgressOrders = [];
orders.forEach(_user => {
if (_user.wantsFood && !_user.orderInProgress) {
completedOrders.push(_user.fullName + " wants a " + _user.order.meat + ' ' + _user.order.type + ' with ' + _user.order.salsa + ' salsa\nSpecial Instructions: ' + _user.order.specialInstructions);
}
});
orders.forEach(_user => {
if (_user.wantsFood && _user.orderInProgress) {
inProgressOrders.push(_user.fullName);
}
});
bot.postMessageToUser(_user, completedOrders.join('\n') + "\n\nPeople who started orders but didn't complete the order:\n" + inProgressOrders.join('\n'));
_reset();
};
/**
* Start step 1 - send message to each user asking them if they want to order
* @private
*/
const _startStep1 = function () {
users.forEach(_user => {
if (_user.name !== 'slackbot') { //|| _user.name === 'joeseckelman') TODO: Remove Kent check
bot.postMessageToUser(_user.name, "WHOA! " + orderStartedBy.real_name + " wants to go get Jalapenos for breakfast. Would you like to place an order?");
}
})
};
/**
* Process step 1 and kick off step 2
* @param {object} originalMessage
* @private
*/
const _processStep1 = function(_message) {
var _userOrder = _findUserOrder(_message.user);
if (_userOrder.wantsFood === null) {
if (_message.text.toLowerCase().indexOf('yes') > -1) {
_userOrder.orderInProgress = true;
_userOrder.wantsFood = true;
_userOrder.order.step++;
_startStep2(_message.user);
} else if (_message.text.toLowerCase().indexOf('no') > -1) {
_userOrder.wantsFood = false;
} else {
// User didn't answer yes or no - send message to retry.
var messageUser = _getUserById(_message.user);
bot.postMessageToUser(messageUser.name, "I don't think you understood. That was a 'yes' or 'no' question...please try again.");
}
}
};
/**
* Start step 2 - ask what they want
* @param {object} user
* @private
*/
const _startStep2 = function(_user) {
var messageUser = _getUserById(_user);
bot.postMessageToUser(messageUser.name, "Great! Would you like a burrito or a taco?");
};
/**
* Process step 2 and kick off step 3
* @param {object} originalMessage
* @private
*/
const _processStep2 = function(_message) {
var _userOrder = _findUserOrder(_message.user);
if (_message.text.toLowerCase().indexOf('taco') > -1) {
_userOrder.order.type = 'taco';
_userOrder.order.step++;
_startStep3(_message.user);
} else if (_message.text.toLowerCase().indexOf('burrito') > -1) {
_userOrder.order.type = 'burrito';
_userOrder.order.step++;
_startStep3(_message.user);
} else {
var messageUser = _getUserById(_message.user);
bot.postMessageToUser(messageUser.name, "I have no idea what you're asking for - do you want a burrito or taco?");
}
};
/**
* Start step 3 - Ask what they want in burrito/taco
* @param {object} user
* @private
*/
const _startStep3 = function(_user) {
var messageUser = _getUserById(_user);
var optionsString = options.join("\n");
bot.postMessageToUser(messageUser.name, "Good choice. What would you like in it? Your options are: \n" + optionsString);
};
/**
* Process step 3 and proceed to step 4
* @param {object} originalMessage
* @private
*/
const _processStep3 = function(_message) {
var _userOrder = _findUserOrder(_message.user);
var found = options.find(option => {
return _message.text.toLowerCase().indexOf(option) > -1;
});
if (found) {
_userOrder.order.meat = found;
_userOrder.order.step++;
_startStep4(_message.user);
} else {
var messageUser = _getUserById(_message.user);
bot.postMessageToUser(messageUser.name, "This isn't they type of place where you can order off menu. Let's stick to the list provided.....can you try that again?");
}
};
/**
* Start step 4 - ask about salsa
* @param {object} user
* @private
*/
const _startStep4 = function(_user) {
var messageUser = _getUserById(_user);
bot.postMessageToUser(messageUser.name, "Got it - let's talk salsa, would you like hot or mild?");
};
/**
* Process step 4 and move to step 5
* @param {object} originalMessage
* @private
*/
const _processStep4 = function(_message) {
var _userOrder = _findUserOrder(_message.user);
if (_message.text.toLowerCase().indexOf('hot') > -1) {
_userOrder.order.salsa = 'hot';
_userOrder.order.step++;
_startStep5(_message.user);
} else if (_message.text.toLowerCase().indexOf('mild') > -1) {
_userOrder.order.salsa = 'mild';
_userOrder.order.step++;
_startStep5(_message.user);
} else {
var messageUser = _getUserById(_message.user);
bot.postMessageToUser(messageUser.name, "I don't think they have that kind of salsa, how about hot or mild?");
}
};
/**
* start step 5 - ask about special instructions
* @param {object} user
* @private
*/
const _startStep5 = function(_user) {
var messageUser = _getUserById(_user);
bot.postMessageToUser(messageUser.name, "Ok, one last thing - are there any special instructions I should know about?");
};
/**
* process step 5
* @param {object} originalMessage
* @private
*/
const _processStep5 = function(_message) {
var _userOrder = _findUserOrder(_message.user);
_userOrder.order.specialInstructions = _message.text;
_userOrder.orderInProgress = false;
var messageUser = _getUserById(_message.user);
bot.postMessageToUser(messageUser.name, "Thanks - I'll let " + orderStartedBy.real_name + " know about your order.");
};
/**
* Lookup Functions
*/
const _getUserById = function (user) {
var found = users.filter(function (_user) {
return _user.id === user;
})[0];
return found;
};
const _findUserOrder = function(_user) {
var found = orders.find(_order => {
return _order.id === _user;
});
return found;
};
const _reset = function () {
orderInProgress = false;
orderStartedBy = null;
orders = [];
};
const _loadBotUser = function () {
user = users.filter(function (user) {
return user.name === name;
})[0];
};