오원석

make.joke.r

...@@ -10,8 +10,8 @@ message_recieved = 0; ...@@ -10,8 +10,8 @@ message_recieved = 0;
10 exports.startbot = ()=>{ 10 exports.startbot = ()=>{
11 // Get authorization to use the slackbot 11 // Get authorization to use the slackbot
12 const bot = new SlackBot({ 12 const bot = new SlackBot({
13 - token : "xoxb-582582124755-587875604934-gdWsVhzGXCze3mECWXpTsAP6", 13 + token : "xoxb-635297512738-635283536771-A03RoBh4Dv8qG1EiLudMpk1f",
14 - name : "joker" 14 + name : "jokebot"
15 }); 15 });
16 16
17 // Start the slackbot 17 // Start the slackbot
...@@ -141,9 +141,8 @@ function MakeJoke(message,user_channel){ ...@@ -141,9 +141,8 @@ function MakeJoke(message,user_channel){
141 var temp=message.split(' '); 141 var temp=message.split(' ');
142 var user=temp[0].substring(2,temp[0].length-1); 142 var user=temp[0].substring(2,temp[0].length-1);
143 console.log('유저 => '+user); 143 console.log('유저 => '+user);
144 - 144 +
145 var path='./joke_data/user.json'; 145 var path='./joke_data/user.json';
146 - console.log(path);
147 fs.exists(path,function(exists){ 146 fs.exists(path,function(exists){
148 if(exists){ 147 if(exists){
149 console.log("yes file exists"); 148 console.log("yes file exists");
...@@ -155,24 +154,33 @@ function MakeJoke(message,user_channel){ ...@@ -155,24 +154,33 @@ function MakeJoke(message,user_channel){
155 obj=JSON.parse(data); 154 obj=JSON.parse(data);
156 var length=obj.table.length; 155 var length=obj.table.length;
157 obj.table.push({id : length+1, type : user, setup : msg[0], punchline : msg[1]}); 156 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]}; 157 + //var myobj = {id : length+1, type : user, setup : msg[0], punchline : msg[1]};
159 MongoClient.connect(url,function(err,db){ 158 MongoClient.connect(url,function(err,db){
160 if(err) throw err; 159 if(err) throw err;
161 var dbo = db.db("userdb"); 160 var dbo = db.db("userdb");
162 - dbo.collection("user").insertOne(myobj, function(err,res){ 161 + var array = dbo.collection('user').find({type : user}).toArray(function(err,docs){
163 - if(err) throw err; 162 + if(err){
164 - console.log("1 insert!!"); 163 + callback(err,null);
165 - db.close; 164 + return ;
166 - }) 165 + }
167 - }) 166 +
167 + console.log(docs.length);
168 + var myobj = {id : docs.length+1, type : user, setup : msg[0], punchline : msg[1]};
169 + dbo.collection("user").insertOne(myobj, function(err,res){
170 + if(err) throw err;
171 + console.log("1 insert!!");
172 + db.close();
173 + })
174 + });
168 175
169 - var json=JSON.stringify(obj); 176 + })
170 - fs.writeFile(path,json,'utf8',function(err){ 177 + // var json=JSON.stringify(obj);
171 - if(err){ 178 + // fs.writeFile(path,json,'utf8',function(err){
172 - console.log(err); 179 + // if(err){
173 - } 180 + // console.log(err);
174 - console.log('완료'); 181 + // }
175 - }); 182 + // console.log('완료');
183 + // });
176 } 184 }
177 }); 185 });
178 comment="Sucess making joke!!:+1::thumbsup:\nWhen you want to show your joke, please enter @jokebot tell-me-userjoke"; 186 comment="Sucess making joke!!:+1::thumbsup:\nWhen you want to show your joke, please enter @jokebot tell-me-userjoke";
...@@ -189,7 +197,7 @@ function MakeJoke(message,user_channel){ ...@@ -189,7 +197,7 @@ function MakeJoke(message,user_channel){
189 dbo.collection("user").insertOne(myobj, function(err,res){ 197 dbo.collection("user").insertOne(myobj, function(err,res){
190 if(err) throw err; 198 if(err) throw err;
191 console.log("1 insert!!"); 199 console.log("1 insert!!");
192 - db.close; 200 + db.close();
193 }) 201 })
194 }); 202 });
195 var json=JSON.stringify(obj); 203 var json=JSON.stringify(obj);
...@@ -259,10 +267,19 @@ UserMakeJoke= (message,user_channel)=>{ ...@@ -259,10 +267,19 @@ UserMakeJoke= (message,user_channel)=>{
259 if (err) throw err; 267 if (err) throw err;
260 //go into database name jokeapi 268 //go into database name jokeapi
261 var db = client.db('userdb'); 269 var db = client.db('userdb');
270 + ary_size=0;
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 + });
262 279
263 - result = db.collection('user').findOne({type: user}); 280 + // var random=getRandomInt(1,ar+1);
264 - console.log(result); 281 + //console.log(array.);
265 - 282 + var result=db.collection('user').findOne({type : user ,id : random});
266 user = result; 283 user = result;
267 //After finding one joke, use promise to run codes synchronously 284 //After finding one joke, use promise to run codes synchronously
268 user.then(function(total){ 285 user.then(function(total){
...@@ -284,12 +301,6 @@ UserMakeJoke= (message,user_channel)=>{ ...@@ -284,12 +301,6 @@ UserMakeJoke= (message,user_channel)=>{
284 }) 301 })
285 302
286 303
287 - var path='./joke_data/'+user+'.json';
288 -
289 - var data=fs.readFileSync(path);
290 - var jsondata=JSON.parse(data);
291 -
292 -
293 } 304 }
294 305
295 //Function for giving out random joke after filtering only general type jokes 306 //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"}]}
...\ No newline at end of file ...\ No newline at end of file
......