-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
68 lines (57 loc) · 2.29 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
var Client = require('slack-client'),
request = require('request');
module.exports = Client;
// Create a new bot at https://YOURSLACK.slack.com/services/new/bot
var BOT_TOKEN = 'YOURSLACK-BOT-TOKEN',
REPO_OWNER = 'augbog',
REPO_NAME = 'slack-github-issue';
var slack = new Client(BOT_TOKEN, true, true);
slack.on('open', function () {
var channels = Object.keys(slack.channels)
.map(function (k) { return slack.channels[k]; })
.filter(function (c) { return c.is_member; })
.map(function (c) { return c.name; });
var groups = Object.keys(slack.groups)
.map(function (k) { return slack.groups[k]; })
.filter(function (g) { return g.is_open && !g.is_archived; })
.map(function (g) { return g.name; });
console.log('Welcome to Slack. You are ' + slack.self.name + ' of ' + slack.team.name);
if (channels.length > 0) {
console.log('You are in: ' + channels.join(', '));
}
else {
console.log('You are not in any channels.');
}
if (groups.length > 0) {
console.log('As well as: ' + groups.join(', '));
}
});
// when someone posts to the channel
slack.on('message', function(message) {
var channel = slack.getChannelGroupOrDMByID(message.channel);
var user = slack.getUserByID(message.user);
// if we find a #...
if (message.type === 'message' && message.hasOwnProperty('text') && message.text.indexOf('#') > -1) {
var issueNum = message.text.substr(message.text.indexOf('#')).split(' ')[0];
if (/^#\d+$/.test(issueNum)) {
var issueDescription,
options = {
url: 'https://api.github.com/repos/' + REPO_OWNER +'/' + REPO_NAME + '/issues/' + issueNum.substr(1),
method: 'GET',
headers: {
'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded'
}
};
//Github API requires User Agent
request(options, function (error, response, body) {
var json = JSON.parse(body);
if (!error && response.statusCode == 200) {
issueDescription = "[#" + json.number + "] " + json.title + "\n " + json.html_url;
channel.send(issueDescription)
}
});
}
}
});
slack.login();