-
Notifications
You must be signed in to change notification settings - Fork 78
/
slackClient.js
executable file
·123 lines (116 loc) · 2.9 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
const fs = require('fs');
const request = require('request');
const WebSocket = require('ws');
const TOKEN = process.env.SLACK_TOKEN;
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);
}
// makes a request to slack. Adds token to query
function slackRequest(endpoint, query, callback) {
const qs = query;
qs.token = TOKEN;
request.get({
url: `https://slack.com/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);
const ws = new WebSocket(parsedData.url);
callback(parsedData, ws);
});
},
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);
}
});
},
};