Last active
August 29, 2015 14:25
-
-
Save brh55/06962cc21314e598b998 to your computer and use it in GitHub Desktop.
Simple Command and Reply Slack Bot
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
module.exports = require('node-slackr'); | |
// Set up server dependencies | |
var express = require('express'), | |
bodyParser = require('body-parser'), | |
app = express(); | |
// App usage | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: true})); | |
// Static Default variables | |
// TODO: Move to .env file for VC | |
var urlHook = 'incominghookURL', | |
channel = '#sandbox', | |
username = 'Tomkat'; | |
// Initialize client | |
Slack = require('node-slackr'); | |
slack = new Slack(urlHook, { | |
channel: channel, | |
username: username | |
}); | |
var server_port = process.env.YOUR_PORT || process.env.PORT || 80; | |
var server_host = process.env.YOUR_HOST || '0.0.0.0'; | |
app.listen(server_port, server_host, function() { | |
console.log('Listening on port %d', server_port); | |
}); | |
app.get('/', function(req, res) { | |
res.send("server is up and running!"); | |
}); | |
app.post('/commands', function(req, res) { | |
var reqString = req.body.text.toLowerCase(); | |
// Default Message | |
messages = { | |
text: "Message test", | |
channel: "#sandbox", | |
username: "new-bot-name", | |
icon_url: "https://slack.com/img/icons/app-57.png" | |
} | |
if (commandCheck(reqString, "ship")) { | |
messages = { | |
text: "The ship has arrived!", | |
channel: '#anthro_tomkat', | |
username: "P.O.P.P.A Ship", | |
icon_emoji: ":ship:" | |
} | |
} | |
if (commandCheck(reqString, "cactus")) { | |
var user = getUser(reqString); | |
messages = { | |
text: "Sorry," + user + "one does not simply say Cactus, without a little dance. \n http://graphics.desivalley.com/wp-content/uploads/2011/01/cactus.gif", | |
channel: '#anthro_tomkat', | |
username: "Cactus", | |
icon_emoji: ":cactus:" | |
} | |
} | |
if (commandCheck(reqString, "hills")) { | |
message = { | |
text: "https://files.slack.com/files-pri/T024UAEDJ-F071KCSUQ/soup-in-the-hills.jpg", | |
channel: '#anthro_tomkat', | |
username: 'The Real Soup', | |
icon_emoji: ":soup:" | |
} | |
} | |
if (commandCheck(reqString, "soup")) { | |
soupArray = [ | |
"http://media.giphy.com/media/VJys6sgIRtcNa/giphy.gif", | |
"http://media.veryfunnypics.eu/2013/10/funny-pics-gordon-ramsay-this-soup-is-so-hot.jpg", | |
"http://www.comicsandmemes.com/wp-content/uploads/Gordon-Ramsay-Angry-Kitchen-SOUP-IS-DRY.jpg", | |
"Only the pure in heart can make a good soup. \n - Ludwig van Beethoven", | |
"A first-rate soup is more creative than a second-rate painting. \n - Abraham Maslow", | |
"I live on good soup, not on fine words. \n - Moliere", | |
"http://originalsoupman.com/wp-content/uploads/2013/03/soupwshaq.jpg", | |
"http://stream1.gifsoup.com/view3/1294662/campbells-chicken-noodle-soup-o.gif", | |
"https://144f2a3a2f948f23fc61-ca525f0a2beaec3e91ca498facd51f15.ssl.cf3.rackcdn.com/uploads/food_portal_data/image/image/2692/compressed_tumblr_m4p19sDgqW1ru5tboo1_500-1.gif", | |
"When you have a good stock, you can make a good soup. \n - Martin Yan \n http://image.pbs.org/merlin/s3.amazonaws.com/pbs.merlin.cdn.prod/curatedwebobjects/yan_thumb.jpg.resize.64x64.jpg", | |
'"Which hand should you use to stir the soup?" "Neither, you should use a spoon."', | |
"To soup--may it be seen and not heard.", | |
'"What is Dracula\'s favorite soup?" "Scream of tomato."', | |
"http://i.imgur.com/BODefEN.gif", | |
"http://www.mememaker.net/static/images/memes/3789720.jpg", | |
"https://files.slack.com/files-pri/T024UAEDJ-F04N85TGS/pasted_image_at_2015_05_06_11_13_am.png" | |
]; | |
var randomNumber = Math.floor((Math.random() * soupArray.length)); | |
messages = { | |
text: soupArray[randomNumber], | |
channel: '#anthro_tomkat', | |
username: "P.O.P.P.A Soup", | |
icon_emoji: ":soup:" | |
} | |
} | |
if (commandCheck(reqString, "babka")) { | |
var buildStart = "Did you ask for a...babka!? \n"; | |
babkaArray = [ | |
"https://img1.etsystatic.com/027/1/7376718/il_570xN.540669381_6et8.jpg", | |
buildStart + "https://s-media-cache-ak0.pinimg.com/236x/f9/b5/e6/f9b5e603a36e3870b4a2d94764ec956f.jpg", | |
buildStart + "https://s-media-cache-ak0.pinimg.com/236x/46/67/e6/4667e6304feaed1750e9dbe1a2853694.jpg", | |
"http://s9.favim.com/orig/130810/bitch-black-and-white-donut-doughnut-Favim.com-845433.png", | |
"http://chrispiascik.wpengine.netdna-cdn.com/wp-content/uploads/2014/06/1654-20140624-DangerDonut.jpg", | |
"I’m on my version of the protein diet, but there ain’t no protein in it. It’s a Krispy Kreme doughnut between two Cinnabons. And you soak it overnight in Red Bull. Then you chase it with a Snickers. \n - J. B. Smoove \n http://s1.evcdn.com/images/block/I0-001/017/815/552-7.jpeg_/jb-smoove-comix-presents-52.jpeg", | |
"I am a jelly doughnut. \n - President John F. Kennedy \n http://thumbs.ebaystatic.com/d/l96/m/mopTO2z8mA8r5tqbCbMOM9g.jpg", | |
"Mmmmmm…doughnuts \n - Homer Simpson \n http://cdn-img.easyicon.net/png/11341/1134100.gif" | |
]; | |
var randomNumber = Math.floor((Math.random() * babkaArray.length)); | |
messages = { | |
text: babkaArray[randomNumber], | |
channel: '#anthro_tomkat', | |
username: "P.O.P.P.A Babka", | |
icon_emoji: ":babka:" | |
} | |
} | |
if (commandCheck(reqString, "burrito")) { | |
var buildStart = "Did you ask for a..burrito? \n"; | |
stringArray = [ | |
'https://wheresthetofu.files.wordpress.com/2013/12/tumblr_lvgm6mdcyo1qgvw4mo2_1280.jpg', | |
'http://images.bwbx.io/cms/2013-10-18/1018_Chipotle_tofu_630x420.jpg', | |
'https://files.slack.com/files-pri/T024UAEDJ-F04NW1VFP/pat_burritoman.jpg' | |
]; | |
var randomNumber = Math.floor((Math.random() * stringArray.length)); | |
messages = { | |
text: stringArray[randomNumber], | |
channel: '#anthro_tomkat', | |
username: "P.O.P.P.A Burrito", | |
icon_url: "http://www.bodybuilding.com/fun/images/2012/chicken_veggie_burrito_50.jpg" | |
} | |
} | |
slack.notify(messages); | |
}); | |
// Command Checker | |
function commandCheck(commandArray, command) { | |
if (commandArray.indexOf(command) !=-1) { | |
return true; | |
} | |
}; | |
function getUser(reqString) { | |
if (reqString.indexOf(":") != -1) { | |
var colonLoc = reqString.indexOf(":"); | |
var userName = reqString.substring(0, colonLoc); | |
return userName; | |
} else { | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment