Slack Message Dispatcher is a Python-based tool that listens to a specific Slack channel, evaluates messages based on predefined rules (keywords or regex), and forwards the messages to the appropriate target channels based on the configuration.
- Real-Time Listening: The bot listens for new messages in the source channel.
- Message Dispatching: Based on configurable rules, messages are routed to target channels.
- Debug Mode: Test message routing logic without dispatching messages.
- Background Service: Run as a daemon (background service) on Linux for continuous operation.
- Python: 3.7+
- Slack SDK: To interact with Slack's API.
You can install the required dependencies using pip:
pip install slack_sdkThe tool relies on a config.json file for specifying Slack API credentials and rules for message dispatching.
Here’s an example configuration file (config.json) with mock data:
{
"slack_bot_token": "xoxb-your-mock-slack-bot-token",
"source_channel": "C01MOCKCHANNEL",
"rules": [
{
"conditions": [
{"keyword": "example1"}
],
"target_channels": ["C02MOCKCHANNEL1"]
},
{
"conditions": [
{"keyword": "example2"}
],
"target_channels": ["C02MOCKCHANNEL2"]
},
{
"regex": "\\b(mock1|mock2|mock3|mockcorp|examplecorp|samplecorp)\\b",
"target_channels": ["C03MOCKCHANNEL"]
},
{
"regex": "(mockcountry)",
"target_channels": ["C04MOCKCHANNEL"]
},
{
"regex": "(mockregion)",
"target_channels": ["C05MOCKCHANNEL"]
}
]
}- slack_bot_token: The token for your Slack bot (mock data in this example).
- source_channel: The Slack channel ID where the bot will listen for messages.
- rules: A list of rules for dispatching messages based on conditions:
- conditions: Simple keyword matching.
- regex: Regular expression matching for more complex patterns.
- target_channels: A list of Slack channel IDs where matching messages will be forwarded.
- Clone the Repository:
git clone https://github.com/Arikius/slack-message-dispatcher.git
cd slack-message-dispatcher- Install Dependencies:
pip install slack_sdk- Run the Tool:
To run in real-time listening mode:
python slack_dispatcher.pyTo run in debug mode (prints messages and shows which rule would match):
python slack_dispatcher.py --debugYou can set up the Slack Message Dispatcher to run as a service on Linux, so it continues to run even after logging out or rebooting the system. This can be achieved by creating a systemd service.
- Create a Service File:
First, create a new service file under /etc/systemd/system/ called slack_dispatcher.service:
sudo nano /etc/systemd/system/slack_dispatcher.service- Define the Service Configuration:
Add the following content to the service file:
[Unit]
Description=Slack Message Dispatcher
After=network.target
[Service]
ExecStart=/usr/bin/python3 /path/to/your/slack_dispatcher.py
WorkingDirectory=/path/to/your/project
StandardOutput=inherit
StandardError=inherit
Restart=always
User=your_linux_user
[Install]
WantedBy=multi-user.target- ExecStart: The full path to the Python interpreter and the
slack_dispatcher.pyscript. - WorkingDirectory: The path to the directory where your project is located.
- User: The Linux user that will run the service.
- Reload systemd to recognize the new service:
sudo systemctl daemon-reload- Start the Service:
sudo systemctl start slack_dispatcher.service- Enable the Service (so it starts on boot):
sudo systemctl enable slack_dispatcher.service- Check the Status of the Service:
To see if the service is running correctly:
sudo systemctl status slack_dispatcher.serviceIf the service is running properly, it will start automatically in the background each time the system boots up.
Feel free to open issues or contribute by submitting pull requests!