Skip to content

Your lightweight open-source personal agent.

fix(telegram): preserve whitespace-delimited command arguments

Shuhan Si009caef55543
diff --git a/nanobot/channels/telegram/runtime.py b/nanobot/channels/telegram/runtime.py
index 152dd5e9e..a12e808c5 100644
--- a/nanobot/channels/telegram/runtime.py
+++ b/nanobot/channels/telegram/runtime.py
@@ -549,7 +549,7 @@ class TelegramChannel(BaseChannel):
# Canonical hyphenated commands stay on a separate handler (below).
TELEGRAM_BUS_SLASH_COMMAND_RE = re.compile(
r"^/(?:new|compact|stop|restart|status|dream|history|goal|trigger|pairing|model|skill"
- r"|dream_log|dream_restore|dream_prompt|evaluator_prompt|evaluator-prompt)(?:@\w+)?(?:\s+.*)?$"
+ r"|dream_log|dream_restore|dream_prompt|evaluator_prompt|evaluator-prompt)(?:@\w+)?(?:\s+[\s\S]*)?$"
)
@classmethod
@@ -606,10 +606,9 @@ class TelegramChannel(BaseChannel):
"""Map Telegram-safe command aliases back to canonical nanobot commands."""
if not content.startswith("/"):
return content
- for alias, canonical in _TELEGRAM_COMMAND_ALIASES.items():
- if content == alias or content.startswith(f"{alias} "):
- return canonical + content[len(alias):]
- return content
+ command = content.split(maxsplit=1)[0]
+ canonical = _TELEGRAM_COMMAND_ALIASES.get(command, command)
+ return canonical + content[len(command):]
async def start(self) -> None:
"""Start the Telegram bot, rebuilding the app whenever polling stalls."""
@@ -708,7 +707,7 @@ class TelegramChannel(BaseChannel):
self._app.add_handler(
MessageHandler(
filters.Regex(
- r"^/(?:dream-log|dream-restore|dream-prompt)(?:@\w+)?(?:\s+.*)?$"
+ r"^/(?:dream-log|dream-restore|dream-prompt)(?:@\w+)?(?:\s+[\s\S]*)?$"
),
self._forward_command,
)
@@ -1944,10 +1943,9 @@ class TelegramChannel(BaseChannel):
# Strip @bot_username suffix if present
content = message.text or ""
- if content.startswith("/") and "@" in content:
- cmd_part, *rest = content.split(" ", 1)
- cmd_part = cmd_part.split("@")[0]
- content = f"{cmd_part} {rest[0]}" if rest else cmd_part
+ if content.startswith("/"):
+ command = content.split(maxsplit=1)[0]
+ content = command.split("@", 1)[0] + content[len(command):]
content = self._normalize_telegram_command(content)
await self._handle_message(
diff --git a/nanobot/channels/telegram/tests/test_telegram_channel.py b/nanobot/channels/telegram/tests/test_telegram_channel.py
index d58216fb8..6d99cba27 100644
--- a/nanobot/channels/telegram/tests/test_telegram_channel.py
+++ b/nanobot/channels/telegram/tests/test_telegram_channel.py
@@ -2430,6 +2430,33 @@ async def test_forward_command_pairs_unauthorized_private_user(monkeypatch) -> N
assert "ABCD-EFGH" in channel._app.bot.sent_messages[0]["text"]
[email protected]("separator", [" ", "\t", "\n", "\r\n", " \n"])
[email protected]("suffix", ["", "@nanobot_test"])
+async def test_forward_command_preserves_whitespace_and_argument_mentions(separator, suffix) -> None:
+ channel = TelegramChannel(
+ TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], group_policy="open"),
+ MessageBus(),
+ )
+ _install_ready_app(channel)
+ handled = []
+
+ async def capture_handle(**kwargs) -> None:
+ handled.append(kwargs)
+
+ channel._handle_message = capture_handle
+ arguments = "[email protected]\nkeep the second line"
+ text = f"/dream_prompt{suffix}{separator}{arguments}"
+ await channel._forward_command(_make_telegram_update(text=text), None)
+
+ assert handled[0]["content"] == f"/dream-prompt{separator}{arguments}"
+
+
[email protected]("text", ["/goal@nanobot_test\nfirst\nsecond", "/dream_prompt first\nsecond"])
+def test_bus_command_regex_accepts_multiline_arguments(text) -> None:
+ assert TelegramChannel.TELEGRAM_BUS_SLASH_COMMAND_RE.fullmatch(text)
+
+
@pytest.mark.asyncio
async def test_forward_command_preserves_dream_log_args_and_strips_bot_suffix() -> None:
channel = TelegramChannel(