API & Development

Creating a Bot to Filter Out Bad Words in a Slack channel

API & Development
January 23, 2022
10 min
Chris Farmer


There are ways to filter out bad words in a Slack channel, but it typically requires using third-party apps or bots as Slack does not have a built-in feature for filtering explicit content. Here are some options you can consider:

  1. Third-Party Apps and Bots:
    • Sift: This is a popular bot that can automatically detect and filter inappropriate language in Slack channels. It offers real-time moderation and customizable filters.
    • ModSquad: This service provides content moderation and can help manage and filter messages in Slack channels.
    • CleanSpeak: This is a more comprehensive content moderation tool that can be integrated with Slack for filtering bad words and managing other types of content.
  2. Custom Solutions:
    • If you have development resources, you can build a custom Slack bot using the Slack API. This bot can listen to messages in channels and use a predefined list of bad words to filter out inappropriate content. It can then delete or flag messages containing these words.
  3. Channel Settings and Policies:
    • While not a direct filtering solution, setting clear guidelines and policies for acceptable behavior in your Slack channels can help minimize the use of inappropriate language. Encouraging users to report any violations can also be effective.
  4. Manual Moderation:
    • Assign moderators to monitor channels and manually remove or address any messages that contain bad words. This method is more labor-intensive but can be effective for smaller teams or channels.

Implementing these solutions can help maintain a professional and respectful environment in your Slack channels.

Creating a Bot to Filter Out Profanity

Here’s a basic guide on how to create such a bot using Python and the Slack API.


  1. Slack Workspace: You need to have a Slack workspace where you have permissions to add apps and bots.
  2. Slack API Token: You'll need a Slack API token for your bot.
  3. Python Environment: Ensure you have Python installed on your machine.

Steps to Create the Bot

1. Create a Slack App

  • Go to the Slack API and create a new app.
  • Choose the workspace where you want to install the bot.
  • Go to the "OAuth & Permissions" section and add the following scopes: channels:history, channels:read, chat:write, chat:write.customize, groups:history, groups:read, im:history, im:read, mpim:history, and mpim:read.
  • Install the app to your workspace and get the Bot User OAuth Token.

2. Set Up Your Python Environment

Install the required libraries:

pip install slack_sdk python-dotenv

3. Create the Bot Code

Create a new Python file (e.g., slack_bot.py) and add the following code:

1import os
2import re
3from slack_sdk import WebClient
4from slack_sdk.errors import SlackApiError
5from slack_sdk.rtm_v2 import RTMClient
6from dotenv import load_dotenv
10slack_token = os.getenv("SLACK_BOT_TOKEN")
11client = WebClient(token=slack_token)
13profanity_list = ["badword1", "badword2"]  # Add your list of profane words
14political_list = ["politicalword1", "politicalword2"]  # Add your list of political words
16def is_inappropriate(message):
17    for word in profanity_list + political_list:
18        if re.search(r'\b' + re.escape(word) + r'\b', message, re.IGNORECASE):
19            return True
20    return False
23def handle_message(**payload):
24    data = payload['data']
25    web_client = payload['web_client']
26    channel_id = data.get('channel')
27    text = data.get('text')
28    user = data.get('user')
30    if text and is_inappropriate(text):
31        try:
32            response = web_client.chat_delete(
33                channel=channel_id,
34                ts=data['ts']
35            )
36            warning_message = f"<@{user}>, your message contained inappropriate language and has been removed."
37            web_client.chat_postMessage(channel=channel_id, text=warning_message)
38        except SlackApiError as e:
39            print(f"Error deleting message: {e.response['error']}")
41if __name__ == "__main__":
42    rtm_client = RTMClient(token=slack_token)
43    rtm_client.start()

4. Set Up Environment Variables

Create a .env file in the same directory as your slack_bot.py file and add your Slack Bot Token:


5. Run the Bot

Run the Python script:

python slack_bot.py


  • The bot uses the slack_sdk library to interact with the Slack API.
  • It listens for new messages in real-time using RTMClient.
  • When a message is detected, it checks if the message contains any words from the profanity_list or political_list.
  • If an inappropriate word is found, the bot deletes the message and posts a warning to the user who sent it.

You can customize the profanity_list and political_list with the words you want to filter out. This is a basic implementation, and you can expand its functionality as needed.

Related Articles

No items found.