오원석

make jokes

...@@ -3,6 +3,9 @@ const SlackBot = require('slackbots'); //link : https://github.com/mishk0/slack- ...@@ -3,6 +3,9 @@ const SlackBot = require('slackbots'); //link : https://github.com/mishk0/slack-
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 +const fs=require('fs');
7 +num=1;
8 +//const userjoke = require('./joke_data/user.json');
6 9
7 exports.startbot = ()=>{ 10 exports.startbot = ()=>{
8 // Get authorization to use the slackbot 11 // Get authorization to use the slackbot
...@@ -20,6 +23,7 @@ exports.startbot = ()=>{ ...@@ -20,6 +23,7 @@ exports.startbot = ()=>{
20 for(i=0; i< channel_length; ++i){ 23 for(i=0; i< channel_length; ++i){
21 //postMessageToChannel(name, message [, params, callback]) (return: promise) - posts a message to channel by name. 24 //postMessageToChannel(name, message [, params, callback]) (return: promise) - posts a message to channel by name.
22 bot.postMessageToChannel(data.channels[i].name, 'Have some fun with @Joker!\nFor commands write @joker --help' 25 bot.postMessageToChannel(data.channels[i].name, 'Have some fun with @Joker!\nFor commands write @joker --help'
26 +
23 , emoji.emojis('bowtie')); 27 , emoji.emojis('bowtie'));
24 } 28 }
25 return data; 29 return data;
...@@ -32,10 +36,11 @@ exports.startbot = ()=>{ ...@@ -32,10 +36,11 @@ exports.startbot = ()=>{
32 bot.on('error', (err) => console.log(err)); 36 bot.on('error', (err) => console.log(err));
33 37
34 //Message Handler 38 //Message Handler
35 -bot.on('message', (data) => { 39 + bot.on('message', (data) => {
36 if(data.type !== 'message'){ 40 if(data.type !== 'message'){
37 return; 41 return;
38 } 42 }
43 +
39 status = data; 44 status = data;
40 message_recieved = 0; 45 message_recieved = 0;
41 46
...@@ -96,7 +101,7 @@ function handleMessage(message, current_channel){ ...@@ -96,7 +101,7 @@ function handleMessage(message, current_channel){
96 else if(message.includes(' programming')){ 101 else if(message.includes(' programming')){
97 programmingJoke(current_channel); 102 programmingJoke(current_channel);
98 } 103 }
99 - else if(message.includes(' reddit')){ 104 + else if(message.includes(' reddit')){;
100 redditJoke(current_channel); 105 redditJoke(current_channel);
101 } 106 }
102 else if(message.includes(' funny story')){ 107 else if(message.includes(' funny story')){
...@@ -106,6 +111,9 @@ function handleMessage(message, current_channel){ ...@@ -106,6 +111,9 @@ function handleMessage(message, current_channel){
106 comment = "Please use @joker --help to know what I can do!:smile::smile::smile:\n You can write type of joke[knock-knock, general, programming, funny story, reddit]"; 111 comment = "Please use @joker --help to know what I can do!:smile::smile::smile:\n You can write type of joke[knock-knock, general, programming, funny story, reddit]";
107 bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth')); 112 bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth'));
108 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed')); 113 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
114 + }
115 + else if(message.includes(' userjoke')){
116 + UserMakeJoke(current_channel);
109 } 117 }
110 } 118 }
111 else if(message.includes(' help')){ 119 else if(message.includes(' help')){
...@@ -117,28 +125,76 @@ function handleMessage(message, current_channel){ ...@@ -117,28 +125,76 @@ function handleMessage(message, current_channel){
117 bot.postMessageToChannel(current_channel, `I have ${jokeTypes[0]}, ${jokeTypes[1]}, ${jokeTypes[2]},${jokeTypes[3]},${jokeTypes[4]} jokes!! :thumbsup: :thumbsup:`, emoji.emojis('thumbsup')); 125 bot.postMessageToChannel(current_channel, `I have ${jokeTypes[0]}, ${jokeTypes[1]}, ${jokeTypes[2]},${jokeTypes[3]},${jokeTypes[4]} jokes!! :thumbsup: :thumbsup:`, emoji.emojis('thumbsup'));
118 return; 126 return;
119 } 127 }
120 - //else if(message.inculdes(' write')) 128 + else if(message.includes(' make joke : '))
121 - //{ 129 + {
122 - // MakeJoke(message); 130 + console.log('123');
123 - //} 131 + MakeJoke(message);
132 + }
133 + // else{
134 + // console.log('456');
135 + // UserMakeJoke(current_channel);
136 +
137 + // }
124 /*else{ 138 /*else{
125 comment = "Sorry I'm not smart enough to understand this.....\nPlease use @joker help to know what I can do!"; 139 comment = "Sorry I'm not smart enough to understand this.....\nPlease use @joker help to know what I can do!";
126 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed')); 140 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
127 }*/ 141 }*/
128 } 142 }
143 +
144 +
129 function MakeJoke(message){ 145 function MakeJoke(message){
130 - fs.writeFile('../joke_data//jokes.json',message,function(err){ 146 + var obj={
131 - if(err) { 147 + table:[]
132 - console.log('Error' + err); 148 + }
149 + var input=message.split(':');
150 + var msg=input[1].split(',');
151 + // obj.table.push({id : msg[1], type : msg[2], setup : msg[3], punchling : msg[4]});
152 + // var json=JSON.stringify(obj);
153 + fs.exists('./joke_data/user.json',function(exists){
154 + if(exists){
155 + console.log("yes file exists");
156 + fs.readFile('./joke_data/user.json',function readFileCallback(err,data){
157 + if(err){
158 + console.log(err);
159 + }
160 + else{
161 + obj=JSON.parse(data);
162 + obj.table.push({id : num, type : 'userjoke', setup : msg[0], punchling : msg[1]});
163 + var json=JSON.stringify(obj);
164 + num++;
165 + fs.writeFile('./joke_data/user.json',json,'utf8',function(err){
166 + if(err){
167 + console.log(err);
168 + }
169 + console.log('완료');
170 + });
171 + }
172 + });
173 + }
174 + else{
175 + console.log("file not exists");
176 + obj.table.push({id : num, type : 'userjoke', setup : msg[0], punchling : msg[1]});
177 + var json=JSON.stringify(obj);
178 + num++;
179 + fs.writeFile('./joke_data/user.json',json,'utf8',function(err){
180 + if(err){
181 + console.log(err);
182 + }
183 + console.log('완료');
184 + });
133 } 185 }
134 - console.log('완료');
135 }) 186 })
187 +
136 } 188 }
189 +
190 +
137 //Gets a random integer 191 //Gets a random integer
138 function getRandomInt(start,end) { 192 function getRandomInt(start,end) {
139 return Math.floor((Math.random() * 100)%(end-start))+start; 193 return Math.floor((Math.random() * 100)%(end-start))+start;
140 } 194 }
141 195
196 +
197 +
142 //Function for giving out random joke 198 //Function for giving out random joke
143 randomJoke= (user_channel)=>{ 199 randomJoke= (user_channel)=>{
144 //Connect to mongodb client 200 //Connect to mongodb client
...@@ -176,6 +232,11 @@ randomJoke= (user_channel)=>{ ...@@ -176,6 +232,11 @@ randomJoke= (user_channel)=>{
176 }) 232 })
177 } 233 }
178 234
235 +// Function for giving out users making joke
236 +UserMakeJoke= (users_channel)=>{
237 +
238 +}
239 +
179 //Function for giving out random joke after filtering only general type jokes 240 //Function for giving out random joke after filtering only general type jokes
180 generalJoke= (user_channel)=>{ 241 generalJoke= (user_channel)=>{
181 MongoClient.connect(url, function (err, client){ 242 MongoClient.connect(url, function (err, client){
......
1 +{"table":[{"id":1,"type":"users","setup":"hello","punchling":" hi"},{"id":2,"type":"users","setup":"hi","punchling":"hello"},{"id":3,"type":"userjoke","setup":" what 6 x 3?","punchling":" 63 buliding!"}]}
...\ No newline at end of file ...\ No newline at end of file