I'm using Python 3.7, and Slack Version 4.1.2 (Production). I want to invite a user group that is already present and has many users in it.
What I have tried in python3:
def invite_user(scid):
# invite_url = 'https://slack.com/api/channels.invite'
# invite_url = 'https://slack.com/api/groups.invite'
invite_url = 'https://slack.com/api/usergroups.update'
invite_url_data = {
'token': auth_token,
'channel': scid,
'usergroup': 'SPXXXXXX',
'name': 'testing'
}
r = requests.post(url=invite_url, data=invite_url_data, headers=header)
print(r.json())
Can someone help me get the right API to invite usergroup to a channel?
Inviting the users of a usergroup to a private channel is indeed just one API call. Before that you need to get the users of the usergroup though, which is another API call. And you can't invite yourself, so you need another API call to get your current user ID.
Here is an example script using the official python library for Slack. Note that this will work for up to 1.000 users. If your usergroups are larger you need to add them in chunks.
import slack
import os
# init slack client with access token
slack_token = os.environ['SLACK_TOKEN']
client = slack.WebClient(token=slack_token)
# get my own user ID
response = client.auth_test()
assert response['ok']
my_user_id = response['user_id']
# get members of usergroup excluding myself
response = client.usergroups_users_list(usergroup='S12345678')
assert response['ok']
users = [x for x in response['users'] if x != my_user_id]
# add members to private channel
response = client.conversations_invite(
channel='G12345678',
users = users
)
assert response['ok']
print(response)