This repository has been archived by the owner on Dec 8, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
slack-client.js
99 lines (80 loc) · 2.76 KB
/
slack-client.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
99
/* jshint node: true */
'use strict';
var http = require('http');
var https = require('https');
var querystring = require('querystring');
var url = require('url');
module.exports = SlackClient;
function SlackClient(robotSlackClient, config) {
this.client = robotSlackClient;
this.timeout = config.slackTimeout;
this.successReaction = config.successReaction;
this.baseurl = url.parse(config.slackApiBaseUrl || SlackClient.API_BASE_URL);
this.requestFactory = (this.baseurl.protocol === 'https:') ? https : http;
}
SlackClient.API_BASE_URL = 'https://slack.com/api/';
// From: https://api.slack.com/events/reaction_added
// May get this directly from a future version of the slack-client package.
SlackClient.REACTION_ADDED = 'reaction_added';
SlackClient.prototype.getChannelName = function(channelId) {
return this.client.getChannelByID(channelId).name;
};
SlackClient.prototype.getTeamDomain = function() {
return this.client.team.domain;
};
SlackClient.prototype.getReactions = function(channel, timestamp) {
return makeApiCall(this, 'reactions.get',
{ channel: channel, timestamp: timestamp });
};
SlackClient.prototype.addSuccessReaction = function(channel, timestamp) {
return makeApiCall(this, 'reactions.add',
{ channel: channel, timestamp: timestamp, name: this.successReaction });
};
function getHttpOptions(client, method, queryParams) {
var baseurl = client.baseurl;
return {
protocol: baseurl.protocol,
host: baseurl.hostname,
port: baseurl.port,
path: baseurl.pathname + method + '?' + querystring.stringify(queryParams),
method: 'GET'
};
}
function makeApiCall(client, method, params) {
return new Promise(function(resolve, reject) {
var httpOptions, req;
params.token = process.env.HUBOT_SLACK_TOKEN;
httpOptions = getHttpOptions(client, method, params);
req = client.requestFactory.request(httpOptions, function(res) {
handleResponse(method, res, resolve, reject);
});
req.setTimeout(client.timeout);
req.on('error', function(err) {
reject(new Error('failed to make Slack API request for method ' +
method + ': ' + err.message));
});
req.end();
});
}
function handleResponse(method, res, resolve, reject) {
var result = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
result = result + chunk;
});
res.on('end', function() {
var parsed;
if (res.statusCode >= 200 && res.statusCode < 300) {
parsed = JSON.parse(result);
if (parsed.ok) {
resolve(parsed);
} else {
reject(new Error('Slack API method ' + method + ' failed: ' +
parsed.error));
}
} else {
reject(new Error('received ' + res.statusCode +
' response from Slack API method ' + method + ': ' + result));
}
});
}