1
1
#!/usr/bin/env python3
2
2
3
- from slackclient import SlackClient
3
+ import time
4
+
5
+ from slack_sdk import WebClient
6
+ from slack_sdk .errors import SlackApiError
4
7
5
8
# Slack API Token
6
- SLACK_TOKEN = ""
9
+ SLACK_TOKEN = "xoxb-... "
7
10
8
11
# Channel to invite users too
9
- DEST_CHANNEL = "general "
12
+ DEST_CHANNEL = "channelname "
10
13
11
14
def channel_id_by_name (client , name ):
12
- """ Fetch channel ID for a given channel name. """
15
+ """Fetch channel ID for a given channel name."""
13
16
14
17
output = client .api_call ("channels.list" )
15
- channels = output [' channels' ]
18
+ channels = output [" channels" ]
16
19
17
- channel_id = ''
18
20
for channel in channels :
19
- if channel [' name' ] == name :
20
- return channel ['id' ]
21
+ if channel [" name" ] == name :
22
+ return channel ["id" ]
21
23
22
24
return None
23
25
24
26
def get_all_users (client ):
25
- """ Fetch all users in the team. Includes deleted/deactivated users. """
27
+ """Fetch all users in the team. Includes deleted/deactivated users."""
26
28
27
29
output = client .api_call ("users.list" )
28
- return output [' members' ]
30
+ return output [" members" ]
29
31
30
- sc = SlackClient (SLACK_TOKEN )
32
+ sc = WebClient (SLACK_TOKEN )
31
33
32
34
channel_id = channel_id_by_name (sc , DEST_CHANNEL )
33
35
@@ -39,11 +41,26 @@ def get_all_users(client):
39
41
members = get_all_users (sc )
40
42
print ("[*] Found {} members." .format (len (members )))
41
43
44
+ # Join the channel, so we can invite to it
45
+ sc .api_call ("conversations.join" , json = {"channel" : channel_id })
46
+
42
47
# Invite to channel in groups of 30
43
48
# Slack limits channel invitations to 30 members per API call.
44
49
print ("[*] Inviting users." )
45
- member_ids = [member ['id' ] for member in members ]
50
+ member_ids = [member ["id" ] for member in members ]
46
51
groups = [member_ids [n :n + 30 ] for n in range (0 , len (member_ids ), 30 )]
47
52
48
53
for group in groups :
49
- sc .api_call ("conversations.invite" , channel = channel_id , users = ',' .join (group ))
54
+ try :
55
+ sc .api_call ("conversations.invite" , json = {"channel" : channel_id , "users" :"," .join (group )})
56
+ except SlackApiError as e :
57
+ # Technically we can have up to 30 errors, and some might be bad
58
+ # e.response.data["errors"] to check them individually
59
+ if "ratelimited" in str (e ):
60
+ time .sleep (1 )
61
+ elif "cant_invite_self" in str (e ):
62
+ continue
63
+ elif "already_in_channel" in str (e ):
64
+ continue
65
+ # TODO: Should we bail otherwise?
66
+ continue
0 commit comments