-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaaa6f7
commit 8952beb
Showing
30 changed files
with
3,129 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Script: Slack Restrict or Approve App | ||
Description: | ||
This script allows administrators to either restrict or approve an app in a Slack workspace. The action | ||
(restrict or approve) is specified by the user, and the app is identified by its App ID and Team ID. | ||
Functions: | ||
- get_slack_token: Reads the Slack token from a specified file. | ||
- Main script block: Initializes the Slack WebClient and processes user input to restrict or approve an app. | ||
Usage: | ||
1. Run the script. | ||
2. Enter the path to your Slack token file when prompted. | ||
3. Choose whether to restrict or approve an app. | ||
4. Enter the App ID and Team ID when prompted. | ||
5. The script will either restrict or approve the app based on the user's input. | ||
Notes: | ||
- Ensure that the Slack token has the necessary permissions to restrict or approve apps. | ||
- 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 | ||
""" | ||
|
||
from slack_sdk import WebClient | ||
from slack_sdk.errors import SlackApiError | ||
|
||
def get_slack_token(token_path): | ||
"""Reads the Slack token from a specified file. | ||
Args: | ||
token_path: The path to the file containing the Slack token. | ||
Returns: | ||
The Slack token as a string. | ||
""" | ||
with open(token_path, 'r') as token_file: | ||
return token_file.read().strip() | ||
|
||
# Initialize the Slack WebClient with your token | ||
token_path = input("Please enter the path to your Slack token file: ") | ||
token = get_slack_token(token_path) | ||
client = WebClient(token=token) | ||
|
||
# Get user input for action (restrict/approve) | ||
action = input("Do you want to restrict or approve the app? (Type 'restrict' or 'approve'): ").lower() | ||
|
||
if action == 'restrict': | ||
app_id = input("Enter the App ID: ") | ||
team_id = input("Enter the Team ID: ") | ||
try: | ||
response = client.admin_apps_restrict(app_id=app_id, team_id=team_id) | ||
print("App was restricted successfully!") | ||
except SlackApiError as e: | ||
print(f"Failed to restrict the app. Error: {e.response['error']}") | ||
elif action == 'approve': | ||
app_id = input("Enter the App ID: ") | ||
team_id = input("Enter the Team ID: ") | ||
try: | ||
response = client.admin_apps_approve(app_id=app_id, team_id=team_id) | ||
print("App was approved successfully!") | ||
except SlackApiError as e: | ||
print(f"Failed to approve the app. Error: {e.response['error']}") | ||
else: | ||
print("Invalid action. Please type 'restrict' or 'approve'.") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
Script: Revoke Slack Token | ||
Description: | ||
This script revokes a specified Slack token. The user is prompted to enter the path to a file containing their | ||
user token, as well as the token to be revoked. The script then uses the Slack API to revoke the specified token. | ||
Functions: | ||
- get_slack_token: Reads the Slack token from a specified file. | ||
- main: Main function to drive the script based on user input. | ||
Usage: | ||
1. Run the script. | ||
2. Enter the path to your user token file when prompted. | ||
3. Enter the token you wish to revoke when prompted. | ||
4. The script will revoke the specified token and provide feedback on the operation's success. | ||
Notes: | ||
- Ensure that the user token has the necessary permissions to revoke other tokens. | ||
- 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 os | ||
from slack_sdk import WebClient | ||
|
||
def get_slack_token(token_path): | ||
"""Reads the Slack token from a specified file. | ||
Args: | ||
token_path: The path to the file containing the Slack token. | ||
Returns: | ||
The Slack token as a string. | ||
""" | ||
with open(token_path, 'r') as token_file: | ||
return token_file.read().strip() | ||
|
||
def main(): | ||
"""Main function to drive the script based on user input.""" | ||
user_token_path = input("Please enter the path to your user token file: ") | ||
user_token = get_slack_token(user_token_path) | ||
token_to_revoke = input("Enter the token to revoke: ") | ||
|
||
client = WebClient(token=user_token) | ||
|
||
try: | ||
response = client.auth_revoke(test=True, token=token_to_revoke) | ||
if response["ok"]: | ||
print("Token was successfully revoked.") | ||
else: | ||
print("Token revocation failed.") | ||
except Exception as e: | ||
print(f"An error occurred: {str(e)}") | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
""" | ||
Script: Slack Add or Remove Users from Channels | ||
Description: | ||
This script adds or removes users from one or more Slack channels based on data provided in a CSV file. | ||
The script can handle both public and private channels. For private channels, the Slack app needs to be | ||
a member, or another token created by a different admin/owner should be used. | ||
CSV File Structure: | ||
- The CSV file should contain headers: channel_id, users | ||
- The 'users' column should contain a comma-separated list of user IDs. | ||
Usage: | ||
1. Install the Slack SDK using 'pip install slack_sdk'. | ||
2. Run the script. | ||
3. Enter the path to your Slack token file when prompted. | ||
4. Provide the location of the CSV file containing channel IDs and user IDs. | ||
5. Choose whether to 'add' or 'remove' users from channels. | ||
Notes: | ||
- Ensure that the Slack token has the necessary permissions to add or remove users from channels. | ||
- The app must be a member of private channels to perform actions, or use a token from an admin/owner. | ||
Author: Chad Ramey | ||
Date: August 2, 2024 | ||
""" | ||
|
||
import csv | ||
import time | ||
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 main(): | ||
# Prompt the user for the Slack token | ||
token_path = input("Please enter the path to your Slack token file: ") | ||
token = get_slack_token(token_path) | ||
|
||
# Prompt the user for the CSV location and header information | ||
csv_location = input("Enter the CSV location (channel_id,users): ") | ||
|
||
# Prompt the user to choose an action | ||
action = input("Do you want to 'add' or 'remove' users from channels? ").lower() | ||
|
||
# Extract the data from the CSV | ||
channels_data = [] | ||
with open(csv_location, 'r') as file: | ||
reader = csv.DictReader(file) | ||
for row in reader: | ||
channel_id = row['channel_id'] | ||
users = row['users'].split(',') | ||
channels_data.append({'channel_id': channel_id, 'users': users}) | ||
|
||
client = WebClient(token=token) | ||
|
||
for channel_data in channels_data: | ||
channel_id = channel_data['channel_id'] | ||
users = channel_data['users'] | ||
|
||
for user_id in users: | ||
retry = True | ||
while retry: | ||
try: | ||
if action == 'add': | ||
response = client.admin_conversations_invite(channel_id=channel_id, user_ids=[user_id]) | ||
elif action == 'remove': | ||
response = client.conversations_kick(channel=channel_id, user=user_id) | ||
else: | ||
print("Invalid action. Please choose 'add' or 'remove'.") | ||
break | ||
|
||
except SlackApiError as e: | ||
if e.response.status_code == 429: # Rate-limited error | ||
print(f"Rate-limited. Retrying after {e.response.headers['Retry-After']} seconds...") | ||
retry_after = int(e.response.headers['Retry-After']) | ||
time.sleep(retry_after) | ||
else: | ||
print(f"Failed to perform action on User {user_id} for Channel {channel_id}: {e.response['error']}") | ||
retry = False | ||
else: | ||
if response['ok']: | ||
print(f"User {user_id} was successfully {action}ed to/from Channel {channel_id}") | ||
else: | ||
print(f"Failed to perform action on User {user_id} for Channel {channel_id}: {response['error']}") | ||
retry = False | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
""" | ||
Script: Slack Add + Remove User from Workspace | ||
Description: | ||
This script adds or removes users from a Slack workspace based on user IDs provided in a CSV file. | ||
The action can be specified as either 'add' or 'remove'. The script handles rate limiting by waiting | ||
before retrying API requests. | ||
CSV File Structure: | ||
- The CSV file should contain a header: user_id | ||
Functions: | ||
- get_slack_token: Reads the Slack token from a specified file. | ||
- add_user_to_slack: Adds a user to the Slack workspace. | ||
- remove_user_from_slack: Removes a user from the Slack workspace. | ||
- 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. Enter the location of the CSV file containing user IDs. | ||
4. Enter the Team ID for the Slack workspace. | ||
5. Choose whether to 'add' or 'remove' users from the workspace. | ||
6. The script will process each user ID and add or remove them from the workspace. | ||
Notes: | ||
- Ensure that the Slack token has the necessary permissions to add or remove users. | ||
- 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 requests | ||
import csv | ||
import time | ||
|
||
def get_slack_token(token_path): | ||
"""Reads the Slack token from a specified file. | ||
Args: | ||
token_path: The path to the file containing the Slack token. | ||
Returns: | ||
The Slack token as a string. | ||
""" | ||
with open(token_path, 'r') as token_file: | ||
return token_file.read().strip() | ||
|
||
def add_user_to_slack(token, team_id, user_id): | ||
"""Adds a user to the Slack workspace. | ||
Args: | ||
token: The Slack API token. | ||
team_id: The ID of the Slack team (workspace). | ||
user_id: The ID of the user to add. | ||
Returns: | ||
The response from the Slack API. | ||
""" | ||
url = "https://slack.com/api/admin.users.assign" | ||
payload = f'team_id={team_id}&user_id={user_id}' | ||
headers = { | ||
'Authorization': f'Bearer {token}', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
} | ||
|
||
response = requests.post(url, headers=headers, data=payload) | ||
return response | ||
|
||
def remove_user_from_slack(token, team_id, user_id): | ||
"""Removes a user from the Slack workspace. | ||
Args: | ||
token: The Slack API token. | ||
team_id: The ID of the Slack team (workspace). | ||
user_id: The ID of the user to remove. | ||
Returns: | ||
The response from the Slack API. | ||
""" | ||
url = "https://slack.com/api/admin.users.remove" | ||
payload = f'team_id={team_id}&user_id={user_id}' | ||
headers = { | ||
'Authorization': f'Bearer {token}', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
} | ||
|
||
response = requests.post(url, headers=headers, data=payload) | ||
return response | ||
|
||
def main(): | ||
"""Main function to drive the script based on user input.""" | ||
token_path = input("Please enter the path to your Slack token file: ") | ||
token = get_slack_token(token_path) | ||
csv_location = input("Please enter the CSV file location: ") | ||
team_id = input("Please enter the Team ID for the workspace: ") | ||
|
||
action = input("Do you want to 'add' or 'remove' users from the workspace? ").lower() | ||
|
||
if action not in ['add', 'remove']: | ||
print("Invalid action. Please choose 'add' or 'remove'.") | ||
return | ||
|
||
with open(csv_location, newline='') as csvfile: | ||
reader = csv.DictReader(csvfile) | ||
for row in reader: | ||
user_id = row.get('user_id') | ||
if user_id: | ||
if action == 'add': | ||
response = add_user_to_slack(token, team_id, user_id) | ||
elif action == 'remove': | ||
response = remove_user_from_slack(token, team_id, user_id) | ||
|
||
if response.status_code == 429: | ||
print(f"Rate limited. Waiting for {response.headers['Retry-After']} seconds...") | ||
time.sleep(int(response.headers['Retry-After'])) | ||
if action == 'add': | ||
response = add_user_to_slack(token, team_id, user_id) | ||
elif action == 'remove': | ||
response = remove_user_from_slack(token, team_id, user_id) | ||
|
||
if response.status_code == 200: | ||
print(f"User {user_id} {action}ed from Slack workspace.") | ||
else: | ||
print(f"Failed to {action} user {user_id}. Error: {response.text}") | ||
else: | ||
print("Missing user_id in the CSV row.") | ||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.