tao-shen Claude Opus 4.6 commited on
Commit
4fb4a97
·
1 Parent(s): e01d625

fix: validate Telegram bot token format before probe (skip junk tokens)

Browse files
Files changed (1) hide show
  1. scripts/sync_hf.py +11 -4
scripts/sync_hf.py CHANGED
@@ -143,11 +143,16 @@ def probe_telegram_api(bot_token: str, timeout: int = 10) -> str:
143
  print("[TELEGRAM] WARNING: All API bases failed! Telegram will not work.")
144
  return ""
145
 
 
 
 
 
 
146
  def get_telegram_bot_token() -> str:
147
  """Extract Telegram bot token from OpenClaw config or environment."""
148
  # 1. Environment variable
149
  token = os.environ.get("TELEGRAM_BOT_TOKEN", "")
150
- if token:
151
  return token
152
 
153
  # 2. From openclaw.json channels config
@@ -158,11 +163,11 @@ def get_telegram_bot_token() -> str:
158
  cfg = json.load(f)
159
  # Check channels.telegram.botToken (single account)
160
  tg = cfg.get("channels", {}).get("telegram", {})
161
- if tg.get("botToken"):
162
  return tg["botToken"]
163
  # Check channels.telegram.accounts.*.botToken (multi account)
164
  for acc in tg.get("accounts", {}).values():
165
- if isinstance(acc, dict) and acc.get("botToken"):
166
  return acc["botToken"]
167
  except Exception:
168
  pass
@@ -523,7 +528,9 @@ class OpenClawFullSync:
523
  else:
524
  print("[TELEGRAM] Official API works — no override needed")
525
  else:
526
- print("[TELEGRAM] No bot token found — skipping API probe")
 
 
527
 
528
  with open(config_path, "w") as f:
529
  json.dump(data, f, indent=2)
 
143
  print("[TELEGRAM] WARNING: All API bases failed! Telegram will not work.")
144
  return ""
145
 
146
+ def _is_valid_bot_token(token: str) -> bool:
147
+ """Check if a string looks like a valid Telegram bot token (digits:alphanumeric)."""
148
+ return bool(re.match(r'^\d+:[A-Za-z0-9_-]+$', token))
149
+
150
+
151
  def get_telegram_bot_token() -> str:
152
  """Extract Telegram bot token from OpenClaw config or environment."""
153
  # 1. Environment variable
154
  token = os.environ.get("TELEGRAM_BOT_TOKEN", "")
155
+ if token and _is_valid_bot_token(token):
156
  return token
157
 
158
  # 2. From openclaw.json channels config
 
163
  cfg = json.load(f)
164
  # Check channels.telegram.botToken (single account)
165
  tg = cfg.get("channels", {}).get("telegram", {})
166
+ if tg.get("botToken") and _is_valid_bot_token(tg["botToken"]):
167
  return tg["botToken"]
168
  # Check channels.telegram.accounts.*.botToken (multi account)
169
  for acc in tg.get("accounts", {}).values():
170
+ if isinstance(acc, dict) and acc.get("botToken") and _is_valid_bot_token(acc["botToken"]):
171
  return acc["botToken"]
172
  except Exception:
173
  pass
 
528
  else:
529
  print("[TELEGRAM] Official API works — no override needed")
530
  else:
531
+ print("[TELEGRAM] No valid bot token found — skipping API probe")
532
+ print("[TELEGRAM] Set TELEGRAM_BOT_TOKEN env var or configure in Control UI")
533
+ print("[TELEGRAM] Token format: 123456789:ABCdefGHIjklMNO (digits:alphanumeric)")
534
 
535
  with open(config_path, "w") as f:
536
  json.dump(data, f, indent=2)