2
0
Fork 0
rootfs/usr/local/bin/sh.zio.notify

103 lines
2.2 KiB
Plaintext
Raw Normal View History

2024-03-30 17:57:47 +01:00
#!/usr/bin/env bash
_PLUG_TITLE="ntfy Helper"
_PLUG_DESCRIPTION=""
_PLUG_ARGS=(
"message;m;;string;(No Message)"
"title;t;;string;(No Title)"
"level;l;;string;info"
2024-04-01 22:26:52 +02:00
"invoked-by;i;;string"
2024-03-30 17:57:47 +01:00
)
2024-04-01 22:26:52 +02:00
function get_cache_header() {
topic="$1"
if [[ "$topic" == "zio_test" ]]; then
echo ""
else
echo "Cache: no"
fi
}
2024-03-30 17:57:47 +01:00
function get_level_emoji() {
case "$1" in
"critical"|"error") echo "⛔" ;;
"warning") echo "⚠️" ;;
"success") echo "✅" ;;
"info") echo "" ;;
"debug"|*) echo "🔧" ;;
esac
}
function get_level_priority() {
case "$1" in
"critical") echo "5" ;;
"error") echo "4" ;;
"warning") echo "4" ;;
"success") echo "3" ;;
"info") echo "2" ;;
"debug"|*) echo "1" ;;
esac
}
2024-04-01 22:26:52 +02:00
function get_topic() {
invoked_by="$1"
topic_prefix="zio"
topic="test"
if [[ -n "$invoked_by" ]]; then
topic="$(echo "$invoked_by" | sed -r 's/sh.zio.//g' | sed -r 's/[.]+/-/g')"
fi
echo "${topic_prefix}_$topic"
}
2024-03-30 17:57:47 +01:00
function main() {
token_path="$(get_config_dir "sh.zio.notify")/token"
[[ ! -f "$token_path" ]] && die "'$token_path' does not exist"
token="$(cat "$token_path")"
hostname="$(hostname -s)"
2024-04-01 22:26:52 +02:00
invoked_by="$_invoked_by"
2024-03-30 17:57:47 +01:00
level_emoji="$(get_level_emoji "$_level")"
n_message="$_message"
n_priority="$(get_level_priority "$_level")"
n_title="$level_emoji [$hostname] $_title"
2024-04-01 22:26:52 +02:00
n_topic="$(get_topic "$_invoked_by")"
2024-03-30 17:57:47 +01:00
body='{
"message": "'$n_message'",
"priority": '$n_priority',
2024-04-01 22:26:52 +02:00
"tags": ["'$hostname'", "'$invoked_by'"],
2024-03-30 17:57:47 +01:00
"title": "'$n_title'",
"topic": "'$n_topic'"
}'
2024-04-01 22:26:52 +02:00
curl -d "$body" \
-H "Authorization: Bearer $token" \
-H "$(get_cache_header "$n_topic")" \
-H "Markdown: yes" \
"https://ntfy.zio.sh/"
2024-03-30 17:57:47 +01:00
}
if [[ $_PLUG_INVOKED != "true" ]]; then
base_dir="$(dirname "$(realpath -s "$0")")"
git_dir="$base_dir/../../.."
if [[ -d "$git_dir/.git" ]]; then
. "$git_dir/usr/local/libexec/zio/helpers/bash.sh"
else
. "/usr/local/libexec/zio/helpers/bash.sh"
fi
export -f get_config_dir
if [[ -d "$git_dir/.git" ]]; then
"$git_dir/usr/local/libexec/sodalite/invoker/src/invoke.sh" "$0" $@
else
"/usr/local/libexec/sodalite/invoker/src/invoke.sh" "$0" $@
fi
fi