Skip to content

Commit 05a9fee

Browse files
authored
Update invite script to use new API
1 parent bb77de7 commit 05a9fee

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

purge/invite_all_users.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
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

1114
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."""
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

2426
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."""
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

3234
channel_id = channel_id_by_name(sc, DEST_CHANNEL)
3335

@@ -39,11 +41,26 @@ def get_all_users(client):
3941
members = get_all_users(sc)
4042
print("[*] 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.
4449
print("[*] Inviting users.")
45-
member_ids = [member['id'] for member in members]
50+
member_ids = [member["id"] for member in members]
4651
groups = [member_ids[n:n+30] for n in range(0, len(member_ids), 30)]
4752

4853
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

Comments
 (0)