오원석

master merge ows

...@@ -6,6 +6,7 @@ const url = 'mongodb://13.124.65.242:27017/'; ...@@ -6,6 +6,7 @@ const url = 'mongodb://13.124.65.242:27017/';
6 const url2 = 'mongodb://13.124.65.242:27017/userdb'; 6 const url2 = 'mongodb://13.124.65.242:27017/userdb';
7 const fs=require('fs'); 7 const fs=require('fs');
8 message_recieved = 0; 8 message_recieved = 0;
9 +ary_size=0;
9 10
10 exports.startbot = ()=>{ 11 exports.startbot = ()=>{
11 // Get authorization to use the slackbot 12 // Get authorization to use the slackbot
...@@ -141,9 +142,8 @@ function MakeJoke(message,user_channel){ ...@@ -141,9 +142,8 @@ function MakeJoke(message,user_channel){
141 var temp=message.split(' '); 142 var temp=message.split(' ');
142 var user=temp[0].substring(2,temp[0].length-1); 143 var user=temp[0].substring(2,temp[0].length-1);
143 console.log('유저 => '+user); 144 console.log('유저 => '+user);
144 - 145 +
145 var path='./joke_data/user.json'; 146 var path='./joke_data/user.json';
146 - console.log(path);
147 fs.exists(path,function(exists){ 147 fs.exists(path,function(exists){
148 if(exists){ 148 if(exists){
149 console.log("yes file exists"); 149 console.log("yes file exists");
...@@ -155,17 +155,26 @@ function MakeJoke(message,user_channel){ ...@@ -155,17 +155,26 @@ function MakeJoke(message,user_channel){
155 obj=JSON.parse(data); 155 obj=JSON.parse(data);
156 var length=obj.table.length; 156 var length=obj.table.length;
157 obj.table.push({id : length+1, type : user, setup : msg[0], punchline : msg[1]}); 157 obj.table.push({id : length+1, type : user, setup : msg[0], punchline : msg[1]});
158 - var myobj = {id : length+1, type : user, setup : msg[0], punchline : msg[1]}; 158 + //var myobj = {id : length+1, type : user, setup : msg[0], punchline : msg[1]};
159 MongoClient.connect(url,function(err,db){ 159 MongoClient.connect(url,function(err,db){
160 if(err) throw err; 160 if(err) throw err;
161 var dbo = db.db("userdb"); 161 var dbo = db.db("userdb");
162 - dbo.collection("user").insertOne(myobj, function(err,res){ 162 + var array = dbo.collection('user').find({type : user}).toArray(function(err,docs){
163 - if(err) throw err; 163 + if(err){
164 - console.log("1 insert!!"); 164 + callback(err,null);
165 - db.close; 165 + return ;
166 - }) 166 + }
167 - }) 167 +
168 + console.log(docs.length);
169 + var myobj = {id : docs.length+1, type : user, setup : msg[0], punchline : msg[1]};
170 + dbo.collection("user").insertOne(myobj, function(err,res){
171 + if(err) throw err;
172 + console.log("1 insert!!");
173 + db.close();
174 + })
175 + });
168 176
177 + })
169 var json=JSON.stringify(obj); 178 var json=JSON.stringify(obj);
170 fs.writeFile(path,json,'utf8',function(err){ 179 fs.writeFile(path,json,'utf8',function(err){
171 if(err){ 180 if(err){
...@@ -189,7 +198,7 @@ function MakeJoke(message,user_channel){ ...@@ -189,7 +198,7 @@ function MakeJoke(message,user_channel){
189 dbo.collection("user").insertOne(myobj, function(err,res){ 198 dbo.collection("user").insertOne(myobj, function(err,res){
190 if(err) throw err; 199 if(err) throw err;
191 console.log("1 insert!!"); 200 console.log("1 insert!!");
192 - db.close; 201 + db.close();
193 }) 202 })
194 }); 203 });
195 var json=JSON.stringify(obj); 204 var json=JSON.stringify(obj);
...@@ -259,37 +268,68 @@ UserMakeJoke= (message,user_channel)=>{ ...@@ -259,37 +268,68 @@ UserMakeJoke= (message,user_channel)=>{
259 if (err) throw err; 268 if (err) throw err;
260 //go into database name jokeapi 269 //go into database name jokeapi
261 var db = client.db('userdb'); 270 var db = client.db('userdb');
271 + var array = db.collection('user').find({type: user}).toArray(function(err,docs){
272 + if(err){
273 + callback(err,null);
274 + return;
275 + }
276 + ary_size=docs.length;
277 + console.log(docs.length);
278 + var random=getRandomInt(1,ary_size+1);
279 + console.log(random);
280 + var result=docs[random-1];
281 + user = result;
282 + console.log(user);
262 283
263 - result = db.collection('user').findOne({type: user}); 284 + question = user.setup;
264 - console.log(result); 285 + joke=user.punchline;
265 -
266 - user = result;
267 - //After finding one joke, use promise to run codes synchronously
268 - user.then(function(total){
269 - question = total.setup;
270 bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing')); 286 bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing'));
271 console.log("질문 불려짐"); 287 console.log("질문 불려짐");
272 - return total; 288 + setTimeout(function secondFunction(){
273 - })
274 - .then((all)=>{
275 - joke=all.punchline;
276 - //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
277 - setTimeout(function secondFunction(){
278 bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing')); 289 bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
279 console.log("허무개그 전송~~~~~~!"); 290 console.log("허무개그 전송~~~~~~!");
280 },3000); 291 },3000);
281 - }) 292 + // //After finding one joke, use promise to run codes synchronously
293 + // user.then(function(total){
294 + // question = total.setup;
295 + // bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing'));
296 + // console.log("질문 불려짐");
297 + // return total;
298 + // })
299 + // .then((all)=>{
300 + // joke=all.punchline;
301 + // //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
302 + // setTimeout(function secondFunction(){
303 + // bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
304 + // console.log("허무개그 전송~~~~~~!");
305 + // },3000);
306 + // })
307 + });
308 +
309 + //var random=getRandomInt(1,ary_size+1);
310 + // console.log(ary_size);
311 + // var result=db.collection('user').findOne({type : user ,id : random});
312 + // user = result;
313 + // //After finding one joke, use promise to run codes synchronously
314 + // user.then(function(total){
315 + // question = total.setup;
316 + // bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing'));
317 + // console.log("질문 불려짐");
318 + // return total;
319 + // })
320 + // .then((all)=>{
321 + // joke=all.punchline;
322 + // //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
323 + // setTimeout(function secondFunction(){
324 + // bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
325 + // console.log("허무개그 전송~~~~~~!");
326 + // },3000);
327 + // })
282 //close mongodb 328 //close mongodb
283 client.close(); 329 client.close();
284 }) 330 })
285 331
286 332
287 - var path='./joke_data/'+user+'.json';
288 -
289 - var data=fs.readFileSync(path);
290 - var jsondata=JSON.parse(data);
291 -
292 -
293 } 333 }
294 334
295 //Function for giving out random joke after filtering only general type jokes 335 //Function for giving out random joke after filtering only general type jokes
......
1 const route = require('./Routers/route'); 1 const route = require('./Routers/route');
2 +// var express=require('express');
3 +// var app=express();
4 +// //Before activating must connect to mongodb to interface
5 +// //Connecting code [mongod --dbpath=data --bind_ip 127.0.0.1] at mongodb directory
6 +// app.get('/slack/bot',function(req,res){
2 7
3 -//Before activating must connect to mongodb to interface 8 +// })
4 -//Connecting code [mongod --dbpath=data --bind_ip 127.0.0.1] at mongodb directory 9 +// app.listen(3000,function(){
10 +// console.log("Connetect 3000 port!!");
11 +// })
5 12
6 route.startbot(); 13 route.startbot();
......
1 -{"table":[{"id":1,"type":"UJZF5UTMX","setup":" ok","punchline":" okkk"},{"id":2,"type":"UJZF5UTMX","setup":" man","punchline":" woman"},{"id":3,"type":"UJZF5UTMX","setup":" kim ","punchline":" minuk"},{"id":4,"type":"UH9RRHSTG","setup":" 팔에 비빔면 뭍으면 빨리 닦아야됨 왜?","punchline":" 팔도비빔면됨"},{"id":5,"type":"UH9RRHSTG","setup":" 팔에 비빔소스 묻으면 빨리 닦아야함","punchline":" 팔도비빔면되서"},{"id":6,"type":"UH9RRHSTG","setup":" check","punchline":" working"}]}
...\ No newline at end of file ...\ No newline at end of file
1 +{"table":[{"id":1,"type":"UJZF5UTMX","setup":" ok","punchline":" okkk"},{"id":2,"type":"UJZF5UTMX","setup":" man","punchline":" woman"},{"id":3,"type":"UJZF5UTMX","setup":" kim ","punchline":" minuk"},{"id":4,"type":"UH9RRHSTG","setup":" 팔에 비빔면 뭍으면 빨리 닦아야됨 왜?","punchline":" 팔도비빔면됨"},{"id":5,"type":"UH9RRHSTG","setup":" 팔에 비빔소스 묻으면 빨리 닦아야함","punchline":" 팔도비빔면되서"},{"id":6,"type":"UH9RRHSTG","setup":" check","punchline":" working"},{"id":7,"type":"UJP8BFSNP","setup":" kim","punchline":" minuk"},{"id":8,"type":"UJP8BFSNP","setup":" kim","punchline":" yeonjun"},{"id":9,"type":"UJP8BFSNP","setup":" hi","punchline":" hello"},{"id":10,"type":"UJP8BFSNP","setup":" hello","punchline":" hi"},{"id":11,"type":"UJP8BFSNP","setup":" bye","punchline":" bi"},{"id":12,"type":"UJP8BFSNP","setup":" kkkkk","punchline":" jjjjj"},{"id":13,"type":"UJP8BFSNP","setup":" oh","punchline":"wonserk"},{"id":14,"type":"UJP8BFSNP","setup":" oh","punchline":" wonserk"},{"id":15,"type":"UJP8BFSNP","setup":" oh","punchline":" wonserk"},{"id":16,"type":"UJP8BFSNP","setup":" kim","punchline":" minuk"},{"id":17,"type":"UJP8BFSNP","setup":" kim","punchline":" yeonjun"},{"id":18,"type":"UJP8BFSNP","setup":" oh","punchline":" wonserk"},{"id":19,"type":"UJP8BFSNP","setup":" oh","punchline":" wonserk"},{"id":20,"type":"UJP8BFSNP","setup":" oh","punchline":" wonserk"},{"id":21,"type":"UJP8BFSNP","setup":" kim","punchline":" minuk"},{"id":22,"type":"UJP8BFSNP","setup":" kim","punchline":"yeonjun"},{"id":23,"type":"UJP8BFSNP","setup":" oh","punchline":" wonserk"},{"id":24,"type":"UJP8BFSNP","setup":" kim","punchline":" minuk"},{"id":25,"type":"UJP8BFSNP","setup":" kim","punchline":" yeonjun"},{"id":26,"type":"UJP8BFSNP","setup":" oh","punchline":" wonserk"},{"id":27,"type":"UJP8BFSNP","setup":" oh","punchline":" wonserk"},{"id":28,"type":"UJP8BFSNP","setup":" oh","punchline":" wonserk"},{"id":29,"type":"UJP8BFSNP","setup":" oh","punchline":" wonserk"},{"id":30,"type":"UJP8BFSNP","setup":" kim","punchline":" minuk"},{"id":31,"type":"UJP8BFSNP","setup":" kim","punchline":" yeonjun"},{"id":32,"type":"UJP8BFSNP","setup":" open","punchline":" source"}]}
...\ No newline at end of file ...\ No newline at end of file
......