Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
82 lines
2.2 KiB
Bash
Executable File
82 lines
2.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Send a text message to the ops Telegram group (forum topic).
|
|
# Never prints the bot token.
|
|
#
|
|
# Credentials from environment (GitHub Actions secrets or exported VPS .env):
|
|
# TELEGRAM_BOT_TOKEN
|
|
# TELEGRAM_GROUP_CHAT_ID + TELEGRAM_GROUP_THREAD_ID
|
|
# fallback TELEGRAM_CHAT_ID (no thread)
|
|
#
|
|
# Missing config / delivery errors fail the step (exit 1) so CI cannot look green
|
|
# while the Telegram message never left.
|
|
set -euo pipefail
|
|
|
|
LOG_PREFIX="[ghabilee-ops-telegram]"
|
|
|
|
trim_cred() {
|
|
local value="${1:-}"
|
|
value="${value#"${value%%[![:space:]]*}"}"
|
|
value="${value%"${value##*[![:space:]]}"}"
|
|
printf '%s' "$value"
|
|
}
|
|
|
|
normalize_thread_id() {
|
|
local raw
|
|
raw="$(trim_cred "${1:-}")"
|
|
raw="${raw%%#*}"
|
|
raw="$(trim_cred "$raw")"
|
|
if [[ "$raw" =~ ^[1-9][0-9]*$ ]]; then
|
|
printf '%s' "$raw"
|
|
fi
|
|
}
|
|
|
|
if [[ $# -ge 1 ]]; then
|
|
TEXT="$*"
|
|
else
|
|
TEXT="$(cat || true)"
|
|
fi
|
|
TEXT="${TEXT%"${TEXT##*[![:space:]]}"}"
|
|
if [[ -z "$TEXT" ]]; then
|
|
echo "$LOG_PREFIX failed: empty message" >&2
|
|
exit 1
|
|
fi
|
|
|
|
TOKEN="$(trim_cred "${TELEGRAM_BOT_TOKEN:-}")"
|
|
GROUP_CHAT_ID="$(trim_cred "${TELEGRAM_GROUP_CHAT_ID:-}")"
|
|
PRIVATE_CHAT_ID="$(trim_cred "${TELEGRAM_CHAT_ID:-}")"
|
|
THREAD_ID="$(normalize_thread_id "${TELEGRAM_GROUP_THREAD_ID:-}")"
|
|
CHAT_ID="$GROUP_CHAT_ID"
|
|
if [[ -z "$CHAT_ID" ]]; then
|
|
CHAT_ID="$PRIVATE_CHAT_ID"
|
|
THREAD_ID=""
|
|
fi
|
|
|
|
if [[ -z "$TOKEN" || -z "$CHAT_ID" ]]; then
|
|
echo "$LOG_PREFIX failed: TELEGRAM_BOT_TOKEN / chat id not configured" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [[ -n "$GROUP_CHAT_ID" && -z "$THREAD_ID" && -n "${TELEGRAM_GROUP_THREAD_ID:-}" ]]; then
|
|
echo "$LOG_PREFIX warning: invalid TELEGRAM_GROUP_THREAD_ID — sending without forum topic" >&2
|
|
fi
|
|
|
|
CURL_ARGS=(
|
|
-sS
|
|
-X POST
|
|
"https://api.telegram.org/bot${TOKEN}/sendMessage"
|
|
--data-urlencode "chat_id=${CHAT_ID}"
|
|
--data-urlencode "text=${TEXT}"
|
|
--data-urlencode "disable_web_page_preview=true"
|
|
)
|
|
if [[ "$THREAD_ID" =~ ^[1-9][0-9]*$ ]]; then
|
|
CURL_ARGS+=(--data-urlencode "message_thread_id=${THREAD_ID}")
|
|
fi
|
|
|
|
RESP="$(curl --connect-timeout 20 --max-time 30 "${CURL_ARGS[@]}" || true)"
|
|
if ! printf '%s' "$RESP" | grep -q '"ok":true'; then
|
|
echo "$LOG_PREFIX sendMessage failed: ${RESP:0:500}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "$LOG_PREFIX sent to chat_id=${CHAT_ID}${THREAD_ID:+ thread=${THREAD_ID}}"
|