A centralized repository for Slack-related development, including bots, custom integrations, automation scripts, and webhook implementations. This repository contains reusable code and tools for enhancing Slack workflows.
A simple Python utility for sending notifications to a Slack channel using an Incoming Webhook URL. This class is designed to make it easy to monitor the status of your scripts, providing methods to send both success and detailed error messages.
- 🧩 Easy Integration: Simple to drop into any Python project.
- ✅ Success Notifications: Send a confirmation message when a script or task completes successfully. You can use a default message or provide a custom one.
- 🐛 Detailed Error Reporting: Automatically capture and send detailed error information, including the exception type, message, and a full traceback, making debugging easier.
- 🔒 Secure Configuration: Keeps your Slack webhook URL secure by loading it from a
.envfile.
Ensure you have the required Python libraries installed. You can install them using pip:
pip install requests python-dotenvCreate a .env file in the root directory of your project. This file will store your Slack webhook URL securely.
- Create a file named .env.
- Add your Slack webhook URL to the file like this:
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"First, import the Notifier class and create an instance by providing the path to your .env file.
import os
from slack_notifier import Notifier
# Define the path to your .env file
env_path = '/path/to/your/project/.env'
# Create an instance of the notifier
notifier = Notifier(env_path)You can send a notification to confirm that your script has run successfully.
# Send the default success message
notifier.send_success()
# Or, send a custom success message
notifier.send_success("Daily report generation complete!")To automatically send error details upon failure, use a try...except block. Pass the exception object to the send_error method.
try:
# This is where your code that might fail goes
result = 10 / 0
except Exception as e:
# Send a detailed error report to Slack
notifier.send_error(exception=e)This is the main class that handles the notifications.
The constructor initializes the notifier. It loads the SLACK_WEBHOOK_URL from the specified .env file.
env_path(str): The file path to your.envfile containing theSLACK_WEBHOOK_URL.
Sends a success message to the configured Slack channel.
message(str, optional): A custom message to send. If not provided, a default message ("✅ The code completed successfully! 🚀") will be used.
dict: A dictionary containing the status of the request (e.g.,{"status": "success"}).
Sends a detailed error notification to the Slack channel, including the exception type, message, and traceback.
exception(Exception, optional): The exception object caught during execution. This is used to generate the detailed error report.
dict: A dictionary containing the status of the request (e.g.,{"status": "success"}).
Author & Developed by: Seongjun Lee @ Jan 2025
