김연준

Initial commit for scatching slackbot idea

1 +node_modules
...\ No newline at end of file ...\ No newline at end of file
1 +const SlackBot = require('slackbots');
2 +const axios = require('axios');
3 +
4 +const bot = new SlackBot({
5 + token : "xoxb-582582124755-587875604934-rRhFVlXlB0StEMnlrmsQlcac",
6 + name : "Joker"
7 +});
8 +
9 +// Start Handler
10 +bot.on('start', () =>{
11 + const face = {
12 + icon_emoji: ':laughing:'
13 + };
14 +
15 + bot.postMessageToChannel('everyone', 'Feeling tired??? Have some fun with @Joker!'
16 + , face);
17 +});
18 +
19 +// Error Handler
20 +bot.on('error', (err) => console.log(err));
21 +
22 +//Message Handler
23 +bot.on('message', (data) => {
24 + if(data.type !== 'message'){
25 + return;
26 + }
27 +
28 + console.log(data);
29 + handleMessage(data.text);
30 +
31 +});
32 +
33 +
34 +// Responding to Data
35 +function handleMessage(message){
36 + if(message.includes('chucknorris')){
37 + chuckJoke();
38 + }
39 + else if(message.includes(' yomama')){
40 + yoMamaJoke();
41 + }
42 + else if(message.includes(' random')){
43 + randomJoke();
44 + }
45 + else if(message.includes(' help')){
46 + runHelp();
47 + }
48 +}
49 +
50 +
51 +// Tell a Chuck Norris Joke
52 +function chuckJoke(){
53 + axios.get('http://api.icndb.com/jokes/random/')
54 + .then(res =>{
55 + const joke = res.data.value.joke;
56 +
57 + const face = {
58 + icon_emoji: ':laughing:'
59 + };
60 +
61 + bot.postMessageToChannel('everyone', `Chuck Norris: ${joke}`,face);
62 + bot.postMessageToChannel('full-stack-web', `Yo mama: ${joke}`,face);
63 + bot.postMessageToChannel('bot_test', `Yo mama: ${joke}`,face);
64 + });
65 +}
66 +
67 +// Tell a yomama Joke
68 +function yoMamaJoke(){
69 + axios.get('http://api.yomomma.info/')
70 + .then(res =>{
71 + const joke = res.data.joke;
72 +
73 + const face = {
74 + icon_emoji: ':laughing:'
75 + };
76 +
77 + bot.postMessageToChannel('everyone', `Yo mama: ${joke}`,face);
78 + bot.postMessageToChannel('full-stack-web', `Yo mama: ${joke}`,face);
79 + bot.postMessageToChannel('bot_test', `Yo mama: ${joke}`,face);
80 +
81 + });
82 +}
83 +//Tell random joke
84 +function randomJoke(){
85 + const rand = Math.floor(Math.random() * 2) +1;
86 + if(rand ===1){
87 + chuckJoke();
88 + }
89 + else if(rand === 2){
90 + yoMamaJoke();
91 + }
92 +}
93 +function runHelp(){
94 + const face = {
95 + icon_emoji: ':question:'
96 + };
97 +
98 + bot.postMessageToChannel('everyone', "Type @joker and write a joke that you would like\n ex- @joker random",face);
99 + bot.postMessageToChannel('full-stack-web', "Type @joker and write a joke that you would like\n ex- @joker random",face);
100 +}
...\ No newline at end of file ...\ No newline at end of file
This diff is collapsed. Click to expand it.
1 +{
2 + "name": "joker",
3 + "version": "1.0.0",
4 + "description": "Random joke teller",
5 + "main": "index.js",
6 + "scripts": {
7 + "start": "node index.js"
8 + },
9 + "author": "Yeonjun Kim",
10 + "license": "MIT",
11 + "dependencies": {
12 + "axios": "^0.18.0",
13 + "free-google-image-search": "^1.0.0",
14 + "google-images": "^2.1.0",
15 + "slackbots": "^1.2.0"
16 + }
17 +}