87 lines
2.9 KiB
Bash
87 lines
2.9 KiB
Bash
# Sends a text message to Rocket.Chat via webhook.
|
|
# Uses global $rocketChatUrl.
|
|
# $1 (text): message text
|
|
# [$2] (attachFile): optional path to a file to attach
|
|
function rocketChatSend() {
|
|
[[ -n "${rocketChatUrl:-}" ]] || { appError "rocketChatUrl is not configured"; return 1; }
|
|
|
|
local text="$1"
|
|
local attachFile="${2:-}"
|
|
|
|
local payload
|
|
if [[ -n "$attachFile" ]]; then
|
|
local attachText
|
|
if [[ -f "$attachFile" ]]; then
|
|
attachText=$(< "$attachFile")
|
|
else
|
|
attachText="_(file not found)_"
|
|
fi
|
|
payload=$(jq -nc \
|
|
--arg text "$text" \
|
|
--arg title "$attachFile" \
|
|
--arg attachText "$attachText" \
|
|
'{"text": $text, "attachments": [{"title": $title, "text": ("```shell\n" + $attachText + "\n```"), "collapsed": true}]}')
|
|
else
|
|
payload=$(jq -nc --arg text "$text" '{"text": $text}')
|
|
fi
|
|
|
|
local error
|
|
runError error curl -sf -X POST -H "Content-Type: application/json" \
|
|
--data "$payload" --retry 2 --retry-delay 1 --max-time 10 -- "$rocketChatUrl" \
|
|
|| { appError "Failed to send Rocket.Chat message${error:+: $error}"; return 1; }
|
|
}
|
|
|
|
# Formats a diagnostic report message for Rocket.Chat and outputs it to stdout.
|
|
# Uses globals: $hostName.
|
|
# $1 (status): report status (uppercased automatically)
|
|
# $2 (report): report text (plain multiline string)
|
|
function rocketChatFormatReport() {
|
|
local status="${1^^}"
|
|
local report="$2"
|
|
|
|
local -a lines=(
|
|
"🚨 *KUBE Alert: $status*"
|
|
""
|
|
"*Source:* \`$hostName\`"
|
|
"*Generated at:* \`$(TZ='Europe/Prague' date '+%Y-%m-%d %H:%M:%S') (Europe/Prague)\`"
|
|
""
|
|
"$report"
|
|
)
|
|
|
|
printf '%s\n' "${lines[@]}"
|
|
}
|
|
|
|
# Sends a text message to Rocket.Chat via webhook.
|
|
# Uses global $rocketChatUrl.
|
|
# $1 (text): message text
|
|
# [$2] (attachFile): optional path to a file to attach
|
|
function rocketChatSend2() {
|
|
[[ -n "${rocketChatUrl:-}" ]] || { appError "rocketChatUrl is not configured"; return 1; }
|
|
|
|
local text="$1"
|
|
local attachFile="${2:-}"
|
|
|
|
local payload
|
|
if [[ -n "$attachFile" ]]; then
|
|
local attachText
|
|
if [[ -f "$attachFile" ]]; then
|
|
attachText=$(< "$attachFile")
|
|
else
|
|
attachText="_(file not found)_"
|
|
fi
|
|
|
|
payload=$(jq -nc \
|
|
--arg text "$text" \
|
|
--arg title "$attachFile" \
|
|
--arg attachText "$attachText" \
|
|
'{"text": ($text + "\n\n<details>\n<summary>📄 " + $title + " (Click to expand)</summary>\n\n```shell\n" + $attachText + "\n```\n</details>")}')
|
|
else
|
|
payload=$(jq -nc --arg text "$text" '{"text": $text}')
|
|
fi
|
|
|
|
local error
|
|
runError error curl -sf -X POST -H "Content-Type: application/json" \
|
|
--data "$payload" --retry 2 --retry-delay 1 --max-time 10 -- "$rocketChatUrl" \
|
|
|| { appError "Failed to send Rocket.Chat message${error:+: $error}"; return 1; }
|
|
}
|