김민욱

add reddit joke

...@@ -4,12 +4,11 @@ const dbname = 'jokeapi'; ...@@ -4,12 +4,11 @@ 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({
11 - token : "xoxb-582582124755-587875604934-PR0QcCuQpOXXggtUN53ytfhH", 10 + token : "xoxb-651692943605-645515979745-sfNJBLVbIIb86Bz1gfFc7oT2",
12 - name : "Joker" 11 + name : "joker"
13 }); 12 });
14 13
15 // Start the slackbot 14 // Start the slackbot
...@@ -81,9 +80,10 @@ bot.on('message', (data) => { ...@@ -81,9 +80,10 @@ bot.on('message', (data) => {
81 // Responding to Data 80 // Responding to Data
82 function handleMessage(message, current_channel){ 81 function handleMessage(message, current_channel){
83 console.log(message); 82 console.log(message);
84 - 83 + var test = -1;
85 //Handles message response depending on the user message 84 //Handles message response depending on the user message
86 if(message.includes(' tell me') || message.includes(' Tell me')){ 85 if(message.includes(' tell me') || message.includes(' Tell me')){
86 + test =1;
87 if(message.includes(' knock')){ 87 if(message.includes(' knock')){
88 knockknockJoke(current_channel); 88 knockknockJoke(current_channel);
89 } 89 }
...@@ -98,37 +98,41 @@ function handleMessage(message, current_channel){ ...@@ -98,37 +98,41 @@ function handleMessage(message, current_channel){
98 else if(message.includes(' programming')){ 98 else if(message.includes(' programming')){
99 programmingJoke(current_channel); 99 programmingJoke(current_channel);
100 } 100 }
101 - else if(message.includes(' me ')){ 101 + else if(message.includes(' reddit')){
102 - bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth')); 102 + redditJoke(current_channel);
103 } 103 }
104 - else{ 104 + else if(message.includes(' me')){
105 - comment = "Sorry I dont' have that kind of joke.....:droplet::droplet::droplet:\nPlease use @joker --help to know what I can do!"; 105 + 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]";
106 + bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth'));
106 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed')); 107 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
107 -
108 } 108 }
109 -
110 } 109 }
111 else if(message.includes(' help')){ 110 else if(message.includes(' help')){
112 - 111 + comment = "If you want to start @joker then write [tell me] and write type of joke [knock-knock, general, programming]:smiley:!!";
112 + bot.postMessageToChannel(current_channel, comment, emoji.emojis('smiliey'));
113 } 113 }
114 else if(message.includes(' what jokes')){ 114 else if(message.includes(' what jokes')){
115 jokeTypes = ["general", 'programming', 'knock-knock']; 115 jokeTypes = ["general", 'programming', 'knock-knock'];
116 bot.postMessageToChannel(current_channel, `I have ${jokeTypes[0]}, ${jokeTypes[1]}, ${jokeTypes[2]} jokes!! :thumbsup: :thumbsup:`, emoji.emojis('thumbsup')); 116 bot.postMessageToChannel(current_channel, `I have ${jokeTypes[0]}, ${jokeTypes[1]}, ${jokeTypes[2]} jokes!! :thumbsup: :thumbsup:`, emoji.emojis('thumbsup'));
117 return; 117 return;
118 } 118 }
119 - // else{ 119 + //else if(message.inculdes(' write'))
120 - // const embarrased = { 120 + //{
121 - // icon_emoji: ':flushed:' 121 + // MakeJoke(message);
122 - // }; 122 + //}
123 - // const sweat = { 123 + /*else if (test = 0){
124 - // icon_emoji: ':droplet:' 124 + comment = "Sorry I'm not smart enough to understand this.....\nPlease use @joker help to know what I can do!";
125 - // }; 125 + bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
126 - // comment = "Sorry I'm not smart enough to understand this.....\nPlease use @joker help to know what I can do!"; 126 + }*/
127 - // bot.postMessageToChannel('everyone', comment, embarrased); 127 +}
128 - 128 +function MakeJoke(message){
129 - // } 129 + fs.writeFile('../joke_data//jokes.json',message,function(err){
130 + if(err) {
131 + console.log('Error' + err);
132 + }
133 + console.log('완료');
134 + })
130 } 135 }
131 -
132 //Gets a random integer 136 //Gets a random integer
133 function getRandomInt(max_num) { 137 function getRandomInt(max_num) {
134 min = Math.ceil(1); 138 min = Math.ceil(1);
...@@ -250,6 +254,33 @@ programmingJoke= (user_channel)=>{ ...@@ -250,6 +254,33 @@ programmingJoke= (user_channel)=>{
250 }) 254 })
251 }; 255 };
252 256
257 + //Function for giving out random joke after filtering only reddit jokes
258 + redditJoke= (user_channel)=>{
259 + MongoClient.connect(url, function (err, client){
260 + if (err) throw err;
261 + var db = client.db('redditjoke');
262 +
263 + json_max = 70;
264 + random = getRandomInt(json_max);
265 + result = db.collection('reddit').findOne({id: random});
266 + user = result;
267 + //if the random picked api type is not general execute the function from the start to get another format for general type
268 + user.then(function(total){
269 + title = total.title;
270 + bot.postMessageToChannel(user_channel, title, emoji.emojis('smiliey'));
271 + console.log("문답형 JOKE")
272 + return total;
273 + })
274 + .then((all)=>{
275 + joke = all.body;
276 + bot.postMessageToChannel(user_channel, `${joke}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
277 + console.log("정답은~~");
278 + return joke;
279 + })
280 + client.close();
281 + })
282 + };
283 +
253 //Function for giving out random joke after filtering only knock-knock type jokes 284 //Function for giving out random joke after filtering only knock-knock type jokes
254 knockknockJoke= (user_channel)=>{ 285 knockknockJoke= (user_channel)=>{
255 MongoClient.connect(url, function (err, client){ 286 MongoClient.connect(url, function (err, client){
......
...@@ -2,18 +2,18 @@ const MongoClient = require('mongodb').MongoClient; ...@@ -2,18 +2,18 @@ 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 - 5 +const dboperation = require('./operations.js');
6 var url = "mongodb://localhost:27017/jokeapi"; 6 var url = "mongodb://localhost:27017/jokeapi";
7 7
8 //Create database 8 //Create database
9 -MongoClient.connect(url, function(err, db) { 9 +MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
10 if (err) throw err; 10 if (err) throw err;
11 console.log("Database created!"); 11 console.log("Database created!");
12 db.close(); 12 db.close();
13 }); 13 });
14 14
15 //Create collection 15 //Create collection
16 -MongoClient.connect(url, function(err, db) { 16 +MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
17 if (err) throw err; 17 if (err) throw err;
18 var dbo = db.db("jokeapi"); 18 var dbo = db.db("jokeapi");
19 dbo.createCollection("joke", function(err, res) { 19 dbo.createCollection("joke", function(err, res) {
...@@ -25,7 +25,7 @@ MongoClient.connect(url, function(err, db) { ...@@ -25,7 +25,7 @@ MongoClient.connect(url, function(err, db) {
25 25
26 26
27 //Storing data jokes.json data into mongodb 27 //Storing data jokes.json data into mongodb
28 -MongoClient.connect(url).then((client) => { 28 +MongoClient.connect(url,{ useNewUrlParser: true }).then((client) => {
29 29
30 console.log('Connected correctly to server'); 30 console.log('Connected correctly to server');
31 31
......
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 +
46 +
47 +
48 +
49 +
50 +
51 +
52 +
53 +
54 +
55 +
56 +
57 +
58 +// //will encapsulate all that database operations
59 +// const assert = require('assert');
60 +
61 +// exports.insertDocument = (db, document, collection, callback) =>{
62 +// const coll = db.collection(collection);
63 +// return coll.insert(document);
64 +// };
65 +
66 +// exports.findDocuments = (db, collection, input,callback)=>{
67 +// const coll = db.collection(collection);
68 +// return coll.find({"type": input}).toArray();
69 +// }
70 +
71 +// exports.removeDocuments = (db, document , collection, callback)=>{
72 +// const coll = db.collection(collection);
73 +// return coll.deleteone(document);
74 +// }
75 +
76 +// exports.updateDocuments = (db, document , update ,collection, callback)=>{
77 +// const coll = db.collection(collection);
78 +// return coll.updateOne(document, {$set: update}, null);
79 +// }
80 +
81 +// exports.getdata = (db, document, collection, input, callback =>{
82 +// result = findDocuments(db,collection);
83 +// return result;
84 +// })
...\ No newline at end of file ...\ No newline at end of file
This diff is collapsed. Click to expand it.