-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (88 loc) · 3.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var Slack, Teacher, autoMark, autoReconnect, slack, token;
Slack = require('slack-client');
Teacher = require('teacher');
token = 'xoxb-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;
type = message.type, ts = message.ts, text = message.text;
if ((text == null) || text.indexOf("#check") < 0) {
return;
}
text = text.replace('#check', '');
channel = slack.getChannelGroupOrDMByID(message.channel);
user = slack.getUserByID(message.user);
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";
response = "" + userName + ", did you mean? ";
console.log("Received: " + type + " " + channelName + " " + userName + " " + ts + " \"" + text + "\"");
if (type === 'message' && (text != null) && (channel != null)) {
Teacher.check(text, function(err, data) {
var i;
i = 0;
return data.forEach(function(word) {
var correct;
if (word.suggestions.option instanceof Array) {
correct = word.suggestions.option[0];
} else {
correct = word.suggestions.option;
}
text = text.replace(word.string, correct);
if (++i === data.length) {
return channel.send("" + response + " _" + text + "_");
}
});
});
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();