├── .gitignore ├── images └── demo.png ├── app.json ├── package.json ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | 3 | -------------------------------------------------------------------------------- /images/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keoghpe/alex-slack/HEAD/images/demo.png -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Alex-Slack", 3 | "description": "An Alex integration for Slack", 4 | "repository": "https://github.com/keoghpe/alex-slack", 5 | "keywords": ["node", "slack", "alex"], 6 | "env": { 7 | "ALEX_TOKEN": { 8 | "description": "Your Slack bot integration token (obtainable at https://my.slack.com/services/new/bot)" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "alex-slack", 3 | "version": "1.0.0", 4 | "description": "Alex for slack", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node index.js" 9 | }, 10 | "author": "keoghpe", 11 | "license": "MIT", 12 | "dependencies": { 13 | "alex": "^1.1.0", 14 | "slack-client": "^1.4.1" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Alex Integration For Slack 2 | 3 | An [Alex](http://alexjs.com/) bot for slack that uses [slack-node-client](https://github.com/slackhq/node-slack-client). 4 | 5 | ![](./images/demo.png) 6 | 7 | ## Getting Started 8 | 1. Create a new [bot integration](https://my.slack.com/services/new/bot) 9 | 1. Follow the steps to deploy the bot to Heroku or run it locally 10 | 11 | #### One-Click Heroku 12 | Click this button: 13 | 14 | [![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy) 15 | 16 | #### Manual Heroku 17 | 1. Install [Heroku toolbelt](https://devcenter.heroku.com/articles/getting-started-with-nodejs#set-up) 18 | 1. Create a new bot integration (as above) 19 | 1. `heroku create` 20 | 1. `heroku config:set ALEX_TOKEN=[Your API token]` 21 | 1. `git push heroku master` 22 | 23 | #### To Run Locally 24 | 1. `npm install` 25 | 1. `export ALEX_TOKEN="your-bot-key"` 26 | 1. `node index.js` 27 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Slack, autoMark, autoReconnect, slack, token; 2 | 3 | Slack = require('slack-client'); 4 | Alex = require('alex'); 5 | var http = require('http'); 6 | 7 | token = process.env.ALEX_TOKEN; 8 | 9 | autoReconnect = true; 10 | 11 | autoMark = true; 12 | 13 | slack = new Slack(token, autoReconnect, autoMark); 14 | 15 | slack.on('open', function() { 16 | var channel, channels, group, groups, id, messages, unreads; 17 | channels = []; 18 | groups = []; 19 | unreads = slack.getUnreadCount(); 20 | channels = (function() { 21 | var ref, results; 22 | ref = slack.channels; 23 | results = []; 24 | for (id in ref) { 25 | channel = ref[id]; 26 | if (channel.is_member) { 27 | results.push("#" + channel.name); 28 | } 29 | } 30 | return results; 31 | })(); 32 | groups = (function() { 33 | var ref, results; 34 | ref = slack.groups; 35 | results = []; 36 | for (id in ref) { 37 | group = ref[id]; 38 | if (group.is_open && !group.is_archived) { 39 | results.push(group.name); 40 | } 41 | } 42 | return results; 43 | })(); 44 | console.log("Welcome to Slack. You are @" + slack.self.name + " of " + slack.team.name); 45 | console.log('You are in: ' + channels.join(', ')); 46 | console.log('As well as: ' + groups.join(', ')); 47 | messages = unreads === 1 ? 'message' : 'messages'; 48 | return console.log("You have " + unreads + " unread " + messages); 49 | }); 50 | 51 | slack.on('message', function(message) { 52 | var channel, channelError, channelName, errors, response, text, textError, ts, type, typeError, user, userName; 53 | channel = slack.getChannelGroupOrDMByID(message.channel); 54 | user = slack.getUserByID(message.user); 55 | response = ''; 56 | type = message.type, ts = message.ts, text = message.text; 57 | channelName = (channel != null ? channel.is_channel : void 0) ? '#' : ''; 58 | channelName = channelName + (channel ? channel.name : 'UNKNOWN_CHANNEL'); 59 | userName = (user != null ? user.name : void 0) != null ? "@" + user.name : "UNKNOWN_USER"; 60 | console.log("Received: " + type + " " + channelName + " " + userName + " " + ts + " \"" + text + "\""); 61 | if (type === 'message' && (text != null) && (channel != null)) { 62 | 63 | var a_messages = Alex(text).messages; 64 | 65 | if(a_messages.length) { 66 | for (var i = 0; i < a_messages.length; i++) { 67 | response += Alex(text).messages[i].reason + '\n'; 68 | }; 69 | 70 | channel.send(response); 71 | return console.log("@" + slack.self.name + " responded with \"" + response + "\""); 72 | } 73 | } else { 74 | typeError = type !== 'message' ? "unexpected type " + type + "." : null; 75 | textError = text == null ? 'text was undefined.' : null; 76 | channelError = channel == null ? 'channel was undefined.' : null; 77 | errors = [typeError, textError, channelError].filter(function(element) { 78 | return element !== null; 79 | }).join(' '); 80 | return console.log("@" + slack.self.name + " could not respond. " + errors); 81 | } 82 | }); 83 | 84 | slack.on('error', function(error) { 85 | return console.error("Error: " + error); 86 | }); 87 | 88 | slack.login(); 89 | 90 | // Heroku requires the process to bind to this port within 60 seconds or it is killed 91 | http.createServer(function(req, res) { 92 | res.end('ALEX_SLACK_BOT'); 93 | }).listen(process.env.PORT || 5000) 94 | --------------------------------------------------------------------------------