#!/bin/bash # 03-slack-webhook.sh — post a message to a Slack channel on each HearthGate event. # # Why you might want this: # You share the Mac with a small team — maybe a home lab, maybe an oncall # rotation. Everyone in #infra-alerts sees in Slack the moment someone # SSHes in, and they see when the session ends. No one has to ask "are # you on the box right now?". # # Setup once (5 minutes): # 1. Go to https://api.slack.com/messaging/webhooks # 2. Create a Slack app, enable "Incoming Webhooks" # 3. "Add New Webhook to Workspace" → pick the channel → copy the URL # 4. Export the URL as SLACK_WEBHOOK_URL, or paste it into the value below # only on a machine you control. Treat webhook URLs like secrets. # # Setup: # Make the script executable, then add it from HearthGate's Hooks tab. # Full guide: https://codnamacs.com/hearthgate/help/features/hooks # # Customise the message: # The two `payload=` lines below build a Slack-formatted JSON; tweak the # emoji, the channel hostname display, whatever you like. # # Environment HearthGate provides: # HEARTHGATE_EVENT # HEARTHGATE_SESSION_DURATION_SEC (disconnect only) # # Test it: # SLACK_WEBHOOK_URL="https://hooks.slack.com/services/..." \ # HEARTHGATE_EVENT=on-connect ./03-slack-webhook.sh set -euo pipefail # Prefer exporting SLACK_WEBHOOK_URL outside this file. Hard-coding also works, # but only do that on a private machine/repo because webhook URLs are secrets. SLACK_WEBHOOK_URL="${SLACK_WEBHOOK_URL:-}" if [ -z "$SLACK_WEBHOOK_URL" ]; then echo "slack-webhook: SLACK_WEBHOOK_URL is not set, skipping." >&2 # Exit 0 — the user hasn't configured the webhook yet and we should not # mark the hook as failed for that. exit 0 fi EVENT="${HEARTHGATE_EVENT:-${1:-unknown}}" DURATION_SEC="${HEARTHGATE_SESSION_DURATION_SEC:-}" HOST="$(hostname -s)" build_payload() { local emoji="$1" local text="$2" cat <&2 exit 0 ;; esac # --max-time so a slow Slack never holds the hook open longer than the # HearthGate timeout. --silent because hook stdout shows up in the UI; we # only care about errors here. curl --silent --show-error --fail \ --max-time 10 \ -X POST -H 'Content-Type: application/json' \ --data "$payload" \ "$SLACK_WEBHOOK_URL" > /dev/null