Created
July 28, 2016 16:45
-
-
Save malonehedges/7d10a8ca392b26ad179e89ffa7fa9712 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
_ = require 'lodash' | |
path = require 'path' | |
Botkit = require 'botkit' | |
mongoStorage = require 'botkit-storage-mongo' | |
mongoose = require 'mongoose' | |
express = require 'express' | |
bodyParser = require 'body-parser' | |
Promise = require 'bluebird' | |
mongoose.Promise = Promise | |
DATABASE_URI = process.env.MONGODB_URI or process.env.MONGOHQ_URL | |
# Multi-team Setup | |
bots = {} | |
trackBot = (bot) -> | |
bots[bot.config.token] = bot | |
# Returns the `controller` | |
setupController = -> | |
controller = Botkit.slackbot | |
retry: Infinity | |
interactive_replies: true | |
storage: mongoStorage(mongoUri: DATABASE_URI) | |
# Configure the controller | |
controller.configureSlackApp | |
clientId: process.env.SLACK_CLIENT_ID | |
clientSecret: process.env.SLACK_CLIENT_SECRET | |
redirectUri: process.env.SLACK_REDIRECT_URI | |
scopes: [ 'bot', 'users:read', 'team:read' ] | |
# Handle a new bot | |
controller.on 'create_bot', (bot, config) -> | |
if bots[bot.config.token]? | |
# Already online! | |
else | |
bot.startRTM (err) -> | |
return console.error err if err | |
trackBot bot | |
bot.startPrivateConversation { user: config.createdBy }, (err, convo) -> | |
return console.error err if err | |
convo.say 'I just joined your team!' | |
convo.say 'You need to /invite me to a channel so I can be helpful.' | |
convo.next() | |
controller.on 'rtm_close', (bot) -> | |
setTimeout bot.startRTM, 3000 | |
controller | |
# Returns the `server` | |
setupServer = -> | |
server = express() | |
server.use bodyParser.json() | |
server.use bodyParser.urlencoded(extended: true) | |
server.set 'view engine', 'jade' | |
server.set 'views', path.join(__dirname, 'web') | |
# Add server locals (helpers) | |
server.locals.moment = require 'moment' | |
server.locals.arrayUtils = require './util/arrayUtils' | |
server | |
# Returns the `models` | |
setupModels = -> | |
models = require './models' | |
models | |
# Connects to the MongoDB database and returns a `connection`. | |
dbConnect = -> | |
options = | |
server: | |
socketOptions: | |
keepAlive: 1 | |
mongoose.connect(DATABASE_URI, options).connection | |
# Connect to all stored teams | |
startRTM = (controller) -> | |
controller.storage.teams.all (err, teams) -> | |
if err | |
console.error "Failed to get all teams" | |
console.error err | |
throw err | |
teams.forEach (team) -> | |
# for team in teams | |
console.log "Trying to connect to the #{team.name} team..." | |
controller.spawn(team).startRTM (err, bot) -> | |
return console.error 'Error connecting bot to Slack: ', err if err | |
console.log "Connected to the #{team.name} team." | |
trackBot bot | |
# Call this to run the thing! | |
runIt = -> | |
# Run initial setup | |
controller = setupController() | |
models = setupModels() | |
server = setupServer() | |
# Hook into all of our plugins | |
pluginMap = require './plugins' | |
plugins = [ | |
# Web related things | |
pluginMap.web.oauth | |
# Bot Commands | |
pluginMap.help | |
] | |
for plugin in plugins | |
plugin.init controller, models, server | |
# Start the web server | |
server.listen(process.env.PORT) | |
# Connect to the database | |
dbConnection = dbConnect() | |
dbConnection | |
.on 'error', console.log | |
.on 'disconnected', dbConnect | |
.once 'open', -> | |
startRTM controller | |
# It's ALIVE | |
runIt() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment