File size: 5,166 Bytes
720c6ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env bash
# 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!\""