Showing
3 changed files
with
16 additions
and
19 deletions
1 | const MongoClient = require('mongodb').MongoClient; | 1 | const MongoClient = require('mongodb').MongoClient; |
2 | -const SlackBot = require('slackbots'); | 2 | +const SlackBot = require('slackbots'); //link : https://github.com/mishk0/slack-bot-api |
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 | 6 | ||
7 | + | ||
7 | exports.startbot = ()=>{ | 8 | exports.startbot = ()=>{ |
8 | // Get authorization to use the slackbot | 9 | // Get authorization to use the slackbot |
9 | const bot = new SlackBot({ | 10 | const bot = new SlackBot({ |
10 | - token : "xoxb-582582124755-587875604934-t3xDZmtuLXVjgUMWDzAf8g1K", | 11 | + token : "xoxb-582582124755-587875604934-PR0QcCuQpOXXggtUN53ytfhH", |
11 | name : "Joker" | 12 | name : "Joker" |
12 | }); | 13 | }); |
13 | 14 | ||
... | @@ -18,6 +19,7 @@ exports.startbot = ()=>{ | ... | @@ -18,6 +19,7 @@ exports.startbot = ()=>{ |
18 | channel.then((data)=>{ | 19 | channel.then((data)=>{ |
19 | channel_length = data.channels.length; | 20 | channel_length = data.channels.length; |
20 | for(i=0; i< channel_length; ++i){ | 21 | for(i=0; i< channel_length; ++i){ |
22 | + //postMessageToChannel(name, message [, params, callback]) (return: promise) - posts a message to channel by name. | ||
21 | bot.postMessageToChannel(data.channels[i].name, 'Have some fun with @Joker!\nFor commands write @joker --help' | 23 | bot.postMessageToChannel(data.channels[i].name, 'Have some fun with @Joker!\nFor commands write @joker --help' |
22 | , emoji.emojis('bowtie')); | 24 | , emoji.emojis('bowtie')); |
23 | } | 25 | } |
... | @@ -81,7 +83,7 @@ function handleMessage(message, current_channel){ | ... | @@ -81,7 +83,7 @@ function handleMessage(message, current_channel){ |
81 | console.log(message); | 83 | console.log(message); |
82 | 84 | ||
83 | //Handles message response depending on the user message | 85 | //Handles message response depending on the user message |
84 | - if(message.includes(' tell me')){ | 86 | + if(message.includes(' tell me') || message.includes(' Tell me')){ |
85 | if(message.includes(' knock')){ | 87 | if(message.includes(' knock')){ |
86 | knockknockJoke(current_channel); | 88 | knockknockJoke(current_channel); |
87 | } | 89 | } |
... | @@ -89,10 +91,7 @@ function handleMessage(message, current_channel){ | ... | @@ -89,10 +91,7 @@ function handleMessage(message, current_channel){ |
89 | generalJoke(current_channel); | 91 | generalJoke(current_channel); |
90 | } | 92 | } |
91 | 93 | ||
92 | - else if(message.includes(' random')){ | 94 | + else if(message.includes(' random') || message.includes(' some joke') || message.includes(' funny joke')){ |
93 | - randomJoke(current_channel); | ||
94 | - } | ||
95 | - else if(message.includes(' a joke')){ | ||
96 | randomJoke(current_channel); | 95 | randomJoke(current_channel); |
97 | } | 96 | } |
98 | 97 | ||
... | @@ -139,35 +138,37 @@ function getRandomInt(max_num) { | ... | @@ -139,35 +138,37 @@ function getRandomInt(max_num) { |
139 | 138 | ||
140 | //Function for giving out random joke | 139 | //Function for giving out random joke |
141 | randomJoke= (user_channel)=>{ | 140 | randomJoke= (user_channel)=>{ |
141 | + //Connect to mongodb client | ||
142 | MongoClient.connect('mongodb://localhost:27017', function (err, client){ | 142 | MongoClient.connect('mongodb://localhost:27017', function (err, client){ |
143 | if (err) throw err; | 143 | if (err) throw err; |
144 | + //go into database name jokeapi | ||
144 | var db = client.db('jokeapi'); | 145 | var db = client.db('jokeapi'); |
145 | 146 | ||
147 | + //set the maximum number of api formats and get a random integer | ||
146 | json_max = 376; | 148 | json_max = 376; |
147 | random = getRandomInt(json_max); | 149 | random = getRandomInt(json_max); |
150 | + //Go to jokes collection inside jokeapi database and find one joke randomly by putting a random number | ||
148 | result = db.collection('jokes').findOne({id: random}); | 151 | result = db.collection('jokes').findOne({id: random}); |
149 | 152 | ||
150 | user = result; | 153 | user = result; |
154 | + //After finding one joke, use promise to run codes synchronously | ||
151 | user.then(function(total){ | 155 | user.then(function(total){ |
152 | question = total.setup; | 156 | question = total.setup; |
153 | - | 157 | + //Ask the question first by extracting 'setup' section from api format |
154 | - function firstFunction(){ | ||
155 | bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing')); | 158 | bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing')); |
156 | - } | ||
157 | - firstFunction('everyone'); | ||
158 | console.log('질문 불려짐'); | 159 | console.log('질문 불려짐'); |
159 | return total; | 160 | return total; |
160 | - | ||
161 | - | ||
162 | }) | 161 | }) |
163 | .then((all)=>{ | 162 | .then((all)=>{ |
164 | joke = all.punchline; | 163 | joke = all.punchline; |
164 | + //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke | ||
165 | setTimeout(function secondFunction(){ | 165 | setTimeout(function secondFunction(){ |
166 | bot.postMessageToChannel(user_channel, `${joke}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing')) | 166 | bot.postMessageToChannel(user_channel, `${joke}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing')) |
167 | console.log( "허무개그 전송~~~~!") | 167 | console.log( "허무개그 전송~~~~!") |
168 | }, 3000); | 168 | }, 3000); |
169 | 169 | ||
170 | }) | 170 | }) |
171 | + //close mongodb | ||
171 | client.close(); | 172 | client.close(); |
172 | }) | 173 | }) |
173 | } | 174 | } |
... | @@ -182,6 +183,7 @@ generalJoke= (user_channel)=>{ | ... | @@ -182,6 +183,7 @@ generalJoke= (user_channel)=>{ |
182 | random = getRandomInt(json_max); | 183 | random = getRandomInt(json_max); |
183 | result = db.collection('jokes').findOne({id: random}); | 184 | result = db.collection('jokes').findOne({id: random}); |
184 | user = result; | 185 | user = result; |
186 | + //if the random picked api type is not general execute the function from the start to get another format for general type | ||
185 | user.then(function(total){ | 187 | user.then(function(total){ |
186 | if(total.type === "general"){ | 188 | if(total.type === "general"){ |
187 | question = total.setup; | 189 | question = total.setup; | ... | ... |
1 | -const SlackBot = require('slackbots'); | ||
2 | -const axios = require('axios'); | ||
3 | const route = require('./Routers/route'); | 1 | const route = require('./Routers/route'); |
4 | -const MongoClient = require('mongodb').MongoClient; | ||
5 | -const url = 'mongodb://localhost:27017/'; | ||
6 | - | ||
7 | 2 | ||
8 | //Before activating must connect to mongodb to interface | 3 | //Before activating must connect to mongodb to interface |
9 | //Connecting code [mongod --dbpath=data --bind_ip 127.0.0.1] at mongodb directory | 4 | //Connecting code [mongod --dbpath=data --bind_ip 127.0.0.1] at mongodb directory | ... | ... |
-
Please register or login to post a comment