diff --git a/.telegram.env.example b/.telegram.env.example new file mode 100644 index 0000000..03b18f5 --- /dev/null +++ b/.telegram.env.example @@ -0,0 +1,9 @@ +# Copy to .telegram.env (gitignored) and fill in real values. +# Used by scripts/notify_telegram.sh to send build artifacts (e.g. the APK) +# to a Telegram chat after a successful build. + +TELEGRAM_BOT_TOKEN=123456:ABC-DEF... +# Numeric chat id of the person/group the bot should message. A bot can't +# message you first — send it any message (e.g. /start) once, then run +# `curl "https://api.telegram.org/bot/getUpdates"` to find your chat id. +TELEGRAM_CHAT_ID=123456789 diff --git a/scripts/notify_telegram.sh b/scripts/notify_telegram.sh new file mode 100755 index 0000000..157797c --- /dev/null +++ b/scripts/notify_telegram.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# Sends a file (e.g. a freshly built APK) to a Telegram chat via a bot. +# Usage: scripts/notify_telegram.sh ["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 [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'