김연준

merging upgraded version from master branch

<<<<<<< HEAD
<h1>Building a Slackbot</h1><br>
<h2>A slackbot that reacts to user's chat and sends jokes </h2><br><br>
<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>
=======
<h1>Joker</h1><br>
<h2>A slackbot that reacts to user's chat and sends jokes </h2><br>
>>>>>>> master
<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>
<b>Collected Jokes api link: https://github.com/15Dkatz/official_joke_api<br>
Opensource for slackbot module link : https://github.com/mishk0/slack-bot-api<br>
Used Database: Mongodb</b><br>
![joker_fix](https://user-images.githubusercontent.com/39256000/58455581-0ce84100-815d-11e9-833c-90c7af08f52f.jpg)
![joker_fix](https://user-images.githubusercontent.com/39256000/58455581-0ce84100-815d-11e9-833c-90c7af08f52f.jpg)
\ No newline at end of file
......
......@@ -4,13 +4,17 @@ 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({
<<<<<<< HEAD
//xoxb-582582124755-587875604934-gdWsVhzGXCze3mECWXpTsAP6
token : "xoxb-582582124755-587875604934-gdWsVhzGXCze3mECWXpTsAP6",
name : "Joker"
=======
token : "xoxb-651692943605-645515979745-sfNJBLVbIIb86Bz1gfFc7oT2",
name : "joker"
>>>>>>> master
});
// Start the slackbot
......@@ -82,7 +86,6 @@ bot.on('message', (data) => {
// Responding to Data
function handleMessage(message, current_channel){
console.log(message);
//Handles message response depending on the user message
if(message.includes(' tell me') || message.includes(' Tell me')){
if(message.includes(' knock')){
......@@ -99,37 +102,44 @@ 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!";
bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
else if(message.includes(' funny story')){
Funnystory(current_channel);
}
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, funny story, reddit]";
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, funny story, reddit]: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'));
jokeTypes = ["general", 'programming', 'knock-knock','reddit','funny story'];
bot.postMessageToChannel(current_channel, `I have ${jokeTypes[0]}, ${jokeTypes[1]}, ${jokeTypes[2]},${jokeTypes[3]},${jokeTypes[4]} 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{
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);
......@@ -251,6 +261,62 @@ programmingJoke= (user_channel)=>{
})
};
//Function for giving out funny story
Funnystory= (user_channel)=>{
MongoClient.connect(url, function (err, client){
if (err) throw err;
var db = client.db('FunnyStoryapi');
random = getRandomInt(200);
result = db.collection('FunnyStory').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){
category = total.category
bot.postMessageToChannel(user_channel, category, emoji.emojis('smiliey'));
console.log("이야기 카테고리")
return total;
})
.then((all)=>{
story = all.body;
bot.postMessageToChannel(user_channel, `${story}:stuck_out_tongue_winking_eye::laughing:`, emoji.emojis('laughing'))
console.log("이야기 시작!");
})
client.close();
})
};
//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("정답은~~");
})
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,19 +2,23 @@ const MongoClient = require('mongodb').MongoClient;
const jokedoc = require('./joke_data/jokes.json');
const dbname = 'jokeapi';
const collec = 'jokes';
<<<<<<< HEAD
const dboperation = require('./operations');
=======
const dboperation = require('./operations.js');
>>>>>>> master
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) {
......@@ -26,7 +30,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/Funny_story.json');
const dbname = 'FunnyStoryapi';
const collec = 'FunnyStory';
const dboperation = require('./operations.js');
var url = "mongodb://localhost:27017/FunnyStoryapi";
//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("FunnyStoryapi");
dbo.createCollection("FunnyStories", 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));
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));
This diff could not be displayed because it is too large.
This diff is collapsed. Click to expand it.