Created
July 28, 2016 16:47
-
-
Save malonehedges/efb709f4304b6b81909433ace0ada35d 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
var Botkit, DATABASE_URI, Promise, _, bodyParser, bots, dbConnect, express, mongoStorage, mongoose, path, runIt, setupController, setupModels, setupServer, startRTM, trackBot; | |
_ = 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 || process.env.MONGOHQ_URL; | |
bots = {}; | |
trackBot = function(bot) { | |
return bots[bot.config.token] = bot; | |
}; | |
setupController = function() { | |
var controller; | |
controller = Botkit.slackbot({ | |
retry: Infinity, | |
interactive_replies: true, | |
storage: mongoStorage({ | |
mongoUri: DATABASE_URI | |
}) | |
}); | |
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'] | |
}); | |
controller.on('create_bot', function(bot, config) { | |
if (bots[bot.config.token] != null) { | |
} else { | |
return bot.startRTM(function(err) { | |
if (err) { | |
return console.error(err); | |
} | |
trackBot(bot); | |
return bot.startPrivateConversation({ | |
user: config.createdBy | |
}, function(err, convo) { | |
if (err) { | |
return console.error(err); | |
} | |
convo.say('I just joined your team!'); | |
convo.say('You need to /invite me to a channel so I can be helpful.'); | |
return convo.next(); | |
}); | |
}); | |
} | |
}); | |
controller.on('rtm_close', function(bot) { | |
return setTimeout(bot.startRTM, 3000); | |
}); | |
return controller; | |
}; | |
setupServer = function() { | |
var server; | |
server = express(); | |
server.use(bodyParser.json()); | |
server.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
server.set('view engine', 'jade'); | |
server.set('views', path.join(__dirname, 'web')); | |
server.locals.moment = require('moment'); | |
server.locals.arrayUtils = require('./util/arrayUtils'); | |
return server; | |
}; | |
setupModels = function() { | |
var models; | |
models = require('./models'); | |
return models; | |
}; | |
dbConnect = function() { | |
var options; | |
options = { | |
server: { | |
socketOptions: { | |
keepAlive: 1 | |
} | |
} | |
}; | |
return mongoose.connect(DATABASE_URI, options).connection; | |
}; | |
startRTM = function(controller) { | |
return controller.storage.teams.all(function(err, teams) { | |
if (err) { | |
console.error("Failed to get all teams"); | |
console.error(err); | |
throw err; | |
} | |
return teams.forEach(function(team) { | |
console.log("Trying to connect to the " + team.name + " team..."); | |
return controller.spawn(team).startRTM(function(err, bot) { | |
if (err) { | |
return console.error('Error connecting bot to Slack: ', err); | |
} | |
console.log("Connected to the " + team.name + " team."); | |
return trackBot(bot); | |
}); | |
}); | |
}); | |
}; | |
runIt = function() { | |
var controller, dbConnection, i, len, models, plugin, pluginMap, plugins, server; | |
controller = setupController(); | |
models = setupModels(); | |
server = setupServer(); | |
pluginMap = require('./plugins'); | |
plugins = [pluginMap.web.oauth, pluginMap.help]; | |
for (i = 0, len = plugins.length; i < len; i++) { | |
plugin = plugins[i]; | |
plugin.init(controller, models, server); | |
} | |
server.listen(process.env.PORT); | |
dbConnection = dbConnect(); | |
return dbConnection.on('error', console.log).on('disconnected', dbConnect).once('open', function() { | |
return startRTM(controller); | |
}); | |
}; | |
runIt(); | |
// --- | |
// generated by coffee-script 1.9.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment