Skip to content

Instantly share code, notes, and snippets.

@sammachin
Last active January 29, 2019 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sammachin/406d984e0e10678bb74ac070ba25929b to your computer and use it in GitHub Desktop.
Save sammachin/406d984e0e10678bb74ac070ba25929b to your computer and use it in GitHub Desktop.
some functions for getting slack users in a channel and adding them to a new channel
from slackclient import SlackClient
TOKEN = "xoxp-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
sc = SlackClient(TOKEN)
#get a dict of channel names to channel IDs
def get_channels():
channels = {}
data = sc.api_call("conversations.list", types='public_channel', exclude_archived=True, limit=1000)
for c in data['channels']:
channels[c['name']] = c['id']
while data['response_metadata']['next_cursor'] != '':
data = sc.api_call("conversations.list", types='public_channel', exclude_archived=True, limit=1000, cursor=data['response_metadata']['next_cursor'])
for c in data['channels']:
channels[c['name']] = c['id']
return channels
#get user id of token holder
def my_id():
resp = sc.api_call("auth.test")
return resp['user_id']
#print name & email of user by user id
def read_user(id):
data = sc.api_call(
"users.profile.get",
user=id,
)
print("{}, {}".format(data['profile']['real_name'], data['profile']['email']))
#get a list of users in the channel
def get_users(channel):
channels = get_channels()
ch_id = channels[channel]
members = []
data = sc.api_call(
"conversations.members",
channel=ch_id,
)
members.extend(data['members'])
while data['response_metadata']['next_cursor'] != '':
data = sc.api_call(
"conversations.members",
channel=ch_id,
cursor=data['response_metadata']['next_cursor']
)
members.extend(data['members'])
members.remove(my_id())
return members
#Add a list of users to the channel named c
def add_users(c, users):
channels = get_channels()
ch_id = channels[c]
for thirty_users in [users[start:start+30] for start in range(0, len(users), 30)]:
ids = ",".join(thirty_users)
resp = sc.api_call(
"conversations.invite",
channel=ch_id,
users=ids
)
print(resp)
if resp['ok'] == False:
print(resp)
else:
print("Status: {}".format(resp['ok']))
#get a list of user IDs that are in the Big Channel and not the Smaller Channel
def missing_users(bigchannel, smallerchannel):
bigusers = get_users(bigchannel)
smallusers = get_users(smallchannel)
diff = list(set(bigusers) - set(smallusers))
return diff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment