Skip to content

Instantly share code, notes, and snippets.

@stefanthoss
Last active February 14, 2026 03:57
Show Gist options
  • Select an option

  • Save stefanthoss/4c8619d9a53b7602762203e03efc772c to your computer and use it in GitHub Desktop.

Select an option

Save stefanthoss/4c8619d9a53b7602762203e03efc772c to your computer and use it in GitHub Desktop.
Sonarr custom notification script for Mattermost incoming webhook
#!/bin/bash
# --- CONFIGURATION ---
# Webhook documentation in https://developers.mattermost.com/integrate/webhooks/incoming/
WEBHOOK_URL="https://mm.example.com/hooks/secretkey"
CHANNEL="shows"
USERNAME="Sonarr"
ICON_URL="https://raw.githubusercontent.com/Sonarr/Sonarr/develop/Logo/256.png"
# Check if script is run by Sonarr
if [ -z "$sonarr_eventtype" ]; then
echo "Error: This script must be run from within Sonarr."
exit 1
fi
EVENT="$sonarr_eventtype"
SERIES_TITLE="$sonarr_series_title"
# Formatting helpers
SEASON=$(printf "%02d" "$sonarr_episodefile_seasonnumber")
EPISODE=$(printf "%02d" "$sonarr_episodefile_episodenumbers")
# Event types: https://wiki.servarr.com/sonarr/custom-scripts
case "$EVENT" in
"Grab")
HEADER="### 📥 Show Grabbed"
BODY="**$SERIES_TITLE** - ${sonarr_release_title}\n**Size**: $((sonarr_release_size / 1024 / 1024)) MB"
;;
"Download")
HEADER="### ✅ Show Imported"
BODY="**$SERIES_TITLE** - S${SEASON}E${EPISODE} - $sonarr_episodefile_episodetitles\n**Quality**: $sonarr_episodefile_quality\n**Path**: $sonarr_episodefile_relativepath"
;;
"EpisodeFileDelete")
HEADER="### 🗑️ Episode Deleted"
BODY="**$SERIES_TITLE** - S${SEASON}E${EPISODE}\n**Reason**: File removed or upgraded."
;;
"ManualInteractionRequired")
HEADER="### ⚠️ Manual Interaction Required"
BODY="**$SERIES_TITLE** requires your attention.\n**Issue**: Sonarr was unable to complete the import automatically.\n**Path**: $sonarr_episodefile_path"
;;
"Test")
HEADER="### 🔔 Test Notification"
BODY="Webhook connection to **$CHANNEL** is successful for $USERNAME!"
;;
*)
HEADER="### ℹ️ $USERNAME Notification"
BODY="Event **$EVENT** triggered for **$SERIES_TITLE**."
;;
esac
PAYLOAD=$(cat <<EOF
{
"channel": "$CHANNEL",
"username": "$USERNAME",
"icon_url": "$ICON_URL",
"text": "$HEADER\n$BODY"
}
EOF
)
curl -s -i -X POST \
-H 'Content-Type: application/json' \
-d "$PAYLOAD" \
"$WEBHOOK_URL" > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment