Go to Slack

Naming channels by convention

Use cases:
  • Let teams know about new channels that are relevant to them
  • Enforce naming conventions for channels
Works with:
  • Events API
  • Message Buttons
Code samples:

An app that watches for new public channels and checks whether their name matches an existing channel category.

If it does match an existing category, the app posts a notification to the parent channel for the category. For example, if you have a category of channels that starts with #support- for different types of support, a notification would get sent into the overarching #support channel when a new #support- channel is created.

How it works

Watch as channels are named by convention

1. Find out when a new channel has been created

To find out when a channel has been created you'll need to subscribe to the channel_created team event being sent out through the Events API. The Events API sends a POST request to a URL you've provided whenever certain events occur on your Slack team. In the case of channel_creaed event, Slack will send an event when a public channel is created. The event will include the channel name and ID.

2. Check if a channel category exists for the prefix

The app assumes that everything before the first hyphen in the channel name is the prefix used as part of the channel naming convention. Using the channel prefix the checks its datastore to see if a matching channel category already exists. If it does, the app will send a notification to the category's parent channel that includes the new channel's name and purpose. To get the channel's purpose, the app has to call the channels.info method, passing in the channel ID from the channel_created event. The notification to the parent channel is sent via the chat.postMessage method.

3. If there isn't an existing convention, prompt the user to create one

In instances where the channel's prefix doesn't match any existing channel categories, the app will prompt the user to create one. Using chat.postMessage, the app will send a message into the newly created channel asking the user if they'd like to create a channel category for the prefix. The message will include "Yes" and "No" buttons. To include message actions (such as buttons and menus), you'll need to enable Interactive Messages in the app settings and supply a URL where Slack can send the button click event.

If the user clicks 'Yes', the app will receive a POST request on its Interactive Message endpoint. The payload will include information about the channel where this action originated along with a response_url that can be used to update the original message. Using the response URL, replace the 'Yes' and "No" buttons with a message menu and ask the user to select the parent channel for the new channel category. The message menu can be pre-populated with a list of channels by setting the menu's data_source to channels.

Once the user selects a channel, the app will receive a POST request on its Interactive Message endpoint. The request will include information about the channel the user selected and the channel they took the action in. Using this information, the app will save a new channel category in its datastore. It will also use chat.postMessage and channels.info to send a notification to the new category's parent channel.

Diagram

How to enforce channel naming conventions programmatically

Customization suggestions

  • What information is included in the new channel notification
  • Handling archiving and unarchiving of channels
  • Support private channels
  • Request a user's API token to automatically rename channels
  • Report on channels that don't follow naming conventions
  • List all the channels for a given naming convention
  • Support more complex naming conventions (e.g. multiple prefixes)

Related documentation