Created
March 19, 2024 17:00
-
-
Save irvingpop/496ae39fd8a66ae8468a8797161fc1a7 to your computer and use it in GitHub Desktop.
Join an app, bot or any named user to all private channels in a Slack team
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
# Your LEGACY Slack API token | |
SLACK_API_TOKEN = "xoxp-my-slack-legacy-token" | |
def get_channel_list(): | |
""" | |
Retrieves a list of available channels using the Slack API. | |
""" | |
url = "https://slack.com/api/conversations.list" | |
headers = { | |
"Authorization": f"Bearer {SLACK_API_TOKEN}" | |
} | |
params = { | |
"types": "private_channel" # Include both public and private channels | |
} | |
response = requests.get(url, headers=headers, params=params) | |
data = response.json() | |
if data.get("ok"): | |
channels = data.get("channels", []) | |
return channels | |
else: | |
print(f"Error retrieving channel list: {data.get('error', 'Unknown error')}") | |
def invite_user_to_channel(channel_id, user_id): | |
url = "https://slack.com/api/conversations.invite" | |
headers = { | |
"Authorization": f"Bearer {SLACK_API_TOKEN}" | |
} | |
params = { | |
"channel": channel_id, | |
"users": user_id | |
} | |
response = requests.post(url, headers=headers, params=params) | |
data = response.json() | |
if data.get("ok"): | |
print(f"Successfully invited users to channel {channel_id}") | |
else: | |
print(f"Error inviting users: {data.get('error', 'Unknown error')}") | |
def join_channels(channel_list): | |
""" | |
Joins the app to the specified channels. | |
""" | |
for channel in channel_list: | |
if channel['name'] == 'admins-cantina': | |
continue | |
print(f"Joining channel: {channel['name']}: {channel['id']}") | |
invite_user_to_channel(channel['id'], "U06Q1PH2X40") | |
if __name__ == "__main__": | |
channel_list = get_channel_list() | |
if channel_list: | |
join_channels(channel_list) | |
else: | |
print("No channels found.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment