Toggle navigation
Toggle navigation
This project
Loading...
Sign in
김민욱
/
Slack Jokebot
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Issues
1
Network
Create a new issue
Commits
Issue Boards
Authored by
오원석
2019-06-03 01:22:43 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
010b1ed1f6a2dda18d240a4d11926e40294a165d
010b1ed1
1 parent
80380dae
make jokes
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
9 deletions
Routers/route.js
joke_data/user.json
Routers/route.js
View file @
010b1ed
...
...
@@ -3,6 +3,9 @@ const SlackBot = require('slackbots'); //link : https://github.com/mishk0/slack-
const
dbname
=
'jokeapi'
;
const
emoji
=
require
(
'../slack_emoji'
);
const
url
=
'mongodb://localhost:27017/'
;
const
fs
=
require
(
'fs'
);
num
=
1
;
//const userjoke = require('./joke_data/user.json');
exports
.
startbot
=
()
=>
{
// Get authorization to use the slackbot
...
...
@@ -20,6 +23,7 @@ exports.startbot = ()=>{
for
(
i
=
0
;
i
<
channel_length
;
++
i
){
//postMessageToChannel(name, message [, params, callback]) (return: promise) - posts a message to channel by name.
bot
.
postMessageToChannel
(
data
.
channels
[
i
].
name
,
'Have some fun with @Joker!\nFor commands write @joker --help'
,
emoji
.
emojis
(
'bowtie'
));
}
return
data
;
...
...
@@ -32,10 +36,11 @@ exports.startbot = ()=>{
bot
.
on
(
'error'
,
(
err
)
=>
console
.
log
(
err
));
//Message Handler
bot
.
on
(
'message'
,
(
data
)
=>
{
bot
.
on
(
'message'
,
(
data
)
=>
{
if
(
data
.
type
!==
'message'
){
return
;
}
status
=
data
;
message_recieved
=
0
;
...
...
@@ -96,7 +101,7 @@ function handleMessage(message, current_channel){
else
if
(
message
.
includes
(
' programming'
)){
programmingJoke
(
current_channel
);
}
else
if
(
message
.
includes
(
' reddit'
)){
else
if
(
message
.
includes
(
' reddit'
)){
;
redditJoke
(
current_channel
);
}
else
if
(
message
.
includes
(
' funny story'
)){
...
...
@@ -107,6 +112,9 @@ function handleMessage(message, current_channel){
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
(
' userjoke'
)){
UserMakeJoke
(
current_channel
);
}
}
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:!!!"
;
...
...
@@ -117,28 +125,76 @@ function handleMessage(message, current_channel){
bot
.
postMessageToChannel
(
current_channel
,
`I have
${
jokeTypes
[
0
]}
,
${
jokeTypes
[
1
]}
,
${
jokeTypes
[
2
]}
,
${
jokeTypes
[
3
]}
,
${
jokeTypes
[
4
]}
jokes!! :thumbsup: :thumbsup:`
,
emoji
.
emojis
(
'thumbsup'
));
return
;
}
//else if(message.inculdes(' write'))
//{
// MakeJoke(message);
//}
else
if
(
message
.
includes
(
' make joke : '
))
{
console
.
log
(
'123'
);
MakeJoke
(
message
);
}
// else{
// console.log('456');
// UserMakeJoke(current_channel);
// }
/*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
);
var
obj
=
{
table
:[]
}
var
input
=
message
.
split
(
':'
);
var
msg
=
input
[
1
].
split
(
','
);
// obj.table.push({id : msg[1], type : msg[2], setup : msg[3], punchling : msg[4]});
// var json=JSON.stringify(obj);
fs
.
exists
(
'./joke_data/user.json'
,
function
(
exists
){
if
(
exists
){
console
.
log
(
"yes file exists"
);
fs
.
readFile
(
'./joke_data/user.json'
,
function
readFileCallback
(
err
,
data
){
if
(
err
){
console
.
log
(
err
);
}
else
{
obj
=
JSON
.
parse
(
data
);
obj
.
table
.
push
({
id
:
num
,
type
:
'userjoke'
,
setup
:
msg
[
0
],
punchling
:
msg
[
1
]});
var
json
=
JSON
.
stringify
(
obj
);
num
++
;
fs
.
writeFile
(
'./joke_data/user.json'
,
json
,
'utf8'
,
function
(
err
){
if
(
err
){
console
.
log
(
err
);
}
console
.
log
(
'완료'
);
});
}
});
}
else
{
console
.
log
(
"file not exists"
);
obj
.
table
.
push
({
id
:
num
,
type
:
'userjoke'
,
setup
:
msg
[
0
],
punchling
:
msg
[
1
]});
var
json
=
JSON
.
stringify
(
obj
);
num
++
;
fs
.
writeFile
(
'./joke_data/user.json'
,
json
,
'utf8'
,
function
(
err
){
if
(
err
){
console
.
log
(
err
);
}
console
.
log
(
'완료'
);
});
}
})
}
//Gets a random integer
function
getRandomInt
(
start
,
end
)
{
return
Math
.
floor
((
Math
.
random
()
*
100
)
%
(
end
-
start
))
+
start
;
}
//Function for giving out random joke
randomJoke
=
(
user_channel
)
=>
{
//Connect to mongodb client
...
...
@@ -176,6 +232,11 @@ randomJoke= (user_channel)=>{
})
}
// Function for giving out users making joke
UserMakeJoke
=
(
users_channel
)
=>
{
}
//Function for giving out random joke after filtering only general type jokes
generalJoke
=
(
user_channel
)
=>
{
MongoClient
.
connect
(
url
,
function
(
err
,
client
){
...
...
joke_data/user.json
0 → 100644
View file @
010b1ed
{
"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
Please
register
or
login
to post a comment