Created
August 12, 2019 12:38
-
-
Save BolajiAyodeji/a1fe5fe40f9c8d5abbc08f84edef5b74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SlackBot = require('slackbots'); | |
const axios = require('axios') | |
const dotenv = require('dotenv') | |
dotenv.config() | |
const bot = new SlackBot({ | |
token: `${process.env.BOT_TOKEN}`, | |
name: 'inspirenuggets' | |
}) | |
// Start Handler | |
bot.on('start', () => { | |
const params = { | |
icon_emoji: ':robot_face:' | |
} | |
bot.postMessageToChannel( | |
'random', | |
'Get inspired while working with @inspirenuggets', | |
params | |
); | |
}) | |
// Error Handler | |
bot.on('error', (err) => { | |
console.log(err); | |
}) | |
// Message Handler | |
bot.on('message', (data) => { | |
if(data.type !== 'message') { | |
return; | |
} | |
handleMessage(data.text); | |
}) | |
// Response Handler | |
function handleMessage(message) { | |
if(message.includes(' inspire me')) { | |
inspireMe() | |
} else if(message.includes(' random joke')) { | |
randomJoke() | |
} else if(message.includes(' help')) { | |
runHelp() | |
} | |
} | |
// inspire Me | |
function inspireMe() { | |
axios.get('https://raw.githubusercontent.com/BolajiAyodeji/inspireNuggets/master/src/quotes.json') | |
.then(res => { | |
const quotes = res.data; | |
const random = Math.floor(Math.random() * quotes.length); | |
const quote = quotes[random].quote | |
const author = quotes[random].author | |
const params = { | |
icon_emoji: ':male-technologist:' | |
} | |
bot.postMessageToChannel( | |
'random', | |
`:zap: ${quote} - *${author}*`, | |
params | |
); | |
}) | |
} | |
// Random Joke | |
function randomJoke() { | |
axios.get('https://api.chucknorris.io/jokes/random') | |
.then(res => { | |
const joke = res.data.value; | |
const params = { | |
icon_emoji: ':smile:' | |
} | |
bot.postMessageToChannel( | |
'random', | |
`:zap: ${joke}`, | |
params | |
); | |
}) | |
} | |
// Show Help | |
function runHelp() { | |
const params = { | |
icon_emoji: ':question:' | |
} | |
bot.postMessageToChannel( | |
'random', | |
`Type *@inspirenuggets* with *inspire me* to get an inspiring techie quote, *random joke* to get a Chuck Norris random joke and *help* to get this instruction again`, | |
params | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment