Skip to content

fix(bots): avoid Telegram 64-byte callback_data overflow on bots menu - #1

Closed
gordonkoehn wants to merge 2 commits into
mainfrom
fix/bot-name-callback-overflow
Closed

fix(bots): avoid Telegram 64-byte callback_data overflow on bots menu#1
gordonkoehn wants to merge 2 commits into
mainfrom
fix/bot-name-callback-overflow

Conversation

@gordonkoehn

@gordonkoehn gordonkoehn commented Apr 19, 2026

Copy link
Copy Markdown
Owner

Problem

/bots fails with telegram.error.BadRequest: Button_data_invalid when any bot has a name long enough that bots:bot_detail:{bot_name} exceeds Telegram's 64-byte callback_data cap (16-byte prefix + name).

Reproduction

Deploy any bot whose name is ≥ 49 chars. Hummingbot's deploy templates produced a 54-char name in my own run:

pmm-EPIC-USDT-kucoin-v1-20260418-163719-20260418-163719   (54 chars)

callback_data = 70 bytes → menu never renders. Log:

handlers.bots.menu - ERROR - Error showing bots menu: Button_data_invalid
telegram.error.BadRequest: Button_data_invalid

Fix

Switch _build_main_menu_keyboard to emit callback_data=f"bots:bot_idx:{i}" and stash the ordered bot-name list in context.chat_data["bots_main_list"] before the keyboard is built. A new dispatcher branch resolves bot_idx back to the name and calls show_bot_detail().

  • handlers/bots/menu.py: index-based callback + chat_data cache
  • handlers/bots/__init__.py: new bot_idx branch next to existing bot_detail

Legacy bots:bot_detail:{name} branch is retained for callers that pass known-short names. Pattern mirrors the existing ctrl_idx mechanism in the same dispatcher.

Scope

  • Two files, ~50 net lines.
  • No behavioral change for bots with short names.
  • No new dependencies.
  • Resilient to cache miss: if bots_main_list is absent or the index is out of range (e.g. stale button after a restart), logs and re-opens the menu instead of crashing.

Long bot_names (e.g. hummingbot-deploy's
'pmm-EPIC-USDT-kucoin-v1-<ts>-<ts>' format, 54 chars) plus the
'bots:bot_detail:' prefix (16 bytes) exceed Telegram's 64-byte
InlineKeyboardButton.callback_data cap, producing
telegram.error.BadRequest: Button_data_invalid and rendering /bots
unusable.

Switch the main bots menu to index-based callback_data
(bots:bot_idx:{i}), stashing the ordered bot-name list in
context.chat_data['bots_main_list'] when the menu is built. A new
dispatcher branch resolves the index back to the name and calls
show_bot_detail(). Legacy bots:bot_detail:{name} branch retained for
callers that pass known-short names.

Pattern mirrors the existing ctrl_idx mechanism in the same dispatcher.
@gordonkoehn

Copy link
Copy Markdown
Owner Author

Upstreamed as hummingbot/condor#61.

Validated end-to-end on a 72-char bot name in live deploy — postmortem at gordonkoehn/riskcraft#164. Keeping this fork PR open for traceability; will close once upstream merges.

… import

Addresses three Copilot review comments on hummingbot#61:

1. Use context.user_data (not chat_data) for the cached bot-name list so
   group-chat users don't overwrite each other's state. Matches the
   per-user convention used elsewhere in this handler.

2. Prefer the stable name-based callback_data form when it fits the
   64-byte cap, fall back to index-based only when the name would
   overflow. Short-named bots keep the original upstream behavior:
   buttons on stale menu messages stay correct across list reordering.
   Only long-named bots (>= 49 chars) use the index-based form, where
   list stability is a hard trade-off vs not crashing.

3. Drop the redundant local re-import of show_bots_menu inside the
   bot_idx dispatcher branch — already imported at module scope.

Validated end-to-end: same 72-char bot name exercises the index path
cleanly.
@gordonkoehn

Copy link
Copy Markdown
Owner Author

Superseded by hummingbot/condor#61 — this fork PR was only a self-review surface. Closing here; keeping the fix/bot-name-callback-overflow branch since it is the head of the upstream PR.

Do not delete this branch until upstream merges.

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.

1 participant