38 lines
1.1 KiB
Bash
38 lines
1.1 KiB
Bash
|
|
#!/bin/sh
|
||
|
|
# Sends a file (e.g. a freshly built APK) to a Telegram chat via a bot.
|
||
|
|
# Usage: scripts/notify_telegram.sh <path-to-file> ["optional caption"]
|
||
|
|
#
|
||
|
|
# Reads TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID from .telegram.env
|
||
|
|
# (gitignored) at the repo root, or from the environment if already set.
|
||
|
|
|
||
|
|
set -eu
|
||
|
|
|
||
|
|
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
|
||
|
|
REPO_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
|
||
|
|
ENV_FILE="$REPO_ROOT/.telegram.env"
|
||
|
|
|
||
|
|
if [ -f "$ENV_FILE" ]; then
|
||
|
|
# shellcheck disable=SC1090
|
||
|
|
. "$ENV_FILE"
|
||
|
|
fi
|
||
|
|
|
||
|
|
if [ -z "${TELEGRAM_BOT_TOKEN:-}" ] || [ -z "${TELEGRAM_CHAT_ID:-}" ]; then
|
||
|
|
echo "TELEGRAM_BOT_TOKEN / TELEGRAM_CHAT_ID not set (checked $ENV_FILE and environment)." >&2
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
FILE_PATH="${1:?Usage: notify_telegram.sh <path-to-file> [caption]}"
|
||
|
|
CAPTION="${2:-$(basename "$FILE_PATH")}"
|
||
|
|
|
||
|
|
if [ ! -f "$FILE_PATH" ]; then
|
||
|
|
echo "File not found: $FILE_PATH" >&2
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
curl -sS -F "chat_id=${TELEGRAM_CHAT_ID}" \
|
||
|
|
-F "document=@${FILE_PATH}" \
|
||
|
|
-F "caption=${CAPTION}" \
|
||
|
|
"https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendDocument" \
|
||
|
|
| tee /dev/stderr \
|
||
|
|
| grep -q '"ok":true'
|