-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
391 lines (325 loc) · 9.73 KB
/
main.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
const (
conversationsInviteURL = "https://slack.com/api/conversations.invite"
conversationsKickURL = "https://slack.com/api/conversations.kick"
conversationsListURL = "https://slack.com/api/conversations.list"
usersLookupByEmailURL = "https://slack.com/api/users.lookupByEmail"
actionAdd = "add"
actionRemove = "remove"
)
type (
conversationsListResponse struct {
Ok bool `json:"ok"`
Channels []channel `json:"channels"`
ResponseMetadata responseMetadata `json:"response_metadata"`
Error string `json:error`
Needed string `json:needed`
Provided string `json:provided`
}
channel struct {
ID string `json:"id"`
Name string `json:"name"`
}
responseMetadata struct {
NextCursor string `json:"next_cursor"`
}
conversationsInviteRequest struct {
ChannelID string `json:"channel"`
UserIDs string `json:"users"`
}
conversationsInviteResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
}
conversationsKickRequest struct {
ChannelID string `json:"channel"`
UserID string `json:"user"`
}
conversationsKickResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
}
usersLookupByEmailResponse struct {
Ok bool `json:"ok"`
User user `json:"user"`
Error string `json:"error"`
}
user struct {
ID string `json:"id"`
Name string `json:"name"`
}
)
// This script invites the given users to the given channels on Slack.
// Due to the oddness of the Slack API, this is accomplished via these steps:
// 1) Look up Slack user IDs by email
// 2) Query all public (private if 'private' flag is set to true) channels in the workspace and create a name -> ID mapping
// 3) For each of the given channels, invite the users (user IDs) to the channel (channel ID)
func main() {
var apiToken string
var action string
var emails string
var channelsArg string
var private bool
var debug bool
// parse flags
flag.StringVar(&apiToken, "api_token", "", "Slack OAuth Access Token")
flag.StringVar(&action, "action", "add", "'add' to invite users, 'remove' to remove users")
flag.StringVar(&emails, "emails", "", "Comma separated list of Slack user emails to invite")
flag.StringVar(&channelsArg, "channels", "", "Comma separated list of channels to invite users to")
flag.BoolVar(&private, "private", false, "Boolean flag to enable private channel invitations (requires OAuth scopes 'groups:read' and 'groups:write')")
flag.BoolVar(&debug, "debug", false, "Enables debug logging when set to true")
flag.Parse()
if apiToken == "" || emails == "" || channelsArg == "" || (action != actionAdd && action != actionRemove) {
flag.Usage()
os.Exit(1)
}
// lookup users by email
fmt.Printf("\nLooking up users ...\n")
var userIDs []string
for _, email := range strings.Split(emails, ",") {
userID, err := getUserID(apiToken, email)
if err != nil {
fmt.Printf("Error while looking up user with email %s: %s\n", email, err)
continue
}
fmt.Printf("Valid user (ID: %s) found for '%s'\n", userID, email)
userIDs = append(userIDs, userID)
}
if len(userIDs) == 0 {
fmt.Println("\nNo users found - aborting")
return
}
// get all channels
channelNameToIDMap, err := getChannels(apiToken, private, debug)
if err != nil {
panic(err)
}
if debug {
fmt.Printf("DEBUG: Total # of channels retrieved: %d\n", len(channelNameToIDMap))
}
// invite/remove users to each channel
if action == actionAdd {
fmt.Printf("\nInviting users to channels ...\n")
} else {
fmt.Printf("\nRemoving users from channels ...\n")
}
channels := strings.Split(channelsArg, ",")
for _, channel := range channels {
channelID := channelNameToIDMap[channel]
if channelID == "" {
fmt.Printf("Channel '%s' not found -- skipping\n", channel)
continue
}
if action == actionAdd {
err := inviteUsersToChannel(apiToken, userIDs, channelID)
if err != nil {
fmt.Printf("Error while inviting users to %s (%s): %s\n", channel, channelID, err)
continue
}
} else {
err := removeUsersFromChannel(apiToken, userIDs, channelID, debug)
if err != nil {
fmt.Printf("Error while removing users from %s (%s): %s\n", channel, channelID, err)
continue
}
}
if action == actionAdd {
fmt.Printf("Users invited to '%s'\n", channel)
} else {
fmt.Printf("Users removed from '%s'\n", channel)
}
}
fmt.Println("\nAll done! You're welcome =)")
}
func getUserID(apiToken, userEmail string) (string, error) {
httpClient := &http.Client{}
// lookup user by email
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(usersLookupByEmailURL+"?email=%s", userEmail), nil)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiToken))
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err := printErrorResponseBody(resp)
if err != nil {
return "", err
}
return "", fmt.Errorf("Non-200 status code (%d)", resp.StatusCode)
}
var data usersLookupByEmailResponse
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return "", err
}
if !data.Ok {
fmt.Printf("usersLookupByEmailResponse: %+v\n", data)
return "", fmt.Errorf("Non-ok response while looking up user by email")
}
// return user ID
return data.User.ID, nil
}
func getChannels(apiToken string, private bool, debug bool) (map[string]string, error) {
channelType := "public_channel"
if private {
channelType = "private_channel"
}
nameToID := make(map[string]string)
httpClient := &http.Client{}
var nextCursor string
for {
// query list of channels
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(conversationsListURL+"?cursor=%s&exclude_archived=true&limit=200&types=%s", nextCursor, channelType), nil)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiToken))
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err := printErrorResponseBody(resp)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("Non-200 status code (%d)", resp.StatusCode)
}
var data conversationsListResponse
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, err
}
if !data.Ok {
fmt.Printf("conversationsListResponse: %+v", data)
return nil, fmt.Errorf("Non-ok response while querying list of channels")
}
if debug {
fmt.Printf("DEBUG: # of channels returned in page: %d\n", len(data.Channels))
}
// map of channel names to IDs
for _, channel := range data.Channels {
nameToID[channel.Name] = channel.ID
}
// paginate if necessary
nextCursor = data.ResponseMetadata.NextCursor
if nextCursor == "" {
break
}
}
return nameToID, nil
}
func inviteUsersToChannel(apiToken string, userIDs []string, channelID string) error {
httpClient := &http.Client{}
reqBody, err := json.Marshal(conversationsInviteRequest{
ChannelID: channelID,
UserIDs: strings.Join(userIDs, ","),
})
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, conversationsInviteURL, bytes.NewReader(reqBody))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiToken))
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err := printErrorResponseBody(resp)
if err != nil {
return err
}
return fmt.Errorf("Non-200 status code: (%d)", resp.StatusCode)
}
var data conversationsInviteResponse
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return err
}
if !data.Ok {
fmt.Printf("conversationsInviteResponse: %+v\n", data)
return fmt.Errorf("Non-ok response while inviting user to channel")
}
return nil
}
func removeUsersFromChannel(apiToken string, userIDs []string, channelID string, debug bool) error {
// API only supports removing users one at a time ...
for _, userID := range userIDs {
err := removeUserFromChannel(apiToken, userID, channelID)
if err != nil {
if debug {
fmt.Printf("DEBUG: Error while removing user %s from channel %s: %s\n", userID, channelID, err)
}
return err
}
}
return nil
}
func removeUserFromChannel(apiToken string, userID string, channelID string) error {
httpClient := &http.Client{}
reqBody, err := json.Marshal(conversationsKickRequest{
ChannelID: channelID,
UserID: userID,
})
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, conversationsKickURL, bytes.NewReader(reqBody))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiToken))
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err := printErrorResponseBody(resp)
if err != nil {
return err
}
return fmt.Errorf("Non-200 status code: (%d)", resp.StatusCode)
}
var data conversationsKickResponse
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return err
}
if !data.Ok {
fmt.Printf("conversationsKickResponse: %+v\n", data)
return fmt.Errorf("Non-ok response while removing user from channel")
}
return nil
}
func printErrorResponseBody(resp *http.Response) error {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Println(string(bodyBytes))
return nil
}