initial commit

This commit is contained in:
Kegan Myers 2014-08-09 01:21:11 -05:00
commit 56b99d2758
5 changed files with 132 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
node_modules
config.json
.idea

28
README.md Normal file
View file

@ -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.

64
app.js Normal file
View file

@ -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));
}));

6
config.json.example Normal file
View file

@ -0,0 +1,6 @@
{
"plugins": [
"slack-roll-command"
],
"local-plugins": []
}

31
package.json Normal file
View file

@ -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 <kegan@keganmyers.com>",
"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"
}