-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.js
244 lines (222 loc) · 8.09 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
'use strict';
// require libraries
require('dotenv').load();
const Config = require('./config');
const Scraper = require('./scraper');
const Chance = require('chance');
// check .env file
if (!process.env.SLACK_API_TOKEN) {
console.log('Error: Specify slack token in environment');
process.exit(1);
}
// init main libraries
var scraper = new Scraper();
const chance = new Chance();
// init botkit
var Botkit = require('botkit/lib/Botkit.js');
var controller = Botkit.slackbot({
debug: process.env.SLACKBOT_DEBUG === 'TRUE'
});
controller.spawn({
token: process.env.SLACK_API_TOKEN
}).startRTM();
/**
* Random greeting
*/
controller.hears(
Config.GREETING_QUESTIONS,
['direct_message', 'direct_mention', 'mention', 'ambient'],
function (bot, message) {
bot.reply(message, Config.GREETING_ANSWERS[chance.natural({
min: 0,
max: Config.GREETING_ANSWERS.length - 1
})]);
}
);
/**
* Question for restaurants
*/
controller.hears(
Config.QUESTION_BY_RESTAURANT,
['direct_message', 'direct_mention', 'mention', 'ambient'],
function (bot, message) {
bot.reply(message, {type: 'typing'});
var hit = message.match[1];
var idSource;
let rests = Config.RESTAURANTS;
for (let i = 0; i <= rests.length - 1; i++) {
if (hit.search(new RegExp(rests[i].regex, 'i')) > -1) {
idSource = rests[i].source;
break;
}
}
scraper.getMenue(idSource).then(function (result) {
if (result.items.length === 0) {
bot.reply(message, 'Pre ' + result.name + ' dnes nemám žiadnu ponuku :(');
} else {
var items = [];
for (var i = 0; i < result.items.length; i++) {
let name = result.items[i].name;
let price = result.items[i].price;
items.push(name + (price !== '' ? ' - *' + price + '*' : ''));
}
bot.reply(message, {
text: 'Aktuálna ponuka pre ' + result.name,
attachments: [{
fallback: 'Denné menu pre ' + result.name + ': ' + result.web,
title: result.name,
title_link: result.web,
text: items.join('\n'),
mrkdwn_in: ['text']
}]
});
}
}).catch(() => bot.reply(message, 'Prepac, nieco se pokazilo a nemozem najst ponuku.'));
}
);
/**
* Question for menues
*/
controller.hears(
Config.QUESTION_RESTAURANTS,
['direct_message', 'direct_mention', 'mention', 'ambient'],
function (bot, message) {
bot.reply(message, {type: 'typing'});
scraper.getAllMenus().then(function (results) {
var menues = [];
for (var key in results) {
if (results.hasOwnProperty(key)) {
var items = [];
for (var i = 0; i < results[key].items.length; i++) {
let name = results[key].items[i].name;
let price = results[key].items[i].price;
items.push(name + (price !== '' ? ' - *' + price + '*' : ''));
}
if (items.length > 0) {
menues.push({
fallback: 'Denné menu pre ' + results[key].name + ': ' + results[key].web,
title: results[key].name,
title_link: results[key].web,
text: items.join('\n'),
mrkdwn_in: ['text']
});
}
}
}
if (menues.length > 0) {
bot.reply(message, {
text: 'Takto vypadá aktuálna ponuka',
attachments: menues
});
} else {
bot.reply(message, 'Bohuzial pre dnesok nemam ziadnu ponuku.');
}
}).catch((error) => console.log('Error:', error));
}
);
/**
* Recomended restaurants
*/
controller.hears(
Config.QUESTION_RECOMENDED,
['direct_message', 'direct_mention', 'mention', 'ambient'],
function (bot, message) {
bot.reply(message, {type: 'typing'});
scraper.getAllMenus().then(function (results) {
if (Object.keys(results).length === 0) {
bot.reply(message, 'Dneska ti asi nic neodporucim. Nemam z coho.');
} else {
var restaurants = {
fallback: [],
text: []
};
var i = 0;
for (var key in results) {
if (results.hasOwnProperty(key)) {
if (i >= Config.MAX_VOTE_EMOJIS.length) {
bot.say(message, 'Hmm... mam nejak viac moznosti co zvladam...');
break;
}
// message
if (results[key].items.length > 0) {
++i;
restaurants.text.push(Config.MAX_VOTE_EMOJIS[i] + ' ' + results[key].name);
restaurants.fallback.push('(' + i + ') ' + results[key].name);
}
}
}
bot.reply(message, {
text: Config.RESTAURANT_GIF[chance.natural({
min: 0,
max: Config.RESTAURANT_GIF.length - 1
})]
});
bot.reply(message, {
text: '>*Kam pôjdeme na obed?*',
attachments: [{
fallback: restaurants.fallback.join(' | '),
color: 'good',
text: restaurants.text.join('\n'),
mrkdwn_in: ['text']
}]
}, function (error, json) {
if (!error) {
var j = 0;
do {
bot.api.reactions.add({
timestamp: json.ts,
channel: json.channel,
name: Config.MAX_VOTE_EMOJIS[j].replace(/:/gi, '')
}, function (err) {
if (err) {
bot.botkit.log('Failed to add emoji reaction ' + Config.MAX_VOTE_EMOJIS[j], err);
}
});
} while (++j < i);
}
}
);
}
}).catch(function (err) {
console.log(err);
let messages = [
'som zmateny',
'nieco mi hapruje pod kapotou'
];
bot.reply(
message,
'Chcel som vam pripravit hlasovanie, ale '
+ messages[chance.natural({
min: 0,
max: messages.length - 1
})] + '.'
);
});
}
);
/*eslint-disable no-process-exit */
function exitHandler(options, err) {
var messages = [
'Kto zakopol o ten kabel? ... ```obed-bot has left the room```'
];
// pre vsetky kanaly kde je bot aktualne pritomny
// bot.say(messages[0]);
if (options.cleanup) {
//console.log('clean');
}
if (err) {
//console.log(err.stack);
}
// regularne odpojenie pokail sa ot ukoncil ctrl+c
if (options.exit) {
console.log(messages[0]);
controller.shutdown();
process.exit();
}
}
////do something when app is closing
//process.on('exit', exitHandler.bind(null, { cleanup : true }));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit: true}));
////catches uncaught exceptions
//process.on('uncaughtException', exitHandler.bind(null, { exit : true }));