route.js
10 KB
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
const MongoClient = require('mongodb').MongoClient;
const SlackBot = require('slackbots');
const dbname = 'jokeapi';
const emoji = require('../slack_emoji');
const url = 'mongodb://localhost:27017/';
exports.startbot = ()=>{
// Get authorization to use the slackbot
const bot = new SlackBot({
token : "xoxb-582582124755-587875604934-t3xDZmtuLXVjgUMWDzAf8g1K",
name : "Joker"
});
// Start the slackbot
bot.on('start', () =>{
//Figure out user's all current channels in the team
channel = bot.getChannels();
channel.then((data)=>{
channel_length = data.channels.length;
for(i=0; i< channel_length; ++i){
bot.postMessageToChannel(data.channels[i].name, 'Have some fun with @Joker!\nFor commands write @joker --help'
, emoji.emojis('bowtie'));
}
return data;
})
.then((data)=>{
console.log('Sucessfully started app to all channels');
})
});
// Error Handler
bot.on('error', (err) => console.log(err));
//Message Handler
bot.on('message', (data) => {
if(data.type !== 'message'){
return;
}
status = data;
message_recieved = 0;
//Figure out which channel the user is sending message
//If it's first input from the user, go through this loop to store the data of channel names and ids
if(message_recieved == 0){
channel_length;
channel_names = [];
channel_ids = [];
//bot.getChannels() returns a list of all channels in the team
channel = bot.getChannels();
channel.then((data)=>{
channel_length = data.channels.length;
for(i=0; i< channel_length; ++i){
channel_names.push(data.channels[i].name);
channel_ids.push(data.channels[i].id);
}
return data;
})
.then((result)=>{
++message_recieved;
console.log("User Channel list: " + channel_names)
for(i=0; i< channel_length; ++i){
if(channel_ids[i] == status.channel)
handleMessage(status.text, channel_names[i]);
}
})
}
console.log(status);
//If it's not the first user input, goes through simple loop to shorten response time
if(message_recieved > 0){
for(i=0; i< channel_length; ++i){
if(channel_ids[i] == status.channel)
handleMessage(status.text, channel_names[i]);
}
}
//handleMessage(data.text);
});
// Responding to Data
function handleMessage(message, current_channel){
console.log(message);
//Handles message response depending on the user message
if(message.includes(' tell me')){
if(message.includes(' knock')){
knockknockJoke(current_channel);
}
else if(message.includes(' general')){
generalJoke(current_channel);
}
else if(message.includes(' random')){
randomJoke(current_channel);
}
else if(message.includes(' a joke')){
randomJoke(current_channel);
}
else if(message.includes(' programming')){
programmingJoke(current_channel);
}
else if(message.includes(' me ')){
bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth'));
}
else{
comment = "Sorry I dont' have that kind of joke.....:droplet::droplet::droplet:\nPlease use @joker --help to know what I can do!";
bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
}
}
else if(message.includes(' help')){
}
else if(message.includes(' what jokes')){
jokeTypes = ["general", 'programming', 'knock-knock'];
bot.postMessageToChannel(current_channel, `I have ${jokeTypes[0]}, ${jokeTypes[1]}, ${jokeTypes[2]} jokes!! :thumbsup: :thumbsup:`, emoji.emojis('thumbsup'));
return;
}
// else{
// const embarrased = {
// icon_emoji: ':flushed:'
// };
// const sweat = {
// icon_emoji: ':droplet:'
// };
// comment = "Sorry I'm not smart enough to understand this.....\nPlease use @joker help to know what I can do!";
// bot.postMessageToChannel('everyone', comment, embarrased);
// }
}
//Gets a random integer
function getRandomInt(max_num) {
min = Math.ceil(1);
max = Math.floor(max_num);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//Function for giving out random joke
randomJoke= (user_channel)=>{
MongoClient.connect('mongodb://localhost:27017', function (err, client){
if (err) throw err;
var db = client.db('jokeapi');
json_max = 376;
random = getRandomInt(json_max);
result = db.collection('jokes').findOne({id: random});
user = result;
user.then(function(total){
question = total.setup;
function firstFunction(){
bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing'));
}
firstFunction('everyone');
console.log('질문 불려짐');
return total;
})
.then((all)=>{
joke = all.punchline;
setTimeout(function secondFunction(){
bot.postMessageToChannel(user_channel, `${joke}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
console.log( "허무개그 전송~~~~!")
}, 3000);
})
client.close();
})
}
//Function for giving out random joke after filtering only general type jokes
generalJoke= (user_channel)=>{
MongoClient.connect(url, function (err, client){
if (err) throw err;
var db = client.db('jokeapi');
json_max = 376;
random = getRandomInt(json_max);
result = db.collection('jokes').findOne({id: random});
user = result;
user.then(function(total){
if(total.type === "general"){
question = total.setup;
joke = total.punchline;
ques_and_joke = [question, joke];
return ques_and_joke;
}
else if(total.type != "general"){
client.close();
generalJoke(user_channel);
}
})
.then((joke_info)=>{
bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing'));
console.log("일반 질문 불려짐");
return joke_info;
})
.then((info)=>{
setTimeout(function secondFunction(){
bot.postMessageToChannel(user_channel, `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
console.log( "허무개그 전송~~~~!")
}, 3000);
})
client.close();
})
};
//Function for giving out random joke after filtering only programming type jokes
programmingJoke= (user_channel)=>{
MongoClient.connect(url, function (err, client){
if (err) throw err;
var db = client.db('jokeapi');
json_max = 376;
random = getRandomInt(json_max);
result = db.collection('jokes').findOne({id: random});
user = result;
user.then(function(total){
if(total.type === "programming"){
question = total.setup;
joke = total.punchline;
ques_and_joke = [question, joke];
return ques_and_joke;
}
else if(total.type != "programming"){
client.close();
programmingJoke(user_channel);
}
})
.then((joke_info)=>{
bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing'));
console.log("프로그래밍 질문 불려짐");
return joke_info;
})
.then((info)=>{
setTimeout(function secondFunction(){
bot.postMessageToChannel(user_channel, `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
console.log( "허무개그 전송~~~~!")
}, 3000);
})
client.close();
})
};
//Function for giving out random joke after filtering only knock-knock type jokes
knockknockJoke= (user_channel)=>{
MongoClient.connect(url, function (err, client){
if (err) throw err;
var db = client.db('jokeapi');
json_max = 61;
random = getRandomInt(json_max);
result = db.collection('jokes').findOne({id: random});
user = result;
user.then(function(total){
if(total.type === "knock-knock"){
question = total.setup;
joke = total.punchline;
ques_and_joke = [question, joke];
return ques_and_joke;
}
else if(total.type != "knock-knock"){
client.close();
knockknockJoke(user_channel);
}
})
.then((joke_info)=>{
bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing'));
console.log("똑똑 질문 불려짐");
return joke_info;
})
.then((info)=>{
setTimeout(function secondFunction(){
bot.postMessageToChannel(user_channel, `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
console.log( "허무개그 전송~~~~!")
}, 3000);
})
client.close();
})
}
//Function for giving out information to user to control the bot
runHelp = () =>{
comment = "Thanks for using Joker bot!:ghost::ghost:laugh:\nBot info: type '@joker --help'\nBot functions: @joker tell me [something] "
bot.postMessageToChannel('everyone', "Type @joker and write a joke that you would like\n ex- @joker random",emoji.emojis('question'));
}
}