Skip to content

feat: forward unknown /commands to Claude as natural language#120

Open
inhyoe wants to merge 1 commit intoRichardAtCT:mainfrom
inhyoe:fix/frozen-message-text-assignment
Open

feat: forward unknown /commands to Claude as natural language#120
inhyoe wants to merge 1 commit intoRichardAtCT:mainfrom
inhyoe:fix/frozen-message-text-assignment

Conversation

@inhyoe
Copy link

@inhyoe inhyoe commented Feb 28, 2026

Summary

  • Adds a catch-all handler for unrecognized slash commands (e.g. /commit, /review, /tofu_at) that forwards them to Claude via agentic_text
  • Converts Telegram-safe underscored commands to hyphenated form (/tofu_atRun /tofu-at) since Telegram bot commands don't support hyphens
  • Uses an override_text parameter on agentic_text() instead of mutating update.message.text, which is frozen (immutable) in python-telegram-bot v20+

Problem

Claude Code supports project-specific slash commands via .claude/commands/*.md. When users send these commands through the Telegram bot (e.g. /tofu_at, /commit), they hit the "unknown command" path and are silently dropped.

A naive fix of setting update.message.text = prompt causes AttributeError: Attribute 'text' of class 'Message' can't be set! because Message objects are frozen dataclasses in python-telegram-bot v22.x.

Solution

  1. Register a filters.COMMAND handler at group 10 (after built-in command handlers) to catch unrecognized commands
  2. _agentic_unknown_command() transforms the command text and passes it via override_text parameter
  3. agentic_text() accepts optional override_text: str | None = None, using it over update.message.text when provided

Test plan

  • Send /tofu_at via Telegram → bot forwards Run /tofu-at to Claude (no AttributeError)
  • Send regular text message → agentic_text works as before (override_text=None)
  • Built-in commands (/start, /new, /status) still handled by their dedicated handlers
  • Verified with python-telegram-bot v22.6

🤖 Generated with Claude Code

Adds a catch-all handler for unrecognized slash commands (e.g. /commit,
/review, /tofu_at) that converts them to natural language prompts and
forwards them to Claude via the existing agentic_text handler.

This enables project-specific Claude Code slash commands defined in
.claude/commands/ to work seamlessly through the Telegram bot without
requiring explicit handler registration for each one.

Implementation notes:
- Registers a COMMAND filter handler at group=10 (after built-in commands)
- Converts underscores to hyphens (/tofu_at → /tofu-at) since Telegram
  bot commands don't support hyphens
- Uses an override_text parameter on agentic_text() instead of mutating
  update.message.text, which is frozen in python-telegram-bot v20+

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@FridayOpenClawBot
Copy link

PR #120 — feat: forward unknown /commands to Claude as natural language

Clean, well-reasoned feature. The group=10 placement ensures built-in handlers always win, and using override_text to work around python-telegram-bot's frozen Message objects is the right call.

One issue to consider:

The replace("_", "-") is applied to the full message_text, including any arguments the user passes. So /deploy my_service would become Run /deploy my-service, which mangles the argument. Consider scoping the replacement to just the command token:

parts = message_text.split(maxsplit=1)
cmd = parts[0].replace("_", "-")  # /tofu_at → /tofu-at
args = parts[1] if len(parts) > 1 else ""
prompt = f"Run {cmd} {args}".strip()

Minor:

  • The override_text parameter signature uses str | None union syntax — fine for Python 3.10+, but worth confirming the project's minimum Python version if it's not already pinned.
  • No tests in the diff. If there's a test suite covering the handler registration or agentic_text, a unit test for the underscore→hyphen transform and the override_text path would be a good addition.

Overall: ship-ready once the argument mangling is addressed.

Friday, AI assistant to @RichardAtCT

Copy link
Owner

@RichardAtCT RichardAtCT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! There's a bug in the command-to-natural-language forwarding:

The replace("_", "-") transformation is applied to the entire message including arguments, not just the command name. For example, /my_command some_argument would become my-command some-argument, mangling the user's input.

Please fix so the replacement only applies to the command portion (before the first space). Happy to re-review once fixed!

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.

3 participants