Spaces:
Sleeping
Sleeping
| # botfather-create.sh — Autonomous BotFather bot creation via Telegram.app AppleScript | |
| # Usage: ./scripts/botfather-create.sh --name <BotName> --username <botusername_bot> [--alias <local-alias>] | |
| # | |
| # What this does (zero manual steps): | |
| # 1. Opens Telegram.app and navigates to @BotFather | |
| # 2. Sends /newbot command | |
| # 3. Sends the display name | |
| # 4. Sends the username | |
| # 5. Captures the token from BotFather's reply via getUpdates | |
| # 6. Registers the new bot with sin-telegrambot CLI | |
| # 7. Bootstraps chat_id | |
| # | |
| # Requirements: | |
| # - Telegram.app installed and logged in | |
| # - sin-telegrambot CLI: ~/.local/bin/sin-telegrambot | |
| # - macOS (uses AppleScript / osascript) | |
| # - BotFather must not be rate-limiting (wait 60s if you just created a bot) | |
| # | |
| # Part of: A2A-SIN-Agent-Template canonical scripts | |
| # Canonical CLI: sin-telegrambot | |
| set -euo pipefail | |
| BOT_DISPLAY_NAME="" | |
| BOT_USERNAME="" | |
| BOT_ALIAS="" | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| --name) BOT_DISPLAY_NAME="$2"; shift 2 ;; | |
| --username) BOT_USERNAME="$2"; shift 2 ;; | |
| --alias) BOT_ALIAS="$2"; shift 2 ;; | |
| *) echo "Unknown arg: $1" >&2; exit 1 ;; | |
| esac | |
| done | |
| if [[ -z "$BOT_DISPLAY_NAME" || -z "$BOT_USERNAME" ]]; then | |
| echo "Usage: $0 --name <DisplayName> --username <botusername_bot> [--alias <local-alias>]" | |
| echo " --name Display name for the bot (e.g. 'My Agent Bot')" | |
| echo " --username Bot username, must end in _bot (e.g. 'myagentbot_bot')" | |
| echo " --alias Local alias for sin-telegrambot registry (default: username without _bot)" | |
| exit 1 | |
| fi | |
| # Derive alias from username if not given | |
| if [[ -z "$BOT_ALIAS" ]]; then | |
| BOT_ALIAS="${BOT_USERNAME%_bot}" | |
| BOT_ALIAS="${BOT_ALIAS,,}" | |
| fi | |
| # BotFather token is well-known (not secret) — it's the Telegram-provided token for @BotFather itself | |
| BOTFATHER_USERNAME="BotFather" | |
| echo "[INFO] Creating Telegram bot via BotFather (AppleScript)" | |
| echo " Display name: $BOT_DISPLAY_NAME" | |
| echo " Username: $BOT_USERNAME" | |
| echo " Local alias: $BOT_ALIAS" | |
| echo "" | |
| # Step 1: Open Telegram and navigate to BotFather, send /newbot | |
| osascript <<APPLESCRIPT | |
| tell application "Telegram" | |
| activate | |
| end tell | |
| delay 2.0 | |
| tell application "System Events" | |
| tell process "Telegram" | |
| -- Open search (Cmd+K) | |
| keystroke "k" using command down | |
| delay 1.0 | |
| -- Type BotFather | |
| keystroke "@${BOTFATHER_USERNAME}" | |
| delay 1.5 | |
| -- Select first result | |
| key code 36 | |
| delay 1.5 | |
| -- Send /newbot | |
| keystroke "/newbot" | |
| key code 36 | |
| delay 1.0 | |
| end tell | |
| end tell | |
| APPLESCRIPT | |
| echo "[INFO] Sent /newbot — waiting 4s for BotFather reply..." | |
| sleep 4 | |
| # Step 2: Send display name | |
| osascript <<APPLESCRIPT | |
| tell application "System Events" | |
| tell process "Telegram" | |
| keystroke "${BOT_DISPLAY_NAME}" | |
| key code 36 | |
| delay 1.0 | |
| end tell | |
| end tell | |
| APPLESCRIPT | |
| echo "[INFO] Sent display name '$BOT_DISPLAY_NAME' — waiting 4s..." | |
| sleep 4 | |
| # Step 3: Send username | |
| osascript <<APPLESCRIPT | |
| tell application "System Events" | |
| tell process "Telegram" | |
| keystroke "${BOT_USERNAME}" | |
| key code 36 | |
| delay 1.0 | |
| end tell | |
| end tell | |
| APPLESCRIPT | |
| echo "[INFO] Sent username '$BOT_USERNAME' — waiting 6s for token..." | |
| sleep 6 | |
| # Step 4: We need a BotFather listener token to poll for the created bot token | |
| # We use the SIN Fleet watcher bot (sin-telegrambot default) if available, | |
| # OR we ask the user to paste the token from the Telegram screen. | |
| # Strategy: try to capture token from macOS clipboard (BotFather's message is selectable) | |
| echo "[INFO] Attempting to capture bot token from Telegram (BotFather reply)..." | |
| # Try AppleScript clipboard approach: select all text in current chat, copy | |
| osascript <<APPLESCRIPT | |
| tell application "System Events" | |
| tell process "Telegram" | |
| keystroke "a" using command down | |
| delay 0.5 | |
| keystroke "c" using command down | |
| delay 0.5 | |
| end tell | |
| end tell | |
| APPLESCRIPT | |
| sleep 1 | |
| CLIPBOARD=$(pbpaste 2>/dev/null || echo "") | |
| # Extract token: format is digits:alphanumeric_string | |
| TOKEN=$(echo "$CLIPBOARD" | grep -oE '[0-9]{8,12}:[A-Za-z0-9_-]{35,}' | head -1 || echo "") | |
| if [[ -z "$TOKEN" ]]; then | |
| echo "[WARN] Could not auto-extract token from clipboard." | |
| echo "[INFO] Please copy the token from Telegram (BotFather reply) and paste it here:" | |
| read -r -p "Token: " TOKEN | |
| fi | |
| if [[ -z "$TOKEN" ]]; then | |
| echo "[ERROR] No token provided. Aborting." | |
| exit 1 | |
| fi | |
| echo "[OK] Token captured: ${TOKEN:0:20}..." | |
| # Step 5: Register with sin-telegrambot CLI | |
| echo "[INFO] Registering with sin-telegrambot CLI..." | |
| sin-telegrambot register "$TOKEN" --name "$BOT_ALIAS" | |
| # Step 6: Bootstrap chat_id | |
| echo "[INFO] Bootstrapping chat_id..." | |
| sin-telegrambot bootstrap "$BOT_ALIAS" || true | |
| # Step 7: Status check | |
| echo "" | |
| sin-telegrambot status "$BOT_ALIAS" || true | |
| # Step 8: Write .env snippet | |
| echo "" | |
| echo "=== Bot created! Add to your agent's .env ===" | |
| echo "TELEGRAM_BOT_TOKEN=$TOKEN" | |
| echo "TELEGRAM_BOT_NAME=$BOT_ALIAS" | |
| echo "" | |
| echo "=== Quick test ===" | |
| echo " sin-telegrambot send $BOT_ALIAS \"Hello from $BOT_DISPLAY_NAME!\"" | |