sin-code-integration / scripts /telegram-bootstrap.sh
GitHub Actions
Deploy SIN-Code-Integration from GitHub Actions
a00031a
Raw
History Blame Contribute Delete
3.86 kB
#!/usr/bin/env bash
# telegram-bootstrap.sh — Universal SIN Telegram Bot Bootstrap
# Usage: ./scripts/telegram-bootstrap.sh [--bot-name <alias>] [--env-file <path>]
# Reads TELEGRAM_BOT_TOKEN (+ optional TELEGRAM_BOT_NAME) from .env,
# registers the bot with sin-telegrambot CLI, and bootstraps the chat_id.
# Zero manual steps required.
#
# Prerequisites:
# - sin-telegrambot CLI installed: ~/.local/bin/sin-telegrambot
# - Telegram.app installed on macOS (for auto-bootstrap via AppleScript)
# - .env file in project root with TELEGRAM_BOT_TOKEN set
#
# Part of: A2A-SIN-Agent-Template canonical scripts
# Canonical CLI: sin-telegrambot (https://github.com/Delqhi/sin-telegrambot-cli)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AGENT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
ENV_FILE="${AGENT_ROOT}/.env"
BOT_NAME_OVERRIDE=""
while [[ $# -gt 0 ]]; do
case "$1" in
--bot-name) BOT_NAME_OVERRIDE="$2"; shift 2 ;;
--env-file) ENV_FILE="$2"; shift 2 ;;
*) echo "Unknown arg: $1"; exit 1 ;;
esac
done
# --- Load .env ---
if [[ -f "$ENV_FILE" ]]; then
set -a
# shellcheck disable=SC1090
source "$ENV_FILE"
set +a
echo "[INFO] Loaded env from $ENV_FILE"
else
echo "[WARN] No .env file found at $ENV_FILE — relying on environment variables"
fi
TOKEN="${TELEGRAM_BOT_TOKEN:-}"
if [[ -z "$TOKEN" ]]; then
echo "[ERROR] TELEGRAM_BOT_TOKEN is not set. Add it to .env or export it."
exit 1
fi
# Determine bot alias: --bot-name > TELEGRAM_BOT_NAME env var > agent dir name
if [[ -n "$BOT_NAME_OVERRIDE" ]]; then
BOT_ALIAS="$BOT_NAME_OVERRIDE"
elif [[ -n "${TELEGRAM_BOT_NAME:-}" ]]; then
BOT_ALIAS="$TELEGRAM_BOT_NAME"
else
BOT_ALIAS="$(basename "$AGENT_ROOT" | tr '[:upper:]' '[:lower:]' | sed 's/a2a-sin-//')"
fi
echo "[INFO] Bot alias: $BOT_ALIAS"
# --- Check sin-telegrambot is installed ---
if ! command -v sin-telegrambot &>/dev/null; then
echo "[ERROR] sin-telegrambot CLI not found. Install it:"
echo " cp ~/.config/opencode/../dev/sin-telegrambot-cli/sin_telegrambot.py ~/.local/bin/sin-telegrambot"
echo " chmod +x ~/.local/bin/sin-telegrambot"
exit 1
fi
# --- Register bot (idempotent) ---
EXISTING=$(sin-telegrambot list 2>/dev/null | python3 -c "
import json,sys
bots = json.load(sys.stdin) if sys.stdin.read(1) == '[' else []
" 2>/dev/null || echo "[]")
echo "[INFO] Registering $BOT_ALIAS ..."
RESULT=$(sin-telegrambot register "$TOKEN" --name "$BOT_ALIAS" 2>&1)
echo "$RESULT"
if echo "$RESULT" | grep -q '"ok": true'; then
echo "[OK] Bot registered successfully."
elif echo "$RESULT" | grep -q 'registered'; then
echo "[OK] Bot already registered / updated."
else
echo "[WARN] Registration result unclear, continuing..."
fi
# --- Bootstrap chat_id ---
echo "[INFO] Bootstrapping chat_id for $BOT_ALIAS ..."
BOOT=$(sin-telegrambot bootstrap "$BOT_ALIAS" 2>&1)
echo "$BOOT"
if echo "$BOOT" | grep -q '"ok": true'; then
CHAT_ID=$(echo "$BOOT" | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('chat_id',''))" 2>/dev/null || echo "")
echo "[OK] chat_id discovered: $CHAT_ID"
# --- Write TELEGRAM_CHAT_ID back to .env if not already set ---
if [[ -f "$ENV_FILE" ]] && ! grep -q "TELEGRAM_CHAT_ID=" "$ENV_FILE"; then
echo "TELEGRAM_CHAT_ID=$CHAT_ID" >> "$ENV_FILE"
echo "[INFO] Wrote TELEGRAM_CHAT_ID=$CHAT_ID to $ENV_FILE"
fi
else
echo "[WARN] chat_id bootstrap didn't return ok — may need manual /start"
fi
# --- Send test message ---
echo "[INFO] Sending test message ..."
sin-telegrambot send "$BOT_ALIAS" "✅ <b>$(basename "$AGENT_ROOT")</b> bootstrap complete! Bot is online." --parse-mode HTML || true
echo ""
echo "=== Bootstrap complete ==="
echo " Alias: $BOT_ALIAS"
echo " CLI: sin-telegrambot send $BOT_ALIAS \"Hello World\""
echo " Status: sin-telegrambot status $BOT_ALIAS"
echo ""