Skip to content

Instantly share code, notes, and snippets.

@kenfdev
Created July 2, 2025 03:02
Show Gist options
  • Save kenfdev/fcdb32250350c109df6d4f989c86d4de to your computer and use it in GitHub Desktop.
Save kenfdev/fcdb32250350c109df6d4f989c86d4de to your computer and use it in GitHub Desktop.
Claude Code Notificaton to Slack Webhook
#!/bin/bash
# Enhanced notify-slack.sh that handles both Notification and Stop hooks
# Replace YOUR_WEBHOOK_URL with your actual Slack webhook URL
# Slack Webhook URL (replace with your actual URL)
WEBHOOK_URL="YOUR/WEBHOOK/URL"
# Check if webhook URL has been updated
if [[ "$WEBHOOK_URL" == *"YOUR/WEBHOOK/URL"* ]]; then
echo "Error: Please update WEBHOOK_URL with your actual Slack webhook URL" >&2
exit 1
fi
# Claude Code sends JSON data via stdin, so we read it into a variable
INPUT=$(cat)
# Determine which hook triggered this script
# The Stop hook doesn't have 'message' or 'title' fields
if echo "$INPUT" | jq -e '.message' > /dev/null 2>&1; then
# This is a Notification hook
MESSAGE=$(echo "$INPUT" | jq -r '.message // "Claude Code notification"')
TITLE=$(echo "$INPUT" | jq -r '.title // "Claude Code"')
ICON="🔔"
COLOR="warning"
else
# This is likely a Stop hook
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // "unknown"')
TRANSCRIPT_PATH=$(echo "$INPUT" | jq -r '.transcript_path // "unknown"')
STOP_HOOK_ACTIVE=$(echo "$INPUT" | jq -r '.stop_hook_active // false')
# For Stop hook, create a custom message
TITLE="Claude Code - Task Completed"
MESSAGE="Task has been completed successfully"
ICON="✅"
COLOR="good"
# If stop_hook_active is true, we might want to skip notification to avoid loops
if [[ "$STOP_HOOK_ACTIVE" == "true" ]]; then
echo "Skipping notification as stop_hook_active is true" >&2
exit 0
fi
fi
# Add timestamp
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
# Create Slack payload with different styling based on hook type
PAYLOAD=$(jq -n \
--arg text "$ICON $TITLE" \
--arg message "$MESSAGE" \
--arg time "$TIMESTAMP" \
--arg color "$COLOR" \
'{
"text": $text,
"attachments": [{
"color": $color,
"fields": [
{
"title": "Message",
"value": $message,
"short": false
},
{
"title": "Time",
"value": $time,
"short": true
}
]
}]
}')
# Send to Slack
curl -X POST \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
"$WEBHOOK_URL" \
-s -w "\nNotification sent to Slack: HTTP %{http_code}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment