김연준

Adding knockknock function and fixed wrong looping

...@@ -7,7 +7,7 @@ const url = 'mongodb://localhost:27017/'; ...@@ -7,7 +7,7 @@ const url = 'mongodb://localhost:27017/';
7 exports.startbot = ()=>{ 7 exports.startbot = ()=>{
8 // Get authorization to use the slackbot 8 // Get authorization to use the slackbot
9 const bot = new SlackBot({ 9 const bot = new SlackBot({
10 - token : "", 10 + token : "xoxb-582582124755-587875604934-86ISu22wmEaDhGWtQmpvumbR",
11 name : "Joker" 11 name : "Joker"
12 }); 12 });
13 13
...@@ -36,14 +36,15 @@ bot.on('message', (data) => { ...@@ -36,14 +36,15 @@ bot.on('message', (data) => {
36 // Responding to Data 36 // Responding to Data
37 function handleMessage(message){ 37 function handleMessage(message){
38 38
39 - if(message.includes(' yomama')){ 39 + if(message.includes(' knockknock')){
40 - yomamaJoke(); 40 + knockknockJoke();
41 } 41 }
42 else if(message.includes(' general')){ 42 else if(message.includes(' general')){
43 generalJoke(); 43 generalJoke();
44 } 44 }
45 45
46 else if(message.includes(' random')){ 46 else if(message.includes(' random')){
47 + console.log(message);
47 randomJoke(); 48 randomJoke();
48 } 49 }
49 50
...@@ -169,25 +170,71 @@ programmingJoke= ()=>{ ...@@ -169,25 +170,71 @@ programmingJoke= ()=>{
169 if(total.type === "programming"){ 170 if(total.type === "programming"){
170 question = total.setup; 171 question = total.setup;
171 joke = total.punchline; 172 joke = total.punchline;
173 + channel = 'everyone';
174 + const face = {
175 + icon_emoji: ':laughing:'
176 + };
177 + ques_and_joke = [question, joke, face, channel];
178 + return ques_and_joke;
172 } 179 }
173 else if(total.type != "programming"){ 180 else if(total.type != "programming"){
174 client.close(); 181 client.close();
175 programmingJoke(); 182 programmingJoke();
176 } 183 }
177 184
185 + // bot.postMessageToChannel('everyone', question, face);
186 + // bot.postMessageToChannel('full-stack-web', question, joke, face);
187 + // bot.postMessageToChannel('bot_test', question, face);
188 + // bot.postMessageToChannel('everyone', joke, face);
189 + // bot.postMessageToChannel('full-stack-web', joke, face);
190 + // bot.postMessageToChannel('bot_test', joke, face);
191 + })
192 + .then((joke_info)=>{
193 + function askQuestion(){
194 + bot.postMessageToChannel(joke_info[3], joke_info[0], joke_info[2]);
195 + console.log("프로그래밍 질문 불려짐");
196 + }
197 + askQuestion();
198 + return joke_info;
199 + })
200 + .then((info)=>{
201 + bot.postMessageToChannel(info[3], info[1], info[2]);
202 + return;
203 + })
204 + client.close();
205 + })
206 + }
207 +
208 +knockknockJoke= ()=>{
209 + MongoClient.connect(url, function (err, client){
210 + if (err) throw err;
211 + var db = client.db('jokeapi');
212 +
213 + json_max = 376;
214 + function getRandomInt() {
215 + min = Math.ceil(1);
216 + max = Math.floor(376);
217 + return Math.floor(Math.random() * (max - min + 1)) + min;
218 + }
219 + random = getRandomInt();
220 + result = db.collection('jokes').findOne({id: random});
221 + user = result;
222 + user.then(function(total){
223 + if(total.type === "knock-knock"){
224 + question = total.setup;
225 + joke = total.punchline;
226 + channel = 'everyone';
178 const face = { 227 const face = {
179 icon_emoji: ':laughing:' 228 icon_emoji: ':laughing:'
180 }; 229 };
181 - function firstFunction(channel){ 230 + ques_and_joke = [question, joke, face, channel];
182 - bot.postMessageToChannel(channel, joke, face); 231 + return ques_and_joke;
183 - console.log("프로그래밍 허무개그 전송~~~~!");
184 } 232 }
185 - 233 + else if(total.type != "knock-knock"){
186 - function secondFunction(channel, callback){ 234 + client.close();
187 - bot.postMessageToChannel(channel, question, face); 235 + programmingJoke();
188 - console.log("프로그래밍 질문 불려짐")
189 } 236 }
190 - secondFunction('everyone').then(firstFunction('everyone')); 237 +
191 // bot.postMessageToChannel('everyone', question, face); 238 // bot.postMessageToChannel('everyone', question, face);
192 // bot.postMessageToChannel('full-stack-web', question, joke, face); 239 // bot.postMessageToChannel('full-stack-web', question, joke, face);
193 // bot.postMessageToChannel('bot_test', question, face); 240 // bot.postMessageToChannel('bot_test', question, face);
...@@ -195,6 +242,18 @@ programmingJoke= ()=>{ ...@@ -195,6 +242,18 @@ programmingJoke= ()=>{
195 // bot.postMessageToChannel('full-stack-web', joke, face); 242 // bot.postMessageToChannel('full-stack-web', joke, face);
196 // bot.postMessageToChannel('bot_test', joke, face); 243 // bot.postMessageToChannel('bot_test', joke, face);
197 }) 244 })
245 + .then((joke_info)=>{
246 + function askQuestion(){
247 + bot.postMessageToChannel(joke_info[3], joke_info[0], joke_info[2]);
248 + console.log("프로그래밍 질문 불려짐");
249 + }
250 + askQuestion();
251 + return joke_info;
252 + })
253 + .then((info)=>{
254 + bot.postMessageToChannel(info[3], info[1], info[2]);
255 + return;
256 + })
198 client.close(); 257 client.close();
199 }) 258 })
200 } 259 }
......