forked from evanyeung/terminal-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackClient.js
executable file
·168 lines (160 loc) · 3.62 KB
/
slackClient.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const fs = require('fs');
const request = require('request');
const WebSocket = require('ws');
const HttpsProxyAgent = require('https-proxy-agent');
const TOKEN = process.env.SLACK_TOKEN;
const HTTP_PROXY = process.env.HTTP_PROXY;
const PROXY = process.env.PROXY;
if (TOKEN === undefined) {
console.log(
// eslint-disable-line no-console
'Error: SLACK_TOKEN undefined. Please add SLACK_TOKEN to the environment variables.'
);
process.exit(1);
}
const proxyUrl = url => {
return url.replace('wss://cerberus-xxxx.lb.slack-msgs.com', `ws://${PROXY}`);
};
// makes a request to slack. Adds token to query
function slackRequest(endpoint, query, callback) {
const qs = query;
qs.token = TOKEN;
request.get(
{
url: `http://${PROXY}/api/${endpoint}`,
qs
},
(error, response, data) => {
if (error) {
fs.writeFileSync('error_log.txt', error);
process.exit(1);
}
if (response.statusCode !== 200) {
fs.writeFileSync(
'error_log.txt',
`Response Error: ${response.statusCode}`
);
process.exit(1);
}
const parsedData = JSON.parse(data);
if (!parsedData.ok) {
// can't see console.logs with blessed
fs.writeFileSync('error_log.txt', `Error: ${parsedData.error}`);
process.exit(1);
}
if (callback) {
callback(error, response, data);
}
}
);
}
module.exports = {
init(callback) {
slackRequest('rtm.start', {}, (error, response, data) => {
const parsedData = JSON.parse(data);
try {
const ws = new WebSocket(proxyUrl(parsedData.url), {
agent: new HttpsProxyAgent(HTTP_PROXY)
});
callback(parsedData, ws);
} catch (e) {
fs.writeFileSync('error_log.txt', parsedData.url);
process.exit(1);
}
});
},
getChannels(callback) {
slackRequest('channels.list', {}, (error, response, data) => {
if (callback) {
callback(error, response, data);
}
});
},
joinChannel(name, callback) {
slackRequest(
'channels.join',
{
name
},
(error, response, data) => {
if (callback) {
callback(error, response, data);
}
}
);
},
getChannelHistory(id, callback) {
slackRequest(
'channels.history',
{
channel: id
},
(error, response, data) => {
if (callback) {
callback(error, response, data);
}
}
);
},
markChannel(id, timestamp, callback) {
slackRequest(
'channels.mark',
{
channel: id,
ts: timestamp
},
(error, response, data) => {
if (callback) {
callback(error, response, data);
}
}
);
},
getUsers(callback) {
slackRequest('users.list', {}, (error, response, data) => {
if (callback) {
callback(error, response, data);
}
});
},
openIm(id, callback) {
slackRequest(
'im.open',
{
user: id
},
(error, response, data) => {
if (callback) {
callback(error, response, data);
}
}
);
},
getImHistory(id, callback) {
slackRequest(
'im.history',
{
channel: id
},
(error, response, data) => {
if (callback) {
callback(error, response, data);
}
}
);
},
markIm(id, timestamp, callback) {
slackRequest(
'im.mark',
{
channel: id,
ts: timestamp
},
(error, response, data) => {
if (callback) {
callback(error, response, data);
}
}
);
}
};