Mass Cleaning And Monitoring Of Slack Channels

A Guide To Tidy Up Your Slack When Things Go Crazy !

AHMAD ASSAF
Jan 20 · 4 min read

Slack is a great tool and can act as a single sink for a lot of our notifications. However, Slack can be easily abused and very often people get carried away with creating tons of channels. This is a simple guide with tips and tricks to keep all of that under control

Image for post
Image for post
Photo by Webaroo.com.au on Unsplash

Slack is a great tool and can act as a single sink for a lot of our notifications. However, Slack can be easily abused and very often people get carried away with creating tons of channels. This is a simple guide with tips and tricks to keep all of that under control. Slack abuse can come as well from bots and integrations set up. This post will sum up some of experiences with “Slack Noise” and share my experience in limiting/fixing that.

AUTOMATIC ARCHIVE/DELETION OF CHANNELS

Drift is a chat software & conversational marketing platform that allows our sales and marketing teams to chat live with website visitors. Drift had a strict requirement which made its integration create a slack channel for every chat initiated. This created thousands of unwanted channels that were just pure noise since the team interacted with drift directly in drift itself.

Drift channels have a consistent naming pattern as they are all prefixed with z- e.g., z-customer-1. At first, I wanted to archive all the drift channels using a simple bash script. The script will:

  • Query Slack API for the list of all channels and save them into a temp file (For information on getting Slack tokens check the doc here)
  • Iterate over the set of channels that contain the naming pattern z-
  • For each channel call the Slack API to archive that channel using its ID.
TOKEN='<SLACK_TOKEN>'
URL='https://slack.com/api/channels.archive';
curl "$URL?token=$TOKEN&exclude_archived=true&pretty=1" > /tmp/channels.list
IDS=$(cat /tmp/channels.list | jq '.channels[] | select(.name | contains("z-")) | .id' | sed -e 's/"//g')
for ID in $IDS; do
URL="https://slack.com/api/channels.archive?token=$TOKEN&channel=$ID&pretty=1"
echo $URL
curl "$URL"
done

Advanced Hint: If you would to hard delete the channel there is an undocumented API endpoint channels.delete that you can use by editing the variable URL to be https://slack.com/api/channels.delete

MONITORING SLACK CHANNELS CREATION

As your Slack team grows, regardless of any best practices or guidelines you have in place, people will still create channels that either is not needed or doesn’t follow your naming guidelines. In order to keep an eye on this, I have set up a Zapier to ping me whenever someone in my Slack workspace creates a new channel.

Image for post
Image for post
Slack Hunter Message in Slack

The zap configuration is as easy as:

  • Assign the trigger to be “New Channel” in Slack.
  • Assign the action to be “Send Direct Message” in Slack
  • To have the message sent to me I set the “To Username” to be Use a Custom Value (advanced)
  • Assign the “Custom Value for To Username ID” to be your username in Slack with the @ e.g., @ahmad
  • I have set up the “Message Text” to be New slack channel was created by #{Creator Name} .. check it out .. Its called #{Name}
  • I chose the messages to be send as a bot and assigned a custom emoji for it as shown in the screenshot below.
Slack Hunter Zap — Send Direct Message Configuration
Slack Hunter Zap — Send Direct Message Configuration
Slack Hunter Zap — Send Direct Message Configuration

BONUS! LET’S AUTO-DELETE/ARCHIVE CHANNELS VIA A ZAP

Running a script periodically to clean up the channels can be hectic. So what about we combine now the zap with the script that can delete that channel automatically!

We can Add an extra step to our Zap above and have the action as “Run Javascript”. The template for the script is:


const TOKEN = "<SLACK_TOKEN>";
const URL = 'https://slack.com/api/channels.archive';
// Make sure we only delete channels that start with z- which are created by drift
if (input.name.indexOf('z-') == 0 ) {
fetch(URL, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: "token="+ TOKEN + "&channel=" + input.id
}).then(function(res) {
return res.text();
}).then(function(body) {
callback(null, {rawHTML: body});
}).catch(callback);
} else callback(null, {message: 'Channel: ' + input.name + ' Not Applicable for Auto-archiving'});

You can change the endpoint to call the undocumented channels.delete endpoint as well

This will be triggered every time a Slack channel is created, it will do a check on the channel’s name (in here we are checking for the drift naming convention with z_ prefix) and then trigger an API call that will use the channel ID. To pass the ID into the JavaScript function I configured the input to be:

Image for post
Image for post
Slack Hunter Zap — JavaScript Input Data

SLACK ANALYTICS

Slack offer an analytics view that can be useful for one-off audit purposes as it allows to:

  • See where (either public or private channels) people send the most messages.
  • Sort public channels by metrics like name, total members or messages sent.
  • Get a snapshot of members’ message activity.

Thats it for now! I hope these few tricks can allow you to stay on top of Slack channels.

overfit

Ramblings about Software Engineering, AI, ML and the Web

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store