Last active
July 5, 2016 20:18
-
-
Save nickdandakis/ed63d3028a22c0c536b14e86045f1168 to your computer and use it in GitHub Desktop.
Basic Botkit scaffolding for Slack slash commands with Mongo support
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
var Botkit = require('botkit'); | |
if(!process.env.SLACK_CLIENT_ID || | |
!process.env.SLACK_CLIENT_SECRET || | |
!process.env.SLACK_VERIFICATION_TOKEN) { | |
console.log('Error: Specify SLACK_CLIENT_ID, SLACK_CLIENT_SECRET and SLACK_VERIFICATION_TOKEN in environment'); | |
process.exit(1); | |
} | |
var config = {} | |
if (process.env.MLAB_URI) { | |
var BotkitStorage = require('botkit-storage-mongo'); | |
config = { | |
storage: BotkitStorage({mongoUri: process.env.MONGO_URI}), | |
}; | |
} else { | |
config = { | |
json_file_store: './db_shits_lit/', | |
}; | |
} | |
var controlla = Botkit.slackbot(config).configureSlackApp({ | |
clientId: process.env.SLACK_CLIENT_ID, | |
clientSecret: process.env.SLACK_CLIENT_SECRET, | |
scopes: ['commands', 'bot'], | |
}); | |
controlla.setupWebserver(process.env.PORT || 3000, function(error, webserver) { | |
controlla.createWebhookEndpoints(controlla.webserver); | |
controlla.createOauthEndpoints(controlla.webserver, function(error, request, response) { | |
if(error) { | |
response.status(500).send('ERROR: ' + error); | |
} else { | |
response.send("You've successfully installed this Slack app. Close the window!"); | |
} | |
}); | |
}); | |
controlla.on('slash_command', function(bot, message) { | |
switch(message.command) { | |
case '/command': // change this to your command | |
if(message.token !== process.env.SLACK_VERIFICATION_TOKEN) return; | |
if(message.text === '') { | |
bot.replyPrivate(message, 'Oh shit, waddup'); | |
return; | |
} | |
// do something with the message here | |
// payload can either be a string or an object that follows | |
// https://api.slack.com/docs/message-attachments | |
var payload = logic(message.text); | |
bot.reply(message, payload); | |
break; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment