From 56b99d275872545e09b09feacdb8da7f6956f215 Mon Sep 17 00:00:00 2001 From: Kegan Myers Date: Sat, 9 Aug 2014 01:21:11 -0500 Subject: [PATCH] initial commit --- .gitignore | 3 +++ README.md | 28 ++++++++++++++++++++ app.js | 64 +++++++++++++++++++++++++++++++++++++++++++++ config.json.example | 6 +++++ package.json | 31 ++++++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 app.js create mode 100644 config.json.example create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1a104e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +config.json +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..534cfeb --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +slack-command-server +==================== +Slack command server is a simple way to build a server that responds to slack slash commands. + +Configuration +============= +Configuring the server is simple, you can use any plugin compatible with [slack-command-router](https://github.com/terribly-lazy/slack-command-router), custom or otherwise. + +To use pre-written plugins write your configuration thusly: +```json +{ + "plugins": [ + "slack-roll-command" + ] +} +``` +Or if you have plugins of your own, you can do this: +```json +{ + "local-plugins": [ + "directory/to/plugin.js" + ] +} +``` +Notes +===== +Any `plugins` specified in your configuration will be installed through npm upon starting your server, and any +`local-plugins` will will be loaded relative to the directory in which `app.js` resides. \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..bcd6f79 --- /dev/null +++ b/app.js @@ -0,0 +1,64 @@ +var connect = require('connect'), + SlackIncoming = require('slack-command-router'), + path = require('path'), + fs = require('fs'); + +var config = JSON.parse(fs.readFileSync("config.json")), + slack = new SlackIncoming(); + +var pluginsToInstall = config.plugins || [], + pluginsToLoad = config["local-plugins"] || []; + +function installPlugins(callback) { + if (pluginsToInstall.length === 0) { + callback([]); + return; + } + var npm = require('npm'); + npm.load({}, function (err) { + if (err) throw err; + npm.commands["install"](pluginsToInstall, function (err) { + if (err) throw err; + var loadedPlugins = []; + for (var i = 0; i < pluginsToInstall.length; i++) { + loadedPlugins.push(path.resolve(__dirname, require(pluginsToInstall[i]))); + } + callback(loadedPlugins); + }); + }); +} + +function loadPlugins(callback) { + var loadedPlugins = []; + for (var i = 0; i < pluginsToLoad.length; i++) { + pluginsToLoad.push(require(pluginsToLoad[i])); + } + callback(loadedPlugins); +} + +function initPlugins(plugins, callback) { + for (var i = 0; i < plugins.length; i++) { + slack.registerHandler(plugins[i]); + } + callback(); +} + +function initCallback(callback) { + return function (loadedPlugins) { + initPlugins(loadedPlugins, callback); + } +} + +function startApp() { + var app = connect(); + app.use(slack.getMiddleware()); + app.use(function (req, res) { + res.writeHead(400); + res.end("Unable to handle request"); + }); + app.listen(2354); +} + +installPlugins(initCallback(function() { + loadPlugins(initCallback(startApp)); +})); diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..ccf3125 --- /dev/null +++ b/config.json.example @@ -0,0 +1,6 @@ +{ + "plugins": [ + "slack-roll-command" + ], + "local-plugins": [] +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..845bc3f --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "slack-command-server", + "version": "0.0.0", + "description": "A slack slash command hander framework", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/terribly-lazy/slack-command-server.git" + }, + "keywords": [ + "slack", + "slash", + "command", + "webhooks" + ], + "author": "Kegan Myers ", + "license": "BSD-2-Clause", + "dependencies": { + "connect": "~3.1.0", + "npm": "^1.4.23", + "require-directory": "~2.0.0", + "slack-command-router": "0.0.0" + }, + "bugs": { + "url": "https://github.com/terribly-lazy/slack-command-server/issues" + }, + "homepage": "https://github.com/terribly-lazy/slack-command-server" +}