forked from keoghpe/alex-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
keoghpe@tcd.ie
committed
Sep 11, 2015
0 parents
commit 6766978
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Alex Integration For Slack | ||
|
||
Set up: | ||
|
||
``` | ||
export ALEX_TOKEN="xoxb-10582780021-4q3dhUv4wWdoFFKyesWXkxPU" | ||
node index.js | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
var Slack, autoMark, autoReconnect, slack, token; | ||
|
||
Slack = require('slack-client'); | ||
Alex = require('alex'); | ||
|
||
token = process.env.ALEX_TOKEN; | ||
|
||
autoReconnect = true; | ||
|
||
autoMark = true; | ||
|
||
slack = new Slack(token, autoReconnect, autoMark); | ||
|
||
slack.on('open', function() { | ||
var channel, channels, group, groups, id, messages, unreads; | ||
channels = []; | ||
groups = []; | ||
unreads = slack.getUnreadCount(); | ||
channels = (function() { | ||
var ref, results; | ||
ref = slack.channels; | ||
results = []; | ||
for (id in ref) { | ||
channel = ref[id]; | ||
if (channel.is_member) { | ||
results.push("#" + channel.name); | ||
} | ||
} | ||
return results; | ||
})(); | ||
groups = (function() { | ||
var ref, results; | ||
ref = slack.groups; | ||
results = []; | ||
for (id in ref) { | ||
group = ref[id]; | ||
if (group.is_open && !group.is_archived) { | ||
results.push(group.name); | ||
} | ||
} | ||
return results; | ||
})(); | ||
console.log("Welcome to Slack. You are @" + slack.self.name + " of " + slack.team.name); | ||
console.log('You are in: ' + channels.join(', ')); | ||
console.log('As well as: ' + groups.join(', ')); | ||
messages = unreads === 1 ? 'message' : 'messages'; | ||
return console.log("You have " + unreads + " unread " + messages); | ||
}); | ||
|
||
slack.on('message', function(message) { | ||
var channel, channelError, channelName, errors, response, text, textError, ts, type, typeError, user, userName; | ||
channel = slack.getChannelGroupOrDMByID(message.channel); | ||
user = slack.getUserByID(message.user); | ||
response = ''; | ||
type = message.type, ts = message.ts, text = message.text; | ||
channelName = (channel != null ? channel.is_channel : void 0) ? '#' : ''; | ||
channelName = channelName + (channel ? channel.name : 'UNKNOWN_CHANNEL'); | ||
userName = (user != null ? user.name : void 0) != null ? "@" + user.name : "UNKNOWN_USER"; | ||
console.log("Received: " + type + " " + channelName + " " + userName + " " + ts + " \"" + text + "\""); | ||
if (type === 'message' && (text != null) && (channel != null)) { | ||
|
||
var a_messages = Alex(text).messages; | ||
|
||
if(a_messages.length) { | ||
for (var i = 0; i < a_messages.length; i++) { | ||
response += Alex(text).messages[i].reason + '\n'; | ||
}; | ||
|
||
channel.send(response); | ||
return console.log("@" + slack.self.name + " responded with \"" + response + "\""); | ||
} | ||
} else { | ||
typeError = type !== 'message' ? "unexpected type " + type + "." : null; | ||
textError = text == null ? 'text was undefined.' : null; | ||
channelError = channel == null ? 'channel was undefined.' : null; | ||
errors = [typeError, textError, channelError].filter(function(element) { | ||
return element !== null; | ||
}).join(' '); | ||
return console.log("@" + slack.self.name + " could not respond. " + errors); | ||
} | ||
}); | ||
|
||
slack.on('error', function(error) { | ||
return console.error("Error: " + error); | ||
}); | ||
|
||
slack.login(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "alex-slack", | ||
"version": "1.0.0", | ||
"description": "Alex for slack", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "keoghpe", | ||
"license": "MIT", | ||
"dependencies": { | ||
"alex": "^1.1.0", | ||
"slack-client": "git+https://github.com/slackhq/node-slack-client.git" | ||
} | ||
} |