Update gotify-send script

This commit is contained in:
albert 2025-04-21 11:35:53 +02:00
parent 562e0087de
commit cc1b58f90f
Signed by: albert
GPG key ID: 3895DD267CA11BA9

View file

@ -192,48 +192,59 @@
end
'';
gotify-send = ''
# --- Argument Check ---
if test (count $argv) -lt 1
echo "Usage: <command> | gotify-send <subject>" >&2
cat > /dev/null
# Removed cat > /dev/null
return 1
end
set -l subject $argv[1]
set -l subject "$(hostname): $argv[1]"
set -l priority 3 # Default priority
set -l gotify_url "https://gotify.sysctl.io"
set -l token_file "/var/run/secrets/gotify_global_token"
# Read token from file
# --- Read Token ---
set -l gotify_token
if test -r "$token_file"
set gotify_token (cat "$token_file" | string trim)
else
echo "Error: Cannot read token file '$token_file'" >&2
cat > /dev/null
# Removed cat > /dev/null
return 1
end
if test -z "$gotify_token"
echo "Error: Token file '$token_file' is empty or read failed." >&2
cat > /dev/null
# Removed cat > /dev/null
return 1
end
set -l message_body (string collect)
# --- Read Piped Input ---
set -l message_body "Command completed"
set -l escaped_subject (string escape --style=json "$subject")
set -l escaped_message (string escape --style=json "$message_body")
# --- Manual JSON Escaping ---
set -l escaped_subject (string replace -a '\\' '\\\\' "$subject" | string replace -a '"' '\\"')
set -l escaped_message (string replace -a '\\' '\\\\' "$message_body" | string replace -a '"' '\\"' | string replace -a \n '\\n')
# --- Construct Payload ---
set -l json_payload (printf '{"title": "%s", "message": "%s", "priority": %d}' \
"$escaped_subject" "$escaped_message" $priority)
# Assumes curl exists
echo $json_payload | curl -sS -X POST \
# --- Execute curl (Verbose, Foreground) ---
echo $json_payload | curl -v -X POST \
-H "Content-Type: application/json" \
-H "X-Gotify-Key: $gotify_token" \
--data @- \
"$gotify_url/message" > /dev/null 2>&1 &
"$gotify_url/message"
# --- Check curl Status ---
set curl_status $status
echo "--- curl Exit Status: $curl_status ---" >&2
if test $curl_status -ne 0
echo "Error: curl command failed." >&2
return $curl_status
end
return 0
'';
};