-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack_channel_workspace.py
122 lines (100 loc) · 4.75 KB
/
slack_channel_workspace.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
Script: Slack Add or Remove Channels to/from Workspaces
Description:
This script adds or removes Slack channels to/from workspaces using the `admin.conversations.setTeams` API method.
Users can provide data either via a JSON file or manual input. The script updates the teams for each provided channel ID.
CSV File Structure (if applicable):
- Headers: channel_id, current_team_id, target_team_ids
Functions:
- get_input: Prompts for user input.
- update_channels_from_json: Updates channels based on data provided in a JSON file.
- update_channel_teams: Updates the teams for a specified channel.
- main: Main function to drive the script based on user input.
Usage:
1. Run the script.
2. Enter the path to your Slack token file when prompted.
3. Choose whether to provide data via JSON or manual input.
4. Provide the required data (JSON path or manual input details).
5. The script will update the channels as specified.
Notes:
- Ensure that the Slack token has the necessary permissions to update channel teams.
- Handle the Slack token securely and do not expose it in the code.
- Customize the input prompts and error handling as needed for your organization.
Author: Chad Ramey
Date: August 2, 2024
"""
import json
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
def get_slack_token(token_path):
with open(token_path, 'r') as token_file:
return token_file.read().strip()
def get_input(prompt, multiple=False):
"""Prompts for user input and optionally handles multiple comma-separated values.
Args:
prompt: The input prompt message.
multiple: Boolean indicating if multiple values are expected.
Returns:
A single value or a list of values based on the `multiple` parameter.
"""
value = input(prompt)
if multiple:
return [item.strip() for item in value.split(',')]
return value
def update_channels_from_json(client, json_path):
"""Updates channels based on data provided in a JSON file.
Args:
client: The Slack WebClient instance.
json_path: The path to the JSON file containing channel data.
"""
with open(json_path, 'r') as file:
data = json.load(file)
for item in data:
channel_id = item["channel_id"]
current_team_id = item.get("current_team_id", None)
target_team_ids = item["target_team_ids"]
print(f"Updating channel {channel_id} with current_team_id: {current_team_id} and target_team_ids: {target_team_ids}")
update_channel_teams(client, channel_id, target_team_ids, current_team_id)
def update_channel_teams(client, channel_id, target_team_ids, current_team_id=None):
"""Updates the teams for a specified channel.
Args:
client: The Slack WebClient instance.
channel_id: The ID of the channel to update.
target_team_ids: The target team IDs to assign the channel to.
current_team_id: The current team ID of the channel (if applicable).
"""
try:
response = client.admin_conversations_setTeams(
channel_id=channel_id,
target_team_ids=target_team_ids,
team_id=current_team_id,
org_channel=False
)
print(f"Response for channel {channel_id}: {response['ok']}")
except SlackApiError as e:
print(f"Error for channel {channel_id}: {e.response['error']}")
def main():
"""Main function to drive the script based on user input."""
token_path = get_input("Please enter the path to your Slack token file: ")
token = get_slack_token(token_path)
client = WebClient(token=token)
data_source = get_input("Do you want to provide data via JSON? (yes/no): ").strip().lower()
if data_source == "yes":
json_path = get_input("Enter the path to the JSON file: ")
update_channels_from_json(client, json_path)
else:
multiple_channels = get_input("Is this for multiple channels? (yes/no): ").strip().lower() == "yes"
if multiple_channels:
channel_ids = get_input("Enter the channel IDs (comma-separated): ", multiple=True)
else:
channel_ids = [get_input("Enter the channel ID: ")]
current_team_id = get_input("Enter the current team ID (leave empty if not needed): ")
if current_team_id:
additional_team_ids = get_input("Enter the additional target team IDs (comma-separated): ", multiple=True)
target_team_ids = [current_team_id] + additional_team_ids
else:
target_team_ids = get_input("Enter the target team IDs (comma-separated): ", multiple=True)
for channel_id in channel_ids:
update_channel_teams(client, channel_id, target_team_ids, current_team_id if current_team_id else None)
if __name__ == "__main__":
main()