#!/usr/bin/env bash # Meldeweg der Box (Autonomie E1): eine Nachricht an den User schicken. # Primär: hermes send → Telegram (nutzt Gateway-Credentials, kein LLM nötig). # Fallback: Logfile + wall (falls Telegram/hermes nicht erreichbar). # # Nutzung: notify.sh "Nachricht" (oder via stdin: echo msg | notify.sh) # notify.sh -s "[Update]" "Text" (Betreffzeile voranstellen) # Exit 0 = zugestellt (Telegram ODER Fallback-Log geschrieben). set -uo pipefail LOG="${MC_NOTIFY_LOG:-$HOME/mc2-notify.log}" SUBJECT="" while getopts ":s:" opt; do case "$opt" in s) SUBJECT="$OPTARG" ;; *) echo "usage: notify.sh [-s subject] " >&2; exit 2 ;; esac done shift $((OPTIND - 1)) MSG="${1:-}" [ -z "$MSG" ] && [ ! -t 0 ] && MSG="$(cat)" if [ -z "$MSG" ]; then echo "usage: notify.sh [-s subject] " >&2 exit 2 fi TS="$(date '+%Y-%m-%d %H:%M:%S')" # Primärweg: Telegram via hermes send (Login-Shell-PATH, falls aus Timer/cron aufgerufen). if [ -n "$SUBJECT" ]; then SEND_OUT="$(bash -lc 'hermes send --to telegram --subject "$1" -- "$2"' _ "$SUBJECT" "$MSG" 2>&1)" else SEND_OUT="$(bash -lc 'hermes send --to telegram -- "$1"' _ "$MSG" 2>&1)" fi if [ $? -eq 0 ]; then echo "$TS OK telegram: $MSG" >> "$LOG" exit 0 fi # Fallback: Logfile + wall — Meldung darf nie verloren gehen. echo "$TS FALLBACK (telegram fehlgeschlagen: $SEND_OUT): $MSG" >> "$LOG" printf '%s\n' "MC2-Meldung: ${SUBJECT:+$SUBJECT }$MSG" | wall 2>/dev/null || true echo "WARN: Telegram fehlgeschlagen, in $LOG protokolliert" >&2 exit 0