Python: Send a Discord Webhook Notification
pythonPROJECT
Command-line tool for sending messages, embeds, and files to Discord webhooks from bash scripts and automation workflows
pythondiscordwebhooknotificationsalertsmonitoringautomation
Quick Start
Copy-paste ready version that works immediately while you set up the full project
Python - Quick Startpython
import json
import os
import urllib.request
WEBHOOK_URL = os.environ["DISCORD_WEBHOOK_URL"]
def send_discord_alert(message: str) -> None:
payload = {"content": message}
data = json.dumps(payload).encode("utf-8")
req = urllib.request.Request(
WEBHOOK_URL,
data=data,
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(req, timeout=10) as response:
if response.status not in (200, 204):
raise RuntimeError(f"Discord webhook failed: {response.status}")
# Example usage
send_discord_alert("Backup completed successfully.")Why It Matters
Discord webhooks are one of the easiest ways to turn ordinary scripts into useful alerting automations.
This is especially useful for:
- backup notifications
- service health checks
- cron jobs
- deployment status messages
- disk space alerts
- homelab monitoring
The quick-start helper above is ideal when you just need to send a message from a Python script.
The full project below is better when you want:
- reusable CLI usage
- multiple webhook profiles
- JSON templates
- rich embeds
- file attachments
- integration from Bash, PowerShell, or Python
Features
- Simple CLI interface for automation workflows
- Rich embeds with colors, titles, and descriptions
- File attachment support
- Multiple webhook profiles for different channels
- Template-driven messages
- Cross-language integrations for Bash, PowerShell, and Python
Use Cases
- Deployment notifications
- Backup status messages
- Monitoring alerts
- Disk and service health notifications
- CI/CD updates
- Incident or error reporting
Installation
Use the full utility when you want a reusable command-line tool instead of a single inline helper. #### Clone the project git clone https://github.com/rtc-scriptedops/discord-webhook.git cd discord-webhook #### Make the CLI executable chmod +x bin/discord-webhook #### Optional: install it into your PATH mkdir -p "$HOME/bin" cp bin/discord-webhook "$HOME/bin/discord-webhook" echo 'export PATH="$HOME/bin:$PATH"' >> "$HOME/.bashrc" source "$HOME/.bashrc" #### Configure your webhook URL export DISCORD\_WEBHOOK\_URL="https://discord.com/api/webhooks/your/webhook/url" If the utility supports config-bDiscord webhooks are one of the easiest ways to turn ordinary scripts into useful alerting automations. This is especially useful for: - backup notifications - service health checks - cron jobs - deployment status messages - disk space alerts - homelab monitoring The quick-start helper above is ideal when you just need to send a message from a Python script. The full project below is better when you want: - reusable CLI usage - multiple webhook profiles - JSON templates - rich embeds - file attachments - integration from Bash, PowerShell, or Python ### Features - Simple CLI interface for automation workflows - Rich embeds with colors, titles, and descriptions - File attachment support - Multiple webhook profiles for different channels - Template-driven messages - Cross-language integrations for Bash, PowerShell, and Python ### Use Cases - Deployment notifications - Backup status messages - Monitoring alerts - Disk and service health notifications - CI/CD updates - Incident or error reporting ased setup, you can also copy the example config and customize it: mkdir -p "$HOME/.config/discord-webhook" cp config/discord-webhook.conf.example "$HOME/.config/discord-webhook/config.conf" Then edit the config file with your webhook URL, profile names, and defaults.
File Structure
Project layout to help you understand what you'll get when you clone or download
Project Structure
▼📁discord-webhook
📄discord-webhook
📝README.md
▼📁examples/
📜jenkins-notification.sh
📜monitoring-alert.sh
▼📁templates/
📋embed-templates.json
Usage Examples
How to use Python: Send a Discord Webhook Notification
# Basic message discord-webhook "Server deployment completed successfully" # With custom username and avatar discord-webhook --username "DeployBot" --avatar "🚀" "Build #123 deployed" # Rich embed with color discord-webhook --embed --color "success" --title "Backup Complete" --description "Daily backup finished at $(date)" # Send file attachment discord-webhook --file "/var/log/deploy.log" "Deployment logs attached" # Multiple webhooks (different channels) discord-webhook --profile "alerts" "High CPU usage detected" discord-webhook --profile "deployments" "Rolling back to previous version"
Get the Full Project
Get new tools weekly.
Subscribe to receive fresh automation scripts, project tools, and IT workflow improvements.
Subscribe to Newsletter