├── .gitignore
├── Slack-Github-Bot.png
├── README.md
├── package.json
└── index.js
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules
--------------------------------------------------------------------------------
/Slack-Github-Bot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/augbog/slack-github-issue/HEAD/Slack-Github-Bot.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # slack-github-issue
2 | Code for Slack bot to auto comment the issue information for a specific repository when a user types the issue number.
3 |
4 | Makes use of [node-slack-client](https://github.com/slackhq/node-slack-client).
5 |
6 |
7 |
8 | ## How to run
9 | 1. Clone repo into your local machine (`https://github.com/augbog/slack-github-issue.git`)
10 | 2. Navigate to folder (`cd slack-github-issue`)
11 | 3. Run `npm install`
12 | 4. Go to `index.js` and update `BOT_TOKEN`, `REPO_OWNER`, and `REPO_NAME`
13 | 5. Run `node index.js`
14 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "slack-github-issue",
3 | "version": "0.0.0",
4 | "description": "Code for Slack bot to auto comment the issue information for a specific repository when a user types the issue number.",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "https://github.com/augbog/slack-github-issue"
12 | },
13 | "author": "Augustus Yuan ",
14 | "license": "ISC",
15 | "bugs": {
16 | "url": "https://github.com/augbog/slack-github-issue/issues"
17 | },
18 | "homepage": "https://github.com/augbog/slack-github-issue",
19 | "devDependencies": {
20 | "slack-client": "~1.4.1",
21 | "request": "~2.58.0"
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | var Client = require('slack-client'),
2 | request = require('request');
3 |
4 | module.exports = Client;
5 |
6 | // Create a new bot at https://YOURSLACK.slack.com/services/new/bot
7 | var BOT_TOKEN = 'YOURSLACK-BOT-TOKEN',
8 | REPO_OWNER = 'augbog',
9 | REPO_NAME = 'slack-github-issue';
10 |
11 | var slack = new Client(BOT_TOKEN, true, true);
12 |
13 | slack.on('open', function () {
14 | var channels = Object.keys(slack.channels)
15 | .map(function (k) { return slack.channels[k]; })
16 | .filter(function (c) { return c.is_member; })
17 | .map(function (c) { return c.name; });
18 |
19 | var groups = Object.keys(slack.groups)
20 | .map(function (k) { return slack.groups[k]; })
21 | .filter(function (g) { return g.is_open && !g.is_archived; })
22 | .map(function (g) { return g.name; });
23 |
24 | console.log('Welcome to Slack. You are ' + slack.self.name + ' of ' + slack.team.name);
25 |
26 | if (channels.length > 0) {
27 | console.log('You are in: ' + channels.join(', '));
28 | }
29 | else {
30 | console.log('You are not in any channels.');
31 | }
32 |
33 | if (groups.length > 0) {
34 | console.log('As well as: ' + groups.join(', '));
35 | }
36 | });
37 |
38 | // when someone posts to the channel
39 | slack.on('message', function(message) {
40 | var channel = slack.getChannelGroupOrDMByID(message.channel);
41 | var user = slack.getUserByID(message.user);
42 | // if we find a #...
43 | if (message.type === 'message' && message.hasOwnProperty('text') && message.text.indexOf('#') > -1) {
44 | var issueNum = message.text.substr(message.text.indexOf('#')).split(' ')[0];
45 | if (/^#\d+$/.test(issueNum)) {
46 | var issueDescription,
47 | options = {
48 | url: 'https://api.github.com/repos/' + REPO_OWNER +'/' + REPO_NAME + '/issues/' + issueNum.substr(1),
49 | method: 'GET',
50 | headers: {
51 | 'User-Agent': 'Super Agent/0.0.1',
52 | 'Content-Type': 'application/x-www-form-urlencoded'
53 | }
54 | };
55 |
56 | //Github API requires User Agent
57 | request(options, function (error, response, body) {
58 | var json = JSON.parse(body);
59 | if (!error && response.statusCode == 200) {
60 | issueDescription = "[#" + json.number + "] " + json.title + "\n " + json.html_url;
61 | channel.send(issueDescription)
62 | }
63 | });
64 | }
65 | }
66 | });
67 |
68 | slack.login();
--------------------------------------------------------------------------------