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:
Implementing these solutions can help maintain a professional and respectful environment in your Slack channels.
Here’s a basic guide on how to create such a bot using Python and the Slack API.
channels:history
, channels:read
, chat:write
, chat:write.customize
, groups:history
, groups:read
, im:history
, im:read
, mpim:history
, and mpim:read
.Install the required libraries:
pip install slack_sdk python-dotenv
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
7
8load_dotenv()
9
10slack_token = os.getenv("SLACK_BOT_TOKEN")
11client = WebClient(token=slack_token)
12
13profanity_list = ["badword1", "badword2"] # Add your list of profane words
14political_list = ["politicalword1", "politicalword2"] # Add your list of political words
15
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
21
22@RTMClient.run_on(event="message")
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')
29
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']}")
40
41if __name__ == "__main__":
42 rtm_client = RTMClient(token=slack_token)
43 rtm_client.start()
44
Create a .env
file in the same directory as your slack_bot.py
file and add your Slack Bot Token:
SLACK_BOT_TOKEN=your-slack-bot-token
Run the Python script:
python slack_bot.py
slack_sdk
library to interact with the Slack API.RTMClient
.profanity_list
or political_list
.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.