-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (77 loc) · 1.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const SlackBot = require("slackbots");
const axios = require("axios");
// Initialize JokeBot
const bot = new SlackBot({
token: "xoxb-414981550259-417598588976-CYRzBPhftJG66skPMXTwKUem",
name: "Joke Bot"
});
// Start Handler
bot.on("start", () => {
const params = {
icon_emoji: ":smiley:"
};
bot.postMessageToChannel(
"react-doubts",
"Get ready to Laugh with @JokeBot",
params
);
});
// Error Handler
bot.on("error", error => console.log(error));
// Message Handler
bot.on("message", data => {
if (data.type !== "message") {
return;
}
handleMessage(data.text);
});
// filter message coming from slack
const handleMessage = message => {
if (message.includes(" chucknorris")) {
chuckJoke();
} else if (message.includes(" yomoma")) {
yoMamaJoke();
} else if (message.includes(" random")) {
randomJoke();
} else if (message.includes(" help")) {
help();
}
};
const chuckJoke = () => {
axios.get("http://api.icndb.com/jokes/random").then(res => {
const joke = res.data.value.joke;
const params = {
icon_emoji: ":laughing:"
};
bot.postMessageToChannel("react-doubts", `Chuck Norris: ${joke}`, params);
});
};
const yoMamaJoke = () => {
axios.get("http://api.yomomma.info").then(res => {
const joke = res.data.joke;
const params = {
icon_emoji: ":laughing:"
};
bot.postMessageToChannel("react-doubts", `Yo Mama: ${joke}`, params);
});
};
// Tell a random Joke
const randomJoke = () => {
const rand = Math.floor(Math.random() * 2) + 1;
if (rand === 1) {
chuckJoke();
} else if (rand === 2) {
yoMamaJoke();
}
};
// show a list of commands
const help = () => {
const params = {
icon_emoji: ":question:"
};
bot.postMessageToChannel(
"react-doubts",
`Tell @JokeBot with either 'chucknorris', 'yomoma' or 'random' to get a Joke`,
params
);
};