김민욱

add reddit joke

......@@ -4,12 +4,11 @@ const dbname = 'jokeapi';
const emoji = require('../slack_emoji');
const url = 'mongodb://localhost:27017/';
exports.startbot = ()=>{
// Get authorization to use the slackbot
const bot = new SlackBot({
token : "xoxb-582582124755-587875604934-PR0QcCuQpOXXggtUN53ytfhH",
name : "Joker"
token : "xoxb-651692943605-645515979745-sfNJBLVbIIb86Bz1gfFc7oT2",
name : "joker"
});
// Start the slackbot
......@@ -81,9 +80,10 @@ bot.on('message', (data) => {
// Responding to Data
function handleMessage(message, current_channel){
console.log(message);
var test = -1;
//Handles message response depending on the user message
if(message.includes(' tell me') || message.includes(' Tell me')){
test =1;
if(message.includes(' knock')){
knockknockJoke(current_channel);
}
......@@ -98,37 +98,41 @@ function handleMessage(message, current_channel){
else if(message.includes(' programming')){
programmingJoke(current_channel);
}
else if(message.includes(' me ')){
bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth'));
else if(message.includes(' reddit')){
redditJoke(current_channel);
}
else{
comment = "Sorry I dont' have that kind of joke.....:droplet::droplet::droplet:\nPlease use @joker --help to know what I can do!";
else if(message.includes(' me')){
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]";
bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth'));
bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
}
}
}
else if(message.includes(' help')){
comment = "If you want to start @joker then write [tell me] and write type of joke [knock-knock, general, programming]:smiley:!!";
bot.postMessageToChannel(current_channel, comment, emoji.emojis('smiliey'));
}
else if(message.includes(' what jokes')){
jokeTypes = ["general", 'programming', 'knock-knock'];
bot.postMessageToChannel(current_channel, `I have ${jokeTypes[0]}, ${jokeTypes[1]}, ${jokeTypes[2]} jokes!! :thumbsup: :thumbsup:`, emoji.emojis('thumbsup'));
return;
}
// else{
// const embarrased = {
// icon_emoji: ':flushed:'
// };
// const sweat = {
// icon_emoji: ':droplet:'
// };
// comment = "Sorry I'm not smart enough to understand this.....\nPlease use @joker help to know what I can do!";
// bot.postMessageToChannel('everyone', comment, embarrased);
// }
//else if(message.inculdes(' write'))
//{
// MakeJoke(message);
//}
/*else if (test = 0){
comment = "Sorry I'm not smart enough to understand this.....\nPlease use @joker help to know what I can do!";
bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
}*/
}
function MakeJoke(message){
fs.writeFile('../joke_data//jokes.json',message,function(err){
if(err) {
console.log('Error' + err);
}
console.log('완료');
})
}
//Gets a random integer
function getRandomInt(max_num) {
min = Math.ceil(1);
......@@ -250,6 +254,33 @@ programmingJoke= (user_channel)=>{
})
};
//Function for giving out random joke after filtering only reddit jokes
redditJoke= (user_channel)=>{
MongoClient.connect(url, function (err, client){
if (err) throw err;
var db = client.db('redditjoke');
json_max = 70;
random = getRandomInt(json_max);
result = db.collection('reddit').findOne({id: random});
user = result;
//if the random picked api type is not general execute the function from the start to get another format for general type
user.then(function(total){
title = total.title;
bot.postMessageToChannel(user_channel, title, emoji.emojis('smiliey'));
console.log("문답형 JOKE")
return total;
})
.then((all)=>{
joke = all.body;
bot.postMessageToChannel(user_channel, `${joke}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
console.log("정답은~~");
return joke;
})
client.close();
})
};
//Function for giving out random joke after filtering only knock-knock type jokes
knockknockJoke= (user_channel)=>{
MongoClient.connect(url, function (err, client){
......
......@@ -2,18 +2,18 @@ const MongoClient = require('mongodb').MongoClient;
const jokedoc = require('./joke_data/jokes.json');
const dbname = 'jokeapi';
const collec = 'jokes';
const dboperation = require('./operations.js');
var url = "mongodb://localhost:27017/jokeapi";
//Create database
MongoClient.connect(url, function(err, db) {
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
//Create collection
MongoClient.connect(url, function(err, db) {
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("jokeapi");
dbo.createCollection("joke", function(err, res) {
......@@ -25,7 +25,7 @@ MongoClient.connect(url, function(err, db) {
//Storing data jokes.json data into mongodb
MongoClient.connect(url).then((client) => {
MongoClient.connect(url,{ useNewUrlParser: true }).then((client) => {
console.log('Connected correctly to server');
......
const MongoClient = require('mongodb').MongoClient;
const jokedoc = require('./joke_data/reddit_joke.json');
const dbname = 'redditjoke';
const collec = 'reddit';
const dboperation = require('./operations.js');
var url = "mongodb://localhost:27017/redditjoke";
//Create database
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
//Create collection
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("redditjoke");
dbo.createCollection("redditjokes", function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close();
});
});
//Storing data jokes.json data into mongodb
MongoClient.connect(url,{ useNewUrlParser: true }).then((client) => {
console.log('Connected correctly to server');
const db = client.db(dbname);
dboperation.insertDocument(db, jokedoc, collec)
.then((result) => {
console.log("Inserted Document:\n", result.ops);
return dboperation.findDocuments(db, collec);
})
.catch((err) => console.log(err));
})
.catch((err) => console.log(err));
// //will encapsulate all that database operations
// const assert = require('assert');
// exports.insertDocument = (db, document, collection, callback) =>{
// const coll = db.collection(collection);
// return coll.insert(document);
// };
// exports.findDocuments = (db, collection, input,callback)=>{
// const coll = db.collection(collection);
// return coll.find({"type": input}).toArray();
// }
// exports.removeDocuments = (db, document , collection, callback)=>{
// const coll = db.collection(collection);
// return coll.deleteone(document);
// }
// exports.updateDocuments = (db, document , update ,collection, callback)=>{
// const coll = db.collection(collection);
// return coll.updateOne(document, {$set: update}, null);
// }
// exports.getdata = (db, document, collection, input, callback =>{
// result = findDocuments(db,collection);
// return result;
// })
\ No newline at end of file
This diff is collapsed. Click to expand it.