-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack-bulkinviter.py
65 lines (56 loc) · 2.04 KB
/
slack-bulkinviter.py
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
#! /usr/bin/env python3
import sys
from slacker import Slacker, Error
# Get channel name from command line
try:
channel_name = sys.argv[1].strip('\'"')
assert channel_name
except:
print("Please specify a channel name")
sys.exit(1)
# Load API key from apikey.txt
try:
with open('apikey.txt') as f:
api_key = f.read().strip()
assert api_key
except IOError:
print("Cannot find apikey.txt, or other reading error")
sys.exit(1)
except AssertionError:
print("Empty API key")
sys.exit(1)
else:
slack = Slacker(api_key)
# Get channel id from name
response = slack.conversations.list(exclude_archived=True, limit=1000, types=['private_channel', 'public_channel'])
channels = [d for d in response.body['channels'] if d['name'] == channel_name]
if not len(channels):
print("Cannot find channel") # Channel can be not found if the workspace has more than 1000 channel (conversations.list sends up to 1000 channel)
sys.exit(1)
assert len(channels) == 1
channel_id = channels[0]['id']
# Get users list
response = slack.users.list()
users = [
(u['id'], u['profile']['display_name'], u['name']) for u in response.body['members']
if u['is_bot'] == False
and u['deleted'] == False
and u['is_ultra_restricted'] == False
and u['is_restricted'] == False
]
# Invite all users to slack channel
print("***** INVITATION OF {} MEMBERS IN PROGRESS TO CHANNEL : {} *****".format(len(users), channel_name))
for user_id, user_display_name, user_name in users:
print("Inviting [ID: %s] %s (aka %s)" % (user_id, user_display_name, user_name))
try:
if user_id != 'ID_USER': # Exclude a person
slack.conversations.invite(channel_id, user_id)
print("\t --> OK")
except Error as e:
code = e.args[0]
if code == "already_in_channel":
print("{} is already in the channel".format(user_name))
elif code in ('cant_invite_self', 'cant_invite'):
print("Skipping user {} ('{}')".format(user_name, code))
else:
raise