dotfiles/scripts/slack_notifier.sh

83 lines
1.9 KiB
Bash
Raw Normal View History

2025-04-06 15:37:42 +01:00
#!/bin/bash
# Check that this can be sourced by cron at actual runtime
# Env vars:
# --- Webhook URLs for given channel, eg $SLACK_WEBHOOK_TEST, $SLACK_WEBHOOK_EOLAS
# --- sourced from `.env` file in Zsh path
# Parameters:
# --- $1 = Slack channel,
# --- $2 = type 'error' | 'success'
# --- $3 = Message
# --- $4 = (Opt) Error details
# --- $5 = (Opt) Error source
# Usage:
# --- ./slack_notifier.sh test 'SUCCESS: It worked'
# --- ./slack_notifier.sh test 'ERROR: Something went wrong' 'Error message'
declare -A CHANNEL_TO_WEBHOOK
CHANNEL_TO_WEBHOOK["test"]=$SLACK_WEBHOOK_TEST
CHANNEL_TO_WEBHOOK["backups"]=$SLACK_WEBHOOK_BACKUPS
CHANNEL_TO_WEBHOOK["eolas"]=$SLACK_WEBHOOK_EOLAS
CHANNEL_TO_WEBHOOK["website"]=$SLACK_WEBHOOK_SYSTEMS_OBSCURE
CHANNEL_TO_WEBHOOK["time-tracking"]=$SLACK_WEBHOOK_TIME_TRACKING
WEBHOOK=${CHANNEL_TO_WEBHOOK[$1]}
ERROR_BLOCKS=$(
jq -n \
--arg channel "$1" \
--arg message "$3" \
--arg details "$4" \
--arg source "$5" \
'{
channel: $channel,
blocks: ([
{
type: "section",
text: {
type: "plain_text",
text: "🔴" + $message
}
},
{
type: "section",
text: {
type: "mrkdwn",
text: "```\n\($details)\n```"
}
},
{
"type": "context",
"elements": [
{
"type": "plain_text",
text: $source
}
]
}
])
}'
)
# Initialise sound playback
mpv --volume=0 --start=0 --length=0.1 /home/thomas/dotfiles/gruvbox-95/sounds/st-computer-on.mp3
sleep 1
# Process notification
if [ "$2" != "error" ]; then
curl -X POST \
-H 'Content-type: application/json' \
--data '{"text":"🟢 '"$3"'"}' \
"$WEBHOOK"
mpv --volume=100 /home/thomas/dotfiles/sounds/star-trek-computer-success.mp3
else
curl -X POST \
-H 'Content-type: application/json' \
--json "$ERROR_BLOCKS" \
"$WEBHOOK"
mpv --volume=100 /home/thomas/dotfiles/sounds/star-trek-computer-error.mp3
fi