오원석

master merge ows

......@@ -6,6 +6,7 @@ const url = 'mongodb://13.124.65.242:27017/';
const url2 = 'mongodb://13.124.65.242:27017/userdb';
const fs=require('fs');
message_recieved = 0;
ary_size=0;
exports.startbot = ()=>{
// Get authorization to use the slackbot
......@@ -141,9 +142,8 @@ function MakeJoke(message,user_channel){
var temp=message.split(' ');
var user=temp[0].substring(2,temp[0].length-1);
console.log('유저 => '+user);
var path='./joke_data/user.json';
console.log(path);
fs.exists(path,function(exists){
if(exists){
console.log("yes file exists");
......@@ -155,17 +155,26 @@ function MakeJoke(message,user_channel){
obj=JSON.parse(data);
var length=obj.table.length;
obj.table.push({id : length+1, type : user, setup : msg[0], punchline : msg[1]});
var myobj = {id : length+1, type : user, setup : msg[0], punchline : msg[1]};
//var myobj = {id : length+1, type : user, setup : msg[0], punchline : msg[1]};
MongoClient.connect(url,function(err,db){
if(err) throw err;
var dbo = db.db("userdb");
dbo.collection("user").insertOne(myobj, function(err,res){
if(err) throw err;
console.log("1 insert!!");
db.close;
})
})
var array = dbo.collection('user').find({type : user}).toArray(function(err,docs){
if(err){
callback(err,null);
return ;
}
console.log(docs.length);
var myobj = {id : docs.length+1, type : user, setup : msg[0], punchline : msg[1]};
dbo.collection("user").insertOne(myobj, function(err,res){
if(err) throw err;
console.log("1 insert!!");
db.close();
})
});
})
var json=JSON.stringify(obj);
fs.writeFile(path,json,'utf8',function(err){
if(err){
......@@ -189,7 +198,7 @@ function MakeJoke(message,user_channel){
dbo.collection("user").insertOne(myobj, function(err,res){
if(err) throw err;
console.log("1 insert!!");
db.close;
db.close();
})
});
var json=JSON.stringify(obj);
......@@ -259,37 +268,68 @@ UserMakeJoke= (message,user_channel)=>{
if (err) throw err;
//go into database name jokeapi
var db = client.db('userdb');
var array = db.collection('user').find({type: user}).toArray(function(err,docs){
if(err){
callback(err,null);
return;
}
ary_size=docs.length;
console.log(docs.length);
var random=getRandomInt(1,ary_size+1);
console.log(random);
var result=docs[random-1];
user = result;
console.log(user);
result = db.collection('user').findOne({type: user});
console.log(result);
user = result;
//After finding one joke, use promise to run codes synchronously
user.then(function(total){
question = total.setup;
question = user.setup;
joke=user.punchline;
bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing'));
console.log("질문 불려짐");
return total;
})
.then((all)=>{
joke=all.punchline;
//Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
setTimeout(function secondFunction(){
setTimeout(function secondFunction(){
bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
console.log("허무개그 전송~~~~~~!");
},3000);
})
// //After finding one joke, use promise to run codes synchronously
// user.then(function(total){
// question = total.setup;
// bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing'));
// console.log("질문 불려짐");
// return total;
// })
// .then((all)=>{
// joke=all.punchline;
// //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
// setTimeout(function secondFunction(){
// bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
// console.log("허무개그 전송~~~~~~!");
// },3000);
// })
});
//var random=getRandomInt(1,ary_size+1);
// console.log(ary_size);
// var result=db.collection('user').findOne({type : user ,id : random});
// user = result;
// //After finding one joke, use promise to run codes synchronously
// user.then(function(total){
// question = total.setup;
// bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing'));
// console.log("질문 불려짐");
// return total;
// })
// .then((all)=>{
// joke=all.punchline;
// //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
// setTimeout(function secondFunction(){
// bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
// console.log("허무개그 전송~~~~~~!");
// },3000);
// })
//close mongodb
client.close();
})
var path='./joke_data/'+user+'.json';
var data=fs.readFileSync(path);
var jsondata=JSON.parse(data);
}
//Function for giving out random joke after filtering only general type jokes
......
const route = require('./Routers/route');
// var express=require('express');
// var app=express();
// //Before activating must connect to mongodb to interface
// //Connecting code [mongod --dbpath=data --bind_ip 127.0.0.1] at mongodb directory
// app.get('/slack/bot',function(req,res){
//Before activating must connect to mongodb to interface
//Connecting code [mongod --dbpath=data --bind_ip 127.0.0.1] at mongodb directory
// })
// app.listen(3000,function(){
// console.log("Connetect 3000 port!!");
// })
route.startbot();
......
{"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
{"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
......