├── .gitignore ├── .jshintrc ├── .travis.yml ├── .yo-rc.json ├── Gruntfile.js ├── README.md ├── lib └── slackbot.js ├── package.json └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "curly": true, 3 | "eqeqeq": true, 4 | "immed": true, 5 | "latedef": true, 6 | "newcap": true, 7 | "noarg": true, 8 | "sub": true, 9 | "undef": true, 10 | "unused": true, 11 | "boss": true, 12 | "eqnull": true, 13 | "node": true 14 | } 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | -------------------------------------------------------------------------------- /.yo-rc.json: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = function (grunt) { 3 | // Show elapsed time at the end 4 | require('time-grunt')(grunt); 5 | // Load all grunt tasks 6 | require('load-grunt-tasks')(grunt); 7 | 8 | grunt.initConfig({ 9 | jshint: { 10 | options: { 11 | jshintrc: '.jshintrc', 12 | reporter: require('jshint-stylish') 13 | }, 14 | js: { 15 | src: ['*.js'] 16 | }, 17 | test: { 18 | src: ['test/**/*.js'] 19 | } 20 | }, 21 | mochacli: { 22 | options: { 23 | reporter: 'nyan', 24 | bail: true 25 | }, 26 | all: ['test/*.js'] 27 | }, 28 | watch: { 29 | gruntfile: { 30 | files: '<%= jshint.gruntfile.src %>', 31 | tasks: ['jshint:gruntfile'] 32 | }, 33 | lib: { 34 | files: '<%= jshint.lib.src %>', 35 | tasks: ['jshint:lib', 'mochacli'] 36 | }, 37 | test: { 38 | files: '<%= jshint.test.src %>', 39 | tasks: ['jshint:test', 'mochacli'] 40 | } 41 | } 42 | }); 43 | 44 | grunt.registerTask('default', ['jshint', 'mochacli']); 45 | }; 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [![Build Status](https://secure.travis-ci.org/rmcdaniel/node-slackbot.png?branch=master)](http://travis-ci.org/rmcdaniel/node-slackbot) 2 | 3 | > Slackbot for node.js using RTM API. 4 | 5 | 6 | ## Install 7 | 8 | ```sh 9 | $ npm install --save node-slackbot 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | var slackbot = require('node-slackbot'); 17 | 18 | var bot = new slackbot('token_goes_here'); 19 | 20 | bot.use(function(message, cb) { 21 | if ('message' == message.type) { 22 | console.log(message.user + ' said: ' + message.text); 23 | } 24 | cb(); 25 | }); 26 | 27 | bot.connect(); 28 | ``` 29 | 30 | ## License 31 | 32 | MIT © [Richard McDaniel]() 33 | -------------------------------------------------------------------------------- /lib/slackbot.js: -------------------------------------------------------------------------------- 1 | var async = require('async'), 2 | https = require('https'), 3 | querystring = require('querystring'), 4 | ws = require('ws'); 5 | 6 | function slackbot(token) { 7 | this.token = token; 8 | this.handlers = []; 9 | this.messageID = 0; 10 | return this; 11 | } 12 | 13 | slackbot.prototype.api = function(method, params, cb) { 14 | var options, post_data, req; 15 | 16 | params['token'] = this.token; 17 | post_data = querystring.stringify(params); 18 | 19 | options = { 20 | hostname: 'api.slack.com', 21 | method: 'POST', 22 | path: '/api/' + method, 23 | headers: { 24 | 'Content-Type': 'application/x-www-form-urlencoded', 25 | 'Content-Length': post_data.length 26 | } 27 | }; 28 | 29 | req = https.request(options); 30 | 31 | req.on('response', function(res) { 32 | var buffer; 33 | buffer = ''; 34 | res.on('data', function(chunk) { 35 | return buffer += chunk; 36 | }); 37 | return res.on('end', function() { 38 | var value; 39 | if (cb != null) { 40 | if (res.statusCode === 200) { 41 | value = JSON.parse(buffer); 42 | return cb(null, value); 43 | } else { 44 | return cb({ 45 | 'ok': false, 46 | 'error': 'API response: ' + res.statusCode 47 | }, null); 48 | } 49 | } 50 | }); 51 | }); 52 | 53 | req.on('error', function(error) { 54 | if (cb != null) { 55 | return cb({ 56 | 'ok': false, 57 | 'error': error.errno 58 | }, null); 59 | } 60 | }); 61 | 62 | req.write(post_data); 63 | return req.end(); 64 | }; 65 | 66 | slackbot.prototype.use = function(fn) { 67 | this.handlers.push(fn); 68 | return this; 69 | }; 70 | 71 | slackbot.prototype.handle = function(data) { 72 | async.series(this.handlers.map(function(fn) { 73 | return function(cb) { 74 | fn(data, cb); 75 | }; 76 | })); 77 | return this; 78 | }; 79 | 80 | slackbot.prototype.sendMessage = function(channel, text) { 81 | var message = { 82 | id: ++this.messageID, 83 | type: 'message', 84 | channel: channel, 85 | text: text 86 | }; 87 | return this.ws.send(JSON.stringify(message)); 88 | }; 89 | 90 | slackbot.prototype.connect = function() { 91 | var self = this; 92 | self.api('rtm.start', {agent: 'node-slack'}, function(err, data) { 93 | self.ws = new ws(data.url); 94 | self.ws.on('message', function(data, flags) { 95 | var message = JSON.parse(data); 96 | self.handle(message); 97 | }); 98 | }); 99 | }; 100 | 101 | module.exports = slackbot; 102 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-slackbot", 3 | "main": "./lib/slackbot.js", 4 | "version": "0.0.8", 5 | "description": "Slackbot for node.js using RTM API.", 6 | "author": { 7 | "name": "Richard McDaniel", 8 | "email": "richard.mcdaniel@uah.edu" 9 | }, 10 | "repository": "rmcdaniel/node-slackbot", 11 | "license": "MIT", 12 | "keywords": [ 13 | "node-slackbot", 14 | "slack", 15 | "bot", 16 | "slackbot" 17 | ], 18 | "dependencies": { 19 | "async": "^0.9.0", 20 | "querystring": "^0.2.0", 21 | "ws": "^0.6.3" 22 | }, 23 | "devDependencies": { 24 | "grunt-cli": "^0.1.13", 25 | "grunt-contrib-jshint": "^0.10.0", 26 | "grunt-contrib-nodeunit": "^0.4.1", 27 | "grunt-contrib-watch": "^0.6.1", 28 | "load-grunt-tasks": "^1.0.0", 29 | "time-grunt": "^1.0.0", 30 | "grunt-mocha-cli": "^1.11.0", 31 | "jshint-stylish": "^1.0.0", 32 | "browserify": "^7.0.0" 33 | }, 34 | "scripts": { 35 | "test": "grunt", 36 | "browser": "browserify index.js > browser.js" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | /*global describe, it */ 2 | 'use strict'; 3 | var assert = require('assert'); 4 | var slackbot = require('../'); 5 | 6 | describe('node-slackbot node module', function () { 7 | it('must return itself', function () { 8 | var self = new slackbot(); 9 | assert(self instanceof slackbot, 'slackbot returned something other than itself'); 10 | }); 11 | }); 12 | --------------------------------------------------------------------------------