-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (123 loc) · 3.22 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
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
'use strict'
const config = require('prefect-worker-config')
const got = require('got')
const log = config.log
module.exports = SlackAPI
function SlackAPI (accessToken, userAccessToken) {
this.client = got.extend({
baseUrl: 'https://slack.com/api/',
json: true
})
this.accessToken = accessToken
this.userAccessToken = userAccessToken
}
SlackAPI.prototype.callAPI = async function callAPI (path, options) {
try {
const response = await this.client(path, options)
if (response.body.ok) {
return [null, response.body]
} else {
log.warn({ response: response.body }, 'Got Slack error')
return [new Error(response.body.error), undefined]
}
} catch (err) {
log.warn({ err, url: err.url }, 'Got error calling Slack API')
return [err, undefined]
}
}
SlackAPI.prototype.post = async function post (path, body, token) {
if (!token) token = this.accessToken
log.info({ path, body, token }, 'About to make POST request')
return this.callAPI(path, {
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
'content-type': 'application/json;charset=utf-8'
},
body
})
}
SlackAPI.prototype.get = async function get (path, params, token) {
if (!token) token = this.accessToken
params.token = token
return this.callAPI(path, {
method: 'GET',
query: params
})
}
SlackAPI.prototype.getPaginated = async function getPaginated (
path,
params,
token
) {
const [err, result] = await this.get(path, params, token)
if (err) {
return [err, result]
}
if (result.response_metadata && result.response_metadata.next_cursor) {
const [nextErr, nextPage] = await this.getPaginated(
path,
{ ...params, cursor: result.response_metadata.next_cursor },
token
)
if (nextErr) {
return [nextErr, undefined]
}
return [null, result.concat(nextPage)]
}
return [null, result]
}
SlackAPI.prototype.getChannels = async function getChannels () {
return this.getPaginated('conversations.list', {}, this.accessToken)
}
SlackAPI.prototype.getMembers = async function getMembers () {
return this.getPaginated('users.list', {}, this.accessToken)
}
SlackAPI.prototype.filePublicUrl = async function filePublicUrl (fileId) {
// Note: this method requires a user access token.
return this.post(
'files.sharedPublicURL',
{
file: fileId
},
this.userAccessToken
)
}
SlackAPI.prototype.userInfo = async function userInfo (userId) {
return this.get('users.info', {
user: userId
})
}
SlackAPI.prototype.getPermalink = async function getPermalink (
channelId,
messageId
) {
return this.get('chat.getPermalink', {
channel: channelId,
message_ts: messageId
})
}
SlackAPI.prototype.postMessage = async function postMessage (
channel,
text,
mrkdwn,
attachments
) {
return this.post('chat.postMessage', {
channel: channel,
text: text,
attachments: attachments,
mrkdwn: mrkdwn,
as_user: false
})
}
SlackAPI.prototype.openDirectMessage = async function openDirectMessage (user) {
return this.post('conversations.open', {
users: user
})
}
SlackAPI.prototype.getBotInfo = async function getBotInfo (botId) {
return this.get('bots.info', {
bot: botId
})
}