Skip to content

Instantly share code, notes, and snippets.

@billmetangmo
Created September 12, 2023 15:49
Show Gist options
  • Save billmetangmo/b484c53a41db8dbd672d160cb72b3692 to your computer and use it in GitHub Desktop.
Save billmetangmo/b484c53a41db8dbd672d160cb72b3692 to your computer and use it in GitHub Desktop.
slack archive channels
import os
import pandas as pd
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
import csv
from time import sleep
# Initialize Slack client with your token
client = WebClient(token="<SLACK_BOT_TOKEN>")
def get_all_channels():
channels = []
cursor = None
while True:
response = client.conversations_list(limit=200, cursor=cursor) # max limit is 1000, using 200 for demonstration
channels.extend(response['channels'])
cursor = response.get('response_metadata', {}).get('next_cursor')
if not cursor:
break
sleep(1) # Avoid hitting rate limits
return channels
def delete_channels(channel_names):
try:
all_channels = get_all_channels()
print(len(all_channels))
# # Filter out the channels you want to delete
channels_to_delete = [ch for ch in all_channels if ch['name'] in channel_names]
# Delete the channels
for ch in channels_to_delete:
try:
#print(ch['name'])
# Add bot to channel before archiving
client.conversations_join(channel=ch['id'])
client.conversations_archive(channel=ch['id'])
print(f"Channel {ch['name']} archived successfully!")
except SlackApiError as e_inner:
print(f"Error deleting channel {ch['name']}: {e_inner.response['error']}")
except SlackApiError as e:
print(f"Error deleting channels: {e.response['error']}")
# Example usage
channels = []
with open("new_channels_with_zero_members.csv", 'r') as file:
reader = csv.DictReader(file)
for row in reader:
channels.append(row["Nom"])
print(len(channels))
delete_channels(channels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment