11#!/usr/bin/env python3
22
3- from slackclient import SlackClient
3+ import time
4+
5+ from slack_sdk import WebClient
6+ from slack_sdk .errors import SlackApiError
47
58# Slack API Token
6- SLACK_TOKEN = ""
9+ SLACK_TOKEN = "xoxb-... "
710
811# Channel to invite users too
9- DEST_CHANNEL = "general "
12+ DEST_CHANNEL = "channelname "
1013
1114def channel_id_by_name (client , name ):
12- """ Fetch channel ID for a given channel name. """
15+ """Fetch channel ID for a given channel name."""
1316
1417 output = client .api_call ("channels.list" )
15- channels = output [' channels' ]
18+ channels = output [" channels" ]
1619
17- channel_id = ''
1820 for channel in channels :
19- if channel [' name' ] == name :
20- return channel ['id' ]
21+ if channel [" name" ] == name :
22+ return channel ["id" ]
2123
2224 return None
2325
2426def 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."""
2628
2729 output = client .api_call ("users.list" )
28- return output [' members' ]
30+ return output [" members" ]
2931
30- sc = SlackClient (SLACK_TOKEN )
32+ sc = WebClient (SLACK_TOKEN )
3133
3234channel_id = channel_id_by_name (sc , DEST_CHANNEL )
3335
@@ -39,11 +41,26 @@ def get_all_users(client):
3941members = get_all_users (sc )
4042print ("[*] Found {} members." .format (len (members )))
4143
44+ # Join the channel, so we can invite to it
45+ sc .api_call ("conversations.join" , json = {"channel" : channel_id })
46+
4247# Invite to channel in groups of 30
4348# Slack limits channel invitations to 30 members per API call.
4449print ("[*] Inviting users." )
45- member_ids = [member ['id' ] for member in members ]
50+ member_ids = [member ["id" ] for member in members ]
4651groups = [member_ids [n :n + 30 ] for n in range (0 , len (member_ids ), 30 )]
4752
4853for 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