Skip to content

Validate Telegram bot username and prevent broken deep links - #31

Open
tiero wants to merge 2 commits into
mainfrom
claude/telegram-link-routing-a748nr
Open

Validate Telegram bot username and prevent broken deep links#31
tiero wants to merge 2 commits into
mainfrom
claude/telegram-link-routing-a748nr

Conversation

@tiero

@tiero tiero commented Jul 24, 2026

Copy link
Copy Markdown
Owner

Summary

Add validation for the TELEGRAM_BOT_USERNAME configuration to prevent deploying broken Telegram deep links (e.g., https://t.me/undefined?start=...). The changes include username normalization logic, deployment-time validation, and runtime safeguards.

Key Changes

  • New telegram.ts module with two functions:

    • normalizeBotUsername(): Validates and normalizes bot usernames (strips @, trims whitespace, enforces Telegram username rules: 5-32 chars, letters/digits/underscore, must start with letter)
    • buildTelegramDeepLink(): Safely constructs auth deep links, returning null if the username is invalid instead of emitting a broken link
  • Deployment validation in scripts/deploy.sh:

    • New check_telegram_config() function validates that if TELEGRAM_BOT_TOKEN is set, TELEGRAM_BOT_USERNAME must also be set and valid
    • Called during both deploy_api() and deploy_worker_api() to fail fast before deployment
    • Provides clear error messages for missing or invalid usernames
  • Runtime safety in api/src/index.ts:

    • Uses buildTelegramDeepLink() instead of string interpolation
    • Logs a warning and omits deep_link (returns null) if the username is invalid, rather than emitting a broken link
    • Prevents silent failures when configuration is incomplete
  • Type safety in api/src/bindings.ts:

    • Changed TELEGRAM_BOT_USERNAME from required to optional (?) to reflect that deploy.sh may skip empty secrets
    • Added comment explaining the runtime behavior
  • Documentation in docs/runbook.md:

    • Added troubleshooting section for broken deep links
    • Explains the root cause, how to check the configuration, and how to fix it

Implementation Details

The username validation regex (^[A-Za-z][A-Za-z0-9_]{4,31}$) enforces Telegram's official username rules and is duplicated in both the TypeScript code and the bash deployment script to catch errors at both layers. The normalizeBotUsername() function is defensive—it accepts common paste errors (leading @, surrounding whitespace) but rejects invalid formats early, preventing the API from ever emitting a link to a nonexistent user.

https://claude.ai/code/session_01ViuX4CKbdKKhoeyJeYoG9e

The auth challenge endpoint built the Telegram deep link as
https://t.me/${TELEGRAM_BOT_USERNAME}?start=... while only checking that
TELEGRAM_BOT_TOKEN was set. deploy.sh silently skips empty secrets, so a
deploy with the bot token but no username left the binding undefined and
the API handed out https://t.me/undefined?start=... — a link to a
Telegram user that doesn't exist.

- Add buildTelegramDeepLink/normalizeBotUsername: trim whitespace, strip
  a leading @, validate the username shape; return null (with a logged
  warning) instead of interpolating a broken value
- Mark TELEGRAM_BOT_USERNAME optional in Env to match runtime reality
- deploy.sh: refuse to deploy a bot token with a missing/invalid
  username, and normalize a pasted @botName
- Runbook: troubleshooting entry for the t.me/undefined symptom
- Unit tests for the deep link builder

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ViuX4CKbdKKhoeyJeYoG9e
@tiero
tiero marked this pull request as ready for review July 24, 2026 23:43
…routing-a748nr

# Conflicts:
#	api/src/index.ts
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 24, 2026

Copy link
Copy Markdown

Deploying claw-cash-landing-page with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2c2dd2b
Status: ✅  Deploy successful!
Preview URL: https://34eab34c.claw-cash-landing-page.pages.dev
Branch Preview URL: https://claude-telegram-link-routing.claw-cash-landing-page.pages.dev

View logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants