I\'m a bot. https://github.com/neelabhg/groupme-bot
'); 20 | } 21 | } 22 | }); 23 | 24 | var registerRoute = function (httpVerb, route, handler) { 25 | if (['get', 'post'].indexOf(httpVerb) === -1) { 26 | console.log('registerRoute: Incorrect router method \'%s\' for route \'%s\'', httpVerb, route); 27 | return; 28 | } 29 | router[httpVerb](route, function () { 30 | this.res.writeHead(200); 31 | handler(this.req.headers, this.req.body || this.req.chunks.join('')); 32 | this.res.end(); 33 | }); 34 | }; 35 | 36 | // http://stackoverflow.com/a/5365577 (node.js require all files in a folder?) 37 | // And http://stackoverflow.com/questions/5364928/node-js-require-all-files-in-a-folder#comment25520686_5365577 38 | var normalizedPath = path.join(__dirname, "servicehooks"); 39 | require("fs").readdirSync(normalizedPath).forEach(function (file) { 40 | if (path.extname(file) === '.js') { 41 | require("./servicehooks/" + file)(registerRoute); 42 | } 43 | }); 44 | 45 | server = http.createServer(function (req, res) { 46 | req.chunks = []; 47 | req.on('data', function (chunk) { 48 | req.chunks.push(chunk.toString()); 49 | }); 50 | 51 | router.dispatch(req, res, function(err) { 52 | res.writeHead(err.status, {"Content-Type": "text/plain"}); 53 | res.end(err.message); 54 | }); 55 | }); 56 | 57 | port = Number(process.env.PORT || 5000); 58 | server.listen(port); 59 | -------------------------------------------------------------------------------- /src/servicehooks/groupme.js: -------------------------------------------------------------------------------- 1 | var bot = require('../bot'); 2 | 3 | module.exports = function (registerRoute) { 4 | registerRoute('post', '/groupme', function (headers, requestBody) { 5 | bot.respond(requestBody); 6 | }); 7 | }; 8 | -------------------------------------------------------------------------------- /src/servicehooks/travis.js: -------------------------------------------------------------------------------- 1 | // http://docs.travis-ci.com/user/notifications/#Webhook-notification 2 | 3 | var bot = require('../bot'); 4 | var config = require('../config'); 5 | var crypto = require('crypto'); 6 | var util = require('util'); 7 | 8 | var isAuthorizedRequest = function (headers) { 9 | var digest = crypto.createHash('sha256').update(headers['travis-repo-slug'] + config.travisUserToken).digest('hex'); 10 | return digest === headers['authorization']; 11 | }; 12 | 13 | var getChatMessageText = function (payload) { 14 | var commit_details; 15 | if (payload.type === 'pull_request') { 16 | commit_details = util.format('pull-request #%d by %s', payload.pull_request_number, payload.author_name); 17 | } else { 18 | commit_details = util.format('commit to %s by %s', payload.branch, payload.author_name); 19 | } 20 | return util.format('Travis CI - %s - build #%d (%s): %s.\nBuild url: %s', 21 | payload.repository.name, payload.number, commit_details, payload.status_message, payload.build_url); 22 | }; 23 | 24 | module.exports = function (registerRoute) { 25 | registerRoute('post', '/travisci', function (headers, requestBody) { 26 | var payload; 27 | if (!(typeof headers === 'object' && headers && typeof requestBody === 'object' && requestBody)) { 28 | console.log('Invalid POST request for route /travisci'); 29 | return; 30 | } 31 | 32 | if (!isAuthorizedRequest(headers)) { 33 | console.log('Travis CI hook: unauthorized payload request for repository', headers['travis-repo-slug']); 34 | return; 35 | } 36 | 37 | try { 38 | payload = JSON.parse(requestBody.payload); 39 | } catch (e) { 40 | console.log('Travis hook error while parsing JSON:', e); 41 | console.log('Travis CI notification hook request body:', requestBody); 42 | return; 43 | } 44 | if (!payload) { 45 | return; 46 | } 47 | 48 | bot.postMessageWithGroupLocalID('2', getChatMessageText(payload)); 49 | }); 50 | }; 51 | -------------------------------------------------------------------------------- /src/utils.js: -------------------------------------------------------------------------------- 1 | var http = require('http'); 2 | var https = require('https'); 3 | 4 | var utils = {}; 5 | 6 | // http://stackoverflow.com/a/9577651 (HTTP GET Request in Node.js Express) 7 | utils.getHttp = function(options, onResult, onError) { 8 | var protocol = options.port == 443 ? https : http; 9 | var req = protocol.request(options, function(res) { 10 | var output = ''; 11 | //console.log(options.host + ':' + res.statusCode); 12 | res.setEncoding('utf8'); 13 | 14 | res.on('data', function (chunk) { 15 | output += chunk; 16 | }); 17 | 18 | res.on('end', function() { 19 | //var obj = JSON.parse(output); 20 | onResult(res.statusCode, output); 21 | }); 22 | }); 23 | 24 | req.on('error', function(err) { 25 | onError(err.message); 26 | }); 27 | 28 | req.end(); 29 | }; 30 | 31 | module.exports = utils; 32 | --------------------------------------------------------------------------------