-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
320 lines (270 loc) · 9.19 KB
/
api.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package slack
import (
"encoding/json"
"fmt"
"strconv"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// Slack consts
const (
TypeMessage = "message"
UserInviteLimit = 30
)
const (
postMessageURL = "https://slack.com/api/chat.postMessage"
conversationsCreateURL = "https://slack.com/api/conversations.create"
conversationsInfoURL = "https://slack.com/api/conversations.info"
conversationsInviteURL = "https://slack.com/api/conversations.invite"
conversationsListURL = "https://slack.com/api/conversations.list"
conversationsMembersURL = "https://slack.com/api/conversations.members"
imListURL = "https://slack.com/api/im.list"
userProfileURL = "https://slack.com/api/users.profile.get"
alreadyInChannelErrorMsg = "already_in_channel"
cantInviteSelfErrorMsg = "cant_invite_self"
)
// SendAPIMessage sends a message with Web API
func SendAPIMessage(token, chanID, message string) error {
var response struct {
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
TS string `json:"ts,omitempty"`
Message struct {
Username string `json:"username"`
BotID string `json:"bot_id"`
Type string `json:"type"`
Subtype string `json:"subtype"`
} `json:"message,omitempty"`
}
request := struct {
Conversation string `json:"channel"`
Text string `json:"text"`
AsUser bool `json:"as_user"`
Username string `json:"username"`
IconEmoji string `json:"icon_emoji"`
}{
Conversation: chanID,
Text: message,
AsUser: false,
Username: "Birthday in RnD",
IconEmoji: ":cake:",
}
body, err := json.Marshal(request)
if err != nil {
return errors.Wrap(err, "unable to marshal request")
}
headers := map[string]string{"Authorization": "Bearer " + token}
body, err = makeRequest(postMessageURL, methodPOST, contentJSON, body, nil, headers)
if err != nil {
return errors.Wrap(err, "unable to make POST request")
}
if err = json.Unmarshal(body, &response); err != nil {
return errors.Wrap(err, "unable to unmarshal response")
}
if !response.OK {
return errors.Errorf("API error: %s", response.Error)
}
return nil
}
// CreateNewConversation creates new Slack conversation and returns its ID and error, if any
func CreateNewConversation(token, conversationName string, isPrivate bool) (string, error) {
var response struct {
OK bool `json:"ok"`
Error string `json:"error"`
Conversation Conversation `json:"channel"`
}
params := map[string]string{
"token": token,
"name": conversationName,
"is_private": strconv.FormatBool(isPrivate),
}
respBody, err := makeRequest(conversationsCreateURL, methodGET, contentEncoded, nil, params, nil)
if err != nil {
return "", errors.Wrap(err, "unable to make GET request")
}
if err = json.Unmarshal(respBody, &response); err != nil {
return "", errors.Wrap(err, "unable to unmarshal response")
}
if !response.OK {
return "", errors.Errorf("API error: %s", response.Error)
}
return response.Conversation.ID, nil
}
// GetConversationInfo returns info about the Slack conversation by its ID
func GetConversationInfo(token, chanID string) (*Conversation, error) {
var response struct {
OK bool `json:"ok"`
Error string `json:"error"`
Conversation Conversation `json:"channel"`
}
params := map[string]string{"token": token, "channel": chanID}
respBody, err := makeRequest(conversationsInfoURL, methodGET, contentEncoded, nil, params, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to make GET request")
}
if err = json.Unmarshal(respBody, &response); err != nil {
return nil, errors.Wrap(err, "unable to unmarshal response")
}
if !response.OK {
return nil, errors.Errorf("API error: %s", response.Error)
}
return &response.Conversation, nil
}
// GetConversations returns info about all of the public and private Slack conversations in the workspace
func GetConversations(token string, withArchived bool) ([]Conversation, error) {
var response struct {
OK bool `json:"ok"`
Error string `json:"error"`
Conversations []Conversation `json:"channels"`
}
params := map[string]string{
"token": token,
"limit": "10000",
"types": "public_channel,private_channel",
"exclude_archived": fmt.Sprintf("%t", !withArchived),
}
respBody, err := makeRequest(conversationsListURL, methodGET, contentEncoded, nil, params, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to make GET request")
}
if err = json.Unmarshal(respBody, &response); err != nil {
return nil, errors.Wrap(err, "unable to unmarshal response")
}
if !response.OK {
return nil, errors.Errorf("API error: %s", response.Error)
}
return response.Conversations, nil
}
// GetConversationMembers returns the Slack conversation member list by conversation ID
func GetConversationMembers(token, chanID string) ([]string, error) {
var response struct {
OK bool `json:"ok"`
Error string `json:"error"`
Members []string `json:"members"`
}
params := map[string]string{"token": token, "channel": chanID}
respBody, err := makeRequest(conversationsMembersURL, methodGET, contentEncoded, nil, params, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to make GET request")
}
if err = json.Unmarshal(respBody, &response); err != nil {
return nil, errors.Wrap(err, "unable to unmarshal response")
}
if !response.OK {
return nil, errors.Errorf("API error: %s", response.Error)
}
return response.Members, nil
}
// GetUserProfile returns the Slack user's profile by user ID
func GetUserProfile(token, userID string) (*UserProfile, error) {
var response struct {
OK bool `json:"ok"`
Error string `json:"error"`
Profile UserProfile `json:"profile"`
}
params := map[string]string{"token": token, "user": userID}
respBody, err := makeRequest(userProfileURL, methodGET, contentEncoded, nil, params, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to make GET request")
}
if err = json.Unmarshal(respBody, &response); err != nil {
return nil, errors.Wrap(err, "unable to unmarshal response")
}
if !response.OK {
return nil, errors.Errorf("API error: %s", response.Error)
}
response.Profile.ID = userID
return &response.Profile, nil
}
// FindDMByUserID returns Slack IM ID for the provided user ID
func FindDMByUserID(token, userID string) (string, error) {
var response struct {
OK bool `json:"ok"`
Error string `json:"error"`
IMs []IM `json:"ims"`
}
params := map[string]string{"token": token}
respBody, err := makeRequest(imListURL, methodGET, contentEncoded, nil, params, nil)
if err != nil {
return "", errors.Wrap(err, "unable to make GET request")
}
if err = json.Unmarshal(respBody, &response); err != nil {
return "", errors.Wrap(err, "unable to unmarshal response")
}
if !response.OK {
return "", errors.Errorf("API error: %s", response.Error)
}
for _, im := range response.IMs {
if im.User == userID {
return im.ID, nil
}
}
return "", errors.New("user not found")
}
// InviteMembersToConversation adds members from the slice to the Slack conversation
func InviteMembersToConversation(token, chanID string, memberIDs []string) error {
var response struct {
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
Conversation Conversation `json:"channel,omitempty"`
Errors []struct {
User string `json:"user"`
OK bool `json:"ok"`
Error string `json:"error"`
} `json:"errors,omitempty"`
}
var request struct {
Conversation string `json:"channel"`
Users string `json:"users"`
}
request.Conversation = chanID
for _, id := range memberIDs {
request.Users = id
logrus.Debugln("Adding user", request.Users)
body, err := json.Marshal(request)
if err != nil {
return errors.Wrapf(err, "unable to marshal request for user %s", id)
}
headers := map[string]string{"Authorization": "Bearer " + token}
body, err = makeRequest(conversationsInviteURL, methodPOST, contentJSON, body, nil, headers)
if err != nil {
return errors.Wrapf(err, "unable to make POST request for user %s", id)
}
if err = json.Unmarshal(body, &response); err != nil {
return errors.Wrapf(err, "unable to unmarshal response for user %s", id)
}
if !response.OK {
if len(response.Errors) > 0 {
err = errors.Errorf("total error count: %d", len(response.Errors))
flag := false
for _, e := range response.Errors {
if !e.OK {
switch e.Error {
case alreadyInChannelErrorMsg, cantInviteSelfErrorMsg:
// ignore and do nothing
case "":
flag = true
err = errors.Wrapf(err, "user %s - unknown error", e.User)
default:
flag = true
err = errors.Wrapf(err, "user %s - %s", e.User, e.Error)
}
}
}
if flag {
return errors.Wrapf(err, "multiple API errors")
}
} else {
switch response.Error {
case alreadyInChannelErrorMsg, cantInviteSelfErrorMsg:
// ignore and do nothing
case "":
return errors.Errorf("unknown API error for user %s", id)
default:
return errors.Errorf("API error for user %s: %s", id, response.Error)
}
}
}
}
return nil
}