오원석

merge ows

...@@ -3,8 +3,8 @@ const SlackBot = require('slackbots'); //link : https://github.com/mishk0/slack- ...@@ -3,8 +3,8 @@ const SlackBot = require('slackbots'); //link : https://github.com/mishk0/slack-
3 const dbname = 'jokeapi'; 3 const dbname = 'jokeapi';
4 const emoji = require('../slack_emoji'); 4 const emoji = require('../slack_emoji');
5 const url = 'mongodb://localhost:27017/'; 5 const url = 'mongodb://localhost:27017/';
6 -const fs = require('fs'); 6 +const fs=require('fs');
7 -//const userjoke = require('./joke_data/user.json'); 7 +message_recieved = 0;
8 8
9 exports.startbot = ()=>{ 9 exports.startbot = ()=>{
10 // Get authorization to use the slackbot 10 // Get authorization to use the slackbot
...@@ -21,9 +21,8 @@ exports.startbot = ()=>{ ...@@ -21,9 +21,8 @@ exports.startbot = ()=>{
21 channel_length = data.channels.length; 21 channel_length = data.channels.length;
22 for(i=0; i< channel_length; ++i){ 22 for(i=0; i< channel_length; ++i){
23 //postMessageToChannel(name, message [, params, callback]) (return: promise) - posts a message to channel by name. 23 //postMessageToChannel(name, message [, params, callback]) (return: promise) - posts a message to channel by name.
24 - bot.postMessageToChannel(data.channels[i].name, 'Have some fun with @Joker!\nFor commands write @joker -help' 24 + bot.postMessageToChannel(data.channels[i].name, 'Have some fun with @Joker!\nFor commands write @joker --help',
25 - 25 + emoji.emojis('bowtie'));
26 - , emoji.emojis('bowtie'));
27 } 26 }
28 return data; 27 return data;
29 }) 28 })
...@@ -41,7 +40,7 @@ exports.startbot = ()=>{ ...@@ -41,7 +40,7 @@ exports.startbot = ()=>{
41 } 40 }
42 41
43 status = data; 42 status = data;
44 - message_recieved = 0; 43 +
45 44
46 //Figure out which channel the user is sending message 45 //Figure out which channel the user is sending message
47 //If it's first input from the user, go through this loop to store the data of channel names and ids 46 //If it's first input from the user, go through this loop to store the data of channel names and ids
...@@ -63,9 +62,10 @@ exports.startbot = ()=>{ ...@@ -63,9 +62,10 @@ exports.startbot = ()=>{
63 ++message_recieved; 62 ++message_recieved;
64 console.log("User Channel list: " + channel_names) 63 console.log("User Channel list: " + channel_names)
65 for(i=0; i< channel_length; ++i){ 64 for(i=0; i< channel_length; ++i){
66 - if(channel_ids[i] == status.channel) 65 + if(channel_ids[i] == status.channel){
67 handleMessage(status.text, channel_names[i]); 66 handleMessage(status.text, channel_names[i]);
68 } 67 }
68 + }
69 }) 69 })
70 } 70 }
71 console.log(status); 71 console.log(status);
...@@ -126,18 +126,8 @@ function handleMessage(message, current_channel){ ...@@ -126,18 +126,8 @@ function handleMessage(message, current_channel){
126 } 126 }
127 else if(message.includes(' make joke : ')) 127 else if(message.includes(' make joke : '))
128 { 128 {
129 - console.log('123');
130 MakeJoke(message); 129 MakeJoke(message);
131 } 130 }
132 - // else{
133 - // console.log('456');
134 - // UserMakeJoke(current_channel);
135 -
136 - // }
137 - /*else{
138 - comment = "Sorry I'm not smart enough to understand this.....\nPlease use @joker help to know what I can do!";
139 - bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
140 - }*/
141 } 131 }
142 132
143 133
...@@ -201,8 +191,7 @@ randomJoke= (user_channel)=>{ ...@@ -201,8 +191,7 @@ randomJoke= (user_channel)=>{
201 var db = client.db('jokeapi'); 191 var db = client.db('jokeapi');
202 192
203 //set the maximum number of api formats and get a random integer 193 //set the maximum number of api formats and get a random integer
204 - json_max = 376; 194 + random = getRandomInt(1,377);
205 - random = getRandomInt(1,json_max);
206 //Go to jokes collection inside jokeapi database and find one joke randomly by putting a random number 195 //Go to jokes collection inside jokeapi database and find one joke randomly by putting a random number
207 result = db.collection('jokes').findOne({id: random}); 196 result = db.collection('jokes').findOne({id: random});
208 197
...@@ -210,19 +199,11 @@ randomJoke= (user_channel)=>{ ...@@ -210,19 +199,11 @@ randomJoke= (user_channel)=>{
210 //After finding one joke, use promise to run codes synchronously 199 //After finding one joke, use promise to run codes synchronously
211 user.then(function(total){ 200 user.then(function(total){
212 question = total.setup; 201 question = total.setup;
202 + joke=total.punchline;
203 + ques_and_joke=question+'\n'+joke+':stuck_out_tongue_winking_eye::laughing:';
213 //Ask the question first by extracting 'setup' section from api format 204 //Ask the question first by extracting 'setup' section from api format
214 - bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing')); 205 + bot.postMessageToChannel(user_channel, ques_and_joke, emoji.emojis('laughing'));
215 - console.log('질문 불려짐'); 206 + console.log('Random joke~~~');
216 - return total;
217 - })
218 - .then((all)=>{
219 - joke = all.punchline;
220 - //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
221 - setTimeout(function secondFunction(){
222 - bot.postMessageToChannel(user_channel, `${joke}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
223 - console.log( "허무개그 전송~~~~!")
224 - }, 3000);
225 -
226 }) 207 })
227 //close mongodb 208 //close mongodb
228 client.close(); 209 client.close();
...@@ -239,11 +220,11 @@ UserMakeJoke= (user_channel)=>{ ...@@ -239,11 +220,11 @@ UserMakeJoke= (user_channel)=>{
239 for(var i=0;i<jsondata.table.length;i++){ 220 for(var i=0;i<jsondata.table.length;i++){
240 if(jsondata.table[i].id==random){ 221 if(jsondata.table[i].id==random){
241 user=jsondata.table[i]; 222 user=jsondata.table[i];
242 - console.log(user);
243 question=user.setup; 223 question=user.setup;
244 joke=user.punchline; 224 joke=user.punchline;
245 result=question+'\n'+joke; 225 result=question+'\n'+joke;
246 bot.postMessageToChannel(user_channel,result, emoji.emojis('laughing')); 226 bot.postMessageToChannel(user_channel,result, emoji.emojis('laughing'));
227 + console.log("User joke~~~");
247 break; 228 break;
248 } 229 }
249 } 230 }
...@@ -262,19 +243,9 @@ generalJoke= (user_channel)=>{ ...@@ -262,19 +243,9 @@ generalJoke= (user_channel)=>{
262 user.then(function(total){ 243 user.then(function(total){
263 question = total.setup; 244 question = total.setup;
264 joke = total.punchline; 245 joke = total.punchline;
265 - ques_and_joke = [question, joke]; 246 + ques_and_joke=question+'\n'+joke+':stuck_out_tongue_winking_eye::laughing:';
266 - return ques_and_joke; 247 + bot.postMessageToChannel(user_channel,ques_and_joke,emoji.emojis('laughing'));
267 - }) 248 + console.log("General Joke~~~~");
268 - .then((joke_info)=>{
269 - bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing'));
270 - console.log("일반 질문 불려짐");
271 - return joke_info;
272 - })
273 - .then((info)=>{
274 - setTimeout(function secondFunction(){
275 - bot.postMessageToChannel(user_channel, `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
276 - console.log( "허무개그 전송~~~~!")
277 - }, 3000);
278 }) 249 })
279 client.close(); 250 client.close();
280 }) 251 })
...@@ -292,19 +263,9 @@ programmingJoke= (user_channel)=>{ ...@@ -292,19 +263,9 @@ programmingJoke= (user_channel)=>{
292 user.then(function(total){ 263 user.then(function(total){
293 question = total.setup; 264 question = total.setup;
294 joke = total.punchline; 265 joke = total.punchline;
295 - ques_and_joke = [question, joke]; 266 + ques_and_joke = question+'\n'+joke+':stuck_out_tongue_winking_eye::laughing:';
296 - return ques_and_joke; 267 + bot.postMessageToChannel(user_channel,ques_and_joke,emoji.emojis('laughing'));
297 - }) 268 + console.log("Programming Joke~~~");
298 - .then((joke_info)=>{
299 - bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing'));
300 - console.log("프로그래밍 질문 불려짐");
301 - return joke_info;
302 - })
303 - .then((info)=>{
304 - setTimeout(function secondFunction(){
305 - bot.postMessageToChannel(user_channel, `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
306 - console.log( "허무개그 전송~~~~!")
307 - }, 3000);
308 }) 269 })
309 client.close(); 270 client.close();
310 }) 271 })
...@@ -319,21 +280,16 @@ Funnystory= (user_channel)=>{ ...@@ -319,21 +280,16 @@ Funnystory= (user_channel)=>{
319 var db = client.db('FunnyStoryapi'); 280 var db = client.db('FunnyStoryapi');
320 281
321 282
322 - random = getRandomInt(1,200); 283 + random = getRandomInt(1,201);
323 result = db.collection('FunnyStory').findOne({id: random}); 284 result = db.collection('FunnyStory').findOne({id: random});
324 user = result; 285 user = result;
325 //if the random picked api type is not general execute the function from the start to get another format for general type 286 //if the random picked api type is not general execute the function from the start to get another format for general type
326 user.then(function(total){ 287 user.then(function(total){
327 category = total.category 288 category = total.category
289 + story=total.body;
290 + category_story=category+'\n'+story+':stuck_out_tongue_winking_eye::laughing:';
328 bot.postMessageToChannel(user_channel, category, emoji.emojis('smiliey')); 291 bot.postMessageToChannel(user_channel, category, emoji.emojis('smiliey'));
329 - console.log("이야기 카테고리") 292 + console.log("Funny story~~~");
330 - return total;
331 - })
332 - .then((all)=>{
333 - story = all.body;
334 - bot.postMessageToChannel(user_channel, `${story}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
335 - console.log("이야기 시작!");
336 -
337 }) 293 })
338 client.close(); 294 client.close();
339 }) 295 })
...@@ -346,22 +302,16 @@ redditJoke= (user_channel)=>{ ...@@ -346,22 +302,16 @@ redditJoke= (user_channel)=>{
346 if (err) throw err; 302 if (err) throw err;
347 var db = client.db('redditjoke'); 303 var db = client.db('redditjoke');
348 304
349 - json_max = 70; 305 + random = getRandomInt(1,71);
350 - random = getRandomInt(1,json_max);
351 result = db.collection('reddit').findOne({id: random}); 306 result = db.collection('reddit').findOne({id: random});
352 user = result; 307 user = result;
353 //if the random picked api type is not general execute the function from the start to get another format for general type 308 //if the random picked api type is not general execute the function from the start to get another format for general type
354 user.then(function(total){ 309 user.then(function(total){
355 title = total.title; 310 title = total.title;
356 - bot.postMessageToChannel(user_channel, title, emoji.emojis('smiliey')); 311 + joke=total.body;
357 - console.log("문답형 JOKE") 312 + title_joke=title+'\n'+joke+':stuck_out_tongue_winking_eye::laughing:';
358 - return total; 313 + bot.postMessageToChannel(user_channel, title_joke, emoji.emojis('smiliey'));
359 - }) 314 + console.log("reddit Joke~~~");
360 - .then((all)=>{
361 - joke = all.body;
362 - bot.postMessageToChannel(user_channel, `${joke}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
363 - console.log("정답은~~");
364 -
365 }) 315 })
366 client.close(); 316 client.close();
367 }) 317 })
...@@ -378,20 +328,9 @@ knockknockJoke= (user_channel)=>{ ...@@ -378,20 +328,9 @@ knockknockJoke= (user_channel)=>{
378 user.then(function(total){ 328 user.then(function(total){
379 question = total.setup; 329 question = total.setup;
380 joke = total.punchline; 330 joke = total.punchline;
381 - ques_and_joke = [question, joke]; 331 + ques_and_joke = question+'\n'+joke+':stuck_out_tongue_winking_eye::laughing:';
382 - return ques_and_joke; 332 + bot.postMessageToChannel(user_channel, ques_and_joke, emoji.emojis('laughing'));
383 - }) 333 + console.log("Knock-Knock joke~~~");
384 - .then((joke_info)=>{
385 - bot.postMessageToChannel(user_channel, joke_info[0], emoji.emojis('laughing'));
386 - console.log("똑똑 질문 불려짐");
387 -
388 - return joke_info;
389 - })
390 - .then((info)=>{
391 - setTimeout(function secondFunction(){
392 - bot.postMessageToChannel(user_channel, `${info[1]}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
393 - console.log( "허무개그 전송~~~~!")
394 - }, 3000);
395 }) 334 })
396 client.close(); 335 client.close();
397 }) 336 })
...@@ -400,8 +339,8 @@ knockknockJoke= (user_channel)=>{ ...@@ -400,8 +339,8 @@ knockknockJoke= (user_channel)=>{
400 //Function for giving out information to user to control the bot 339 //Function for giving out information to user to control the bot
401 runHelp = (user_channel) =>{ 340 runHelp = (user_channel) =>{
402 341
403 - comment = "Thanks for using Joker bot!:ghost::ghost:laugh:\nBot info: type '@joker -help' for infos\nBot functions: '@joker tell me [something] joke' will send related jokes, if I don't have what you mentioned, I will tell you I don't have that joke:smile:\n" 342 + comment = "Thanks for using Joker bot!:ghost::ghost:laugh:\nBot info: type '@joker help' for infos\nBot functions: '@joker tell-me [something] joke' will send related jokes, if I don't have what you mentioned, I will tell you I don't have that joke:smile:\n"
404 - current_jokes = "Joke types I have: general , knock-knock , programming , reddit, funny story" 343 + current_jokes = "Joke types I have: 1)general , 2)knock-knock , 3)programming , 4)reddit, 5)funny story, 6)make joke 7)userjoke"
405 bot.postMessageToChannel(user_channel, comment + current_jokes ,emoji.emojis('question')); 344 bot.postMessageToChannel(user_channel, comment + current_jokes ,emoji.emojis('question'));
406 } 345 }
407 } 346 }
......