Showing
7 changed files
with
199 additions
and
28 deletions
1 | +<<<<<<< HEAD | ||
1 | <h1>Building a Slackbot</h1><br> | 2 | <h1>Building a Slackbot</h1><br> |
2 | <h2>A slackbot that reacts to user's chat and sends jokes </h2><br><br> | 3 | <h2>A slackbot that reacts to user's chat and sends jokes </h2><br><br> |
3 | 4 | ||
4 | <h3>Currently operating on aws EC2 / managing it to run on docker container combining mongodb and the code to interact on 'aws docker version' branch</h3> | 5 | <h3>Currently operating on aws EC2 / managing it to run on docker container combining mongodb and the code to interact on 'aws docker version' branch</h3> |
6 | +======= | ||
7 | +<h1>Joker</h1><br> | ||
8 | +<h2>A slackbot that reacts to user's chat and sends jokes </h2><br> | ||
9 | +>>>>>>> master | ||
10 | + | ||
11 | +<h3>Currently operating on aws EC2 / managing it to run on docker container combining mongodb and the code to interact on 'aws docker version' branch</h3><br><br> | ||
12 | +<b>Collected Jokes api link: https://github.com/15Dkatz/official_joke_api<br> | ||
13 | +Opensource for slackbot module link : https://github.com/mishk0/slack-bot-api<br> | ||
14 | +Used Database: Mongodb</b><br> | ||
15 | + | ||
16 | + | ||
17 | + | ||
5 | 18 | ||
6 | - | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -4,13 +4,17 @@ const dbname = 'jokeapi'; | ... | @@ -4,13 +4,17 @@ 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 | 6 | ||
7 | - | ||
8 | exports.startbot = ()=>{ | 7 | exports.startbot = ()=>{ |
9 | // Get authorization to use the slackbot | 8 | // Get authorization to use the slackbot |
10 | const bot = new SlackBot({ | 9 | const bot = new SlackBot({ |
10 | +<<<<<<< HEAD | ||
11 | //xoxb-582582124755-587875604934-gdWsVhzGXCze3mECWXpTsAP6 | 11 | //xoxb-582582124755-587875604934-gdWsVhzGXCze3mECWXpTsAP6 |
12 | token : "xoxb-582582124755-587875604934-gdWsVhzGXCze3mECWXpTsAP6", | 12 | token : "xoxb-582582124755-587875604934-gdWsVhzGXCze3mECWXpTsAP6", |
13 | name : "Joker" | 13 | name : "Joker" |
14 | +======= | ||
15 | + token : "xoxb-651692943605-645515979745-sfNJBLVbIIb86Bz1gfFc7oT2", | ||
16 | + name : "joker" | ||
17 | +>>>>>>> master | ||
14 | }); | 18 | }); |
15 | 19 | ||
16 | // Start the slackbot | 20 | // Start the slackbot |
... | @@ -82,7 +86,6 @@ bot.on('message', (data) => { | ... | @@ -82,7 +86,6 @@ bot.on('message', (data) => { |
82 | // Responding to Data | 86 | // Responding to Data |
83 | function handleMessage(message, current_channel){ | 87 | function handleMessage(message, current_channel){ |
84 | console.log(message); | 88 | console.log(message); |
85 | - | ||
86 | //Handles message response depending on the user message | 89 | //Handles message response depending on the user message |
87 | if(message.includes(' tell me') || message.includes(' Tell me')){ | 90 | if(message.includes(' tell me') || message.includes(' Tell me')){ |
88 | if(message.includes(' knock')){ | 91 | if(message.includes(' knock')){ |
... | @@ -99,37 +102,44 @@ function handleMessage(message, current_channel){ | ... | @@ -99,37 +102,44 @@ function handleMessage(message, current_channel){ |
99 | else if(message.includes(' programming')){ | 102 | else if(message.includes(' programming')){ |
100 | programmingJoke(current_channel); | 103 | programmingJoke(current_channel); |
101 | } | 104 | } |
102 | - else if(message.includes(' me ')){ | 105 | + else if(message.includes(' reddit')){ |
103 | - bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth')); | 106 | + redditJoke(current_channel); |
104 | } | 107 | } |
105 | - else{ | 108 | + else if(message.includes(' funny story')){ |
106 | - comment = "Sorry I dont' have that kind of joke.....:droplet::droplet::droplet:\nPlease use @joker --help to know what I can do!"; | 109 | + Funnystory(current_channel); |
107 | - bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed')); | ||
108 | - | ||
109 | } | 110 | } |
110 | - | 111 | + else if(message.includes(' me')){ |
112 | + comment = "Please use @joker --help to know what I can do!:smiliey::smiliey::smiliey:\n You can write type of joke[knock-knock, general, programming, funny story, reddit]"; | ||
113 | + bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth')); | ||
114 | + bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed')); | ||
115 | + } | ||
111 | } | 116 | } |
112 | else if(message.includes(' help')){ | 117 | else if(message.includes(' help')){ |
113 | - | 118 | + comment = "If you want to start @joker then write [tell me] and write type of joke [knock-knock, general, programming, funny story, reddit]:smiley:!!!"; |
119 | + bot.postMessageToChannel(current_channel, comment, emoji.emojis('smiliey')); | ||
114 | } | 120 | } |
115 | else if(message.includes(' what jokes')){ | 121 | else if(message.includes(' what jokes')){ |
116 | - jokeTypes = ["general", 'programming', 'knock-knock']; | 122 | + jokeTypes = ["general", 'programming', 'knock-knock','reddit','funny story']; |
117 | - bot.postMessageToChannel(current_channel, `I have ${jokeTypes[0]}, ${jokeTypes[1]}, ${jokeTypes[2]} jokes!! :thumbsup: :thumbsup:`, emoji.emojis('thumbsup')); | 123 | + bot.postMessageToChannel(current_channel, `I have ${jokeTypes[0]}, ${jokeTypes[1]}, ${jokeTypes[2]},${jokeTypes[3]},${jokeTypes[4]} jokes!! :thumbsup: :thumbsup:`, emoji.emojis('thumbsup')); |
118 | return; | 124 | return; |
119 | } | 125 | } |
120 | - // else{ | 126 | + //else if(message.inculdes(' write')) |
121 | - // const embarrased = { | 127 | + //{ |
122 | - // icon_emoji: ':flushed:' | 128 | + // MakeJoke(message); |
123 | - // }; | 129 | + //} |
124 | - // const sweat = { | 130 | + /*else{ |
125 | - // icon_emoji: ':droplet:' | 131 | + comment = "Sorry I'm not smart enough to understand this.....\nPlease use @joker help to know what I can do!"; |
126 | - // }; | 132 | + bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed')); |
127 | - // comment = "Sorry I'm not smart enough to understand this.....\nPlease use @joker help to know what I can do!"; | 133 | + }*/ |
128 | - // bot.postMessageToChannel('everyone', comment, embarrased); | 134 | +} |
129 | - | 135 | +function MakeJoke(message){ |
130 | - // } | 136 | + fs.writeFile('../joke_data//jokes.json',message,function(err){ |
137 | + if(err) { | ||
138 | + console.log('Error' + err); | ||
139 | + } | ||
140 | + console.log('완료'); | ||
141 | + }) | ||
131 | } | 142 | } |
132 | - | ||
133 | //Gets a random integer | 143 | //Gets a random integer |
134 | function getRandomInt(max_num) { | 144 | function getRandomInt(max_num) { |
135 | min = Math.ceil(1); | 145 | min = Math.ceil(1); |
... | @@ -251,6 +261,62 @@ programmingJoke= (user_channel)=>{ | ... | @@ -251,6 +261,62 @@ programmingJoke= (user_channel)=>{ |
251 | }) | 261 | }) |
252 | }; | 262 | }; |
253 | 263 | ||
264 | + | ||
265 | + | ||
266 | +//Function for giving out funny story | ||
267 | +Funnystory= (user_channel)=>{ | ||
268 | + MongoClient.connect(url, function (err, client){ | ||
269 | + if (err) throw err; | ||
270 | + var db = client.db('FunnyStoryapi'); | ||
271 | + | ||
272 | + | ||
273 | + random = getRandomInt(200); | ||
274 | + result = db.collection('FunnyStory').findOne({id: random}); | ||
275 | + user = result; | ||
276 | + //if the random picked api type is not general execute the function from the start to get another format for general type | ||
277 | + user.then(function(total){ | ||
278 | + category = total.category | ||
279 | + bot.postMessageToChannel(user_channel, category, emoji.emojis('smiliey')); | ||
280 | + console.log("이야기 카테고리") | ||
281 | + return total; | ||
282 | + }) | ||
283 | + .then((all)=>{ | ||
284 | + story = all.body; | ||
285 | + bot.postMessageToChannel(user_channel, `${story}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing')) | ||
286 | + console.log("이야기 시작!"); | ||
287 | + | ||
288 | + }) | ||
289 | + client.close(); | ||
290 | + }) | ||
291 | + }; | ||
292 | + | ||
293 | + | ||
294 | +//Function for giving out random joke after filtering only reddit jokes | ||
295 | +redditJoke= (user_channel)=>{ | ||
296 | + MongoClient.connect(url, function (err, client){ | ||
297 | + if (err) throw err; | ||
298 | + var db = client.db('redditjoke'); | ||
299 | + | ||
300 | + json_max = 70; | ||
301 | + random = getRandomInt(json_max); | ||
302 | + result = db.collection('reddit').findOne({id: random}); | ||
303 | + user = result; | ||
304 | + //if the random picked api type is not general execute the function from the start to get another format for general type | ||
305 | + user.then(function(total){ | ||
306 | + title = total.title; | ||
307 | + bot.postMessageToChannel(user_channel, title, emoji.emojis('smiliey')); | ||
308 | + console.log("문답형 JOKE") | ||
309 | + return total; | ||
310 | + }) | ||
311 | + .then((all)=>{ | ||
312 | + joke = all.body; | ||
313 | + bot.postMessageToChannel(user_channel, `${joke}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing')) | ||
314 | + console.log("정답은~~"); | ||
315 | + | ||
316 | + }) | ||
317 | + client.close(); | ||
318 | + }) | ||
319 | + }; | ||
254 | //Function for giving out random joke after filtering only knock-knock type jokes | 320 | //Function for giving out random joke after filtering only knock-knock type jokes |
255 | knockknockJoke= (user_channel)=>{ | 321 | knockknockJoke= (user_channel)=>{ |
256 | MongoClient.connect(url, function (err, client){ | 322 | MongoClient.connect(url, function (err, client){ | ... | ... |
... | @@ -2,19 +2,23 @@ const MongoClient = require('mongodb').MongoClient; | ... | @@ -2,19 +2,23 @@ const MongoClient = require('mongodb').MongoClient; |
2 | const jokedoc = require('./joke_data/jokes.json'); | 2 | const jokedoc = require('./joke_data/jokes.json'); |
3 | const dbname = 'jokeapi'; | 3 | const dbname = 'jokeapi'; |
4 | const collec = 'jokes'; | 4 | const collec = 'jokes'; |
5 | +<<<<<<< HEAD | ||
5 | const dboperation = require('./operations'); | 6 | const dboperation = require('./operations'); |
6 | 7 | ||
8 | +======= | ||
9 | +const dboperation = require('./operations.js'); | ||
10 | +>>>>>>> master | ||
7 | var url = "mongodb://localhost:27017/jokeapi"; | 11 | var url = "mongodb://localhost:27017/jokeapi"; |
8 | 12 | ||
9 | //Create database | 13 | //Create database |
10 | -MongoClient.connect(url, function(err, db) { | 14 | +MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { |
11 | if (err) throw err; | 15 | if (err) throw err; |
12 | console.log("Database created!"); | 16 | console.log("Database created!"); |
13 | db.close(); | 17 | db.close(); |
14 | }); | 18 | }); |
15 | 19 | ||
16 | //Create collection | 20 | //Create collection |
17 | -MongoClient.connect(url, function(err, db) { | 21 | +MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { |
18 | if (err) throw err; | 22 | if (err) throw err; |
19 | var dbo = db.db("jokeapi"); | 23 | var dbo = db.db("jokeapi"); |
20 | dbo.createCollection("joke", function(err, res) { | 24 | dbo.createCollection("joke", function(err, res) { |
... | @@ -26,7 +30,7 @@ MongoClient.connect(url, function(err, db) { | ... | @@ -26,7 +30,7 @@ MongoClient.connect(url, function(err, db) { |
26 | 30 | ||
27 | 31 | ||
28 | //Storing data jokes.json data into mongodb | 32 | //Storing data jokes.json data into mongodb |
29 | -MongoClient.connect(url).then((client) => { | 33 | +MongoClient.connect(url,{ useNewUrlParser: true }).then((client) => { |
30 | 34 | ||
31 | console.log('Connected correctly to server'); | 35 | console.log('Connected correctly to server'); |
32 | 36 | ... | ... |
db_control_funny.js
0 → 100644
1 | +const MongoClient = require('mongodb').MongoClient; | ||
2 | +const jokedoc = require('./joke_data/Funny_story.json'); | ||
3 | +const dbname = 'FunnyStoryapi'; | ||
4 | +const collec = 'FunnyStory'; | ||
5 | +const dboperation = require('./operations.js'); | ||
6 | +var url = "mongodb://localhost:27017/FunnyStoryapi"; | ||
7 | + | ||
8 | +//Create database | ||
9 | +MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { | ||
10 | + if (err) throw err; | ||
11 | + console.log("Database created!"); | ||
12 | + db.close(); | ||
13 | +}); | ||
14 | + | ||
15 | +//Create collection | ||
16 | +MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { | ||
17 | + if (err) throw err; | ||
18 | + var dbo = db.db("FunnyStoryapi"); | ||
19 | + dbo.createCollection("FunnyStories", function(err, res) { | ||
20 | + if (err) throw err; | ||
21 | + console.log("Collection created!"); | ||
22 | + db.close(); | ||
23 | + }); | ||
24 | + }); | ||
25 | + | ||
26 | + | ||
27 | +//Storing data jokes.json data into mongodb | ||
28 | +MongoClient.connect(url,{ useNewUrlParser: true }).then((client) => { | ||
29 | + | ||
30 | + console.log('Connected correctly to server'); | ||
31 | + | ||
32 | + | ||
33 | + const db = client.db(dbname); | ||
34 | + | ||
35 | + dboperation.insertDocument(db, jokedoc, collec) | ||
36 | + .then((result) => { | ||
37 | + console.log("Inserted Document:\n", result.ops); | ||
38 | + | ||
39 | + return dboperation.findDocuments(db, collec); | ||
40 | + }) | ||
41 | + .catch((err) => console.log(err)); | ||
42 | + | ||
43 | +}) | ||
44 | +.catch((err) => console.log(err)); |
db_control_red.js
0 → 100644
1 | +const MongoClient = require('mongodb').MongoClient; | ||
2 | +const jokedoc = require('./joke_data/reddit_joke.json'); | ||
3 | +const dbname = 'redditjoke'; | ||
4 | +const collec = 'reddit'; | ||
5 | +const dboperation = require('./operations.js'); | ||
6 | +var url = "mongodb://localhost:27017/redditjoke"; | ||
7 | + | ||
8 | +//Create database | ||
9 | +MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { | ||
10 | + if (err) throw err; | ||
11 | + console.log("Database created!"); | ||
12 | + db.close(); | ||
13 | +}); | ||
14 | + | ||
15 | +//Create collection | ||
16 | +MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { | ||
17 | + if (err) throw err; | ||
18 | + var dbo = db.db("redditjoke"); | ||
19 | + dbo.createCollection("redditjokes", function(err, res) { | ||
20 | + if (err) throw err; | ||
21 | + console.log("Collection created!"); | ||
22 | + db.close(); | ||
23 | + }); | ||
24 | + }); | ||
25 | + | ||
26 | + | ||
27 | +//Storing data jokes.json data into mongodb | ||
28 | +MongoClient.connect(url,{ useNewUrlParser: true }).then((client) => { | ||
29 | + | ||
30 | + console.log('Connected correctly to server'); | ||
31 | + | ||
32 | + | ||
33 | + const db = client.db(dbname); | ||
34 | + | ||
35 | + dboperation.insertDocument(db, jokedoc, collec) | ||
36 | + .then((result) => { | ||
37 | + console.log("Inserted Document:\n", result.ops); | ||
38 | + | ||
39 | + return dboperation.findDocuments(db, collec); | ||
40 | + }) | ||
41 | + .catch((err) => console.log(err)); | ||
42 | + | ||
43 | +}) | ||
44 | +.catch((err) => console.log(err)); | ||
45 | + |
joke_data/Funny_story.json
0 → 100644
This diff could not be displayed because it is too large.
joke_data/reddit_joke.json
0 → 100644
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment