Compare commits
2 Commits
feature/is
...
fix/start-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c5eadc23b | ||
| 462f8763ab |
16
src/main.py
16
src/main.py
@@ -579,25 +579,10 @@ async def run(settings: Settings) -> None:
|
|||||||
command_handler = TelegramCommandHandler(telegram)
|
command_handler = TelegramCommandHandler(telegram)
|
||||||
|
|
||||||
# Register basic commands
|
# Register basic commands
|
||||||
async def handle_start() -> None:
|
|
||||||
"""Handle /start command."""
|
|
||||||
message = (
|
|
||||||
"<b>🤖 The Ouroboros Trading Bot</b>\n\n"
|
|
||||||
"AI-powered global stock trading agent with real-time notifications.\n\n"
|
|
||||||
"<b>Available commands:</b>\n"
|
|
||||||
"/help - Show this help message\n"
|
|
||||||
"/status - Current trading status\n"
|
|
||||||
"/positions - View holdings\n"
|
|
||||||
"/stop - Pause trading\n"
|
|
||||||
"/resume - Resume trading"
|
|
||||||
)
|
|
||||||
await telegram.send_message(message)
|
|
||||||
|
|
||||||
async def handle_help() -> None:
|
async def handle_help() -> None:
|
||||||
"""Handle /help command."""
|
"""Handle /help command."""
|
||||||
message = (
|
message = (
|
||||||
"<b>📖 Available Commands</b>\n\n"
|
"<b>📖 Available Commands</b>\n\n"
|
||||||
"/start - Welcome message\n"
|
|
||||||
"/help - Show available commands\n"
|
"/help - Show available commands\n"
|
||||||
"/status - Trading status (mode, markets, P&L)\n"
|
"/status - Trading status (mode, markets, P&L)\n"
|
||||||
"/positions - Current holdings\n"
|
"/positions - Current holdings\n"
|
||||||
@@ -722,7 +707,6 @@ async def run(settings: Settings) -> None:
|
|||||||
"<b>⚠️ Error</b>\n\nFailed to retrieve positions."
|
"<b>⚠️ Error</b>\n\nFailed to retrieve positions."
|
||||||
)
|
)
|
||||||
|
|
||||||
command_handler.register_command("start", handle_start)
|
|
||||||
command_handler.register_command("help", handle_help)
|
command_handler.register_command("help", handle_help)
|
||||||
command_handler.register_command("stop", handle_stop)
|
command_handler.register_command("stop", handle_stop)
|
||||||
command_handler.register_command("resume", handle_resume)
|
command_handler.register_command("resume", handle_resume)
|
||||||
|
|||||||
@@ -492,7 +492,8 @@ class TelegramCommandHandler:
|
|||||||
if not command_parts:
|
if not command_parts:
|
||||||
return
|
return
|
||||||
|
|
||||||
command_name = command_parts[0]
|
# Remove @botname suffix if present (for group chats)
|
||||||
|
command_name = command_parts[0].split("@")[0]
|
||||||
|
|
||||||
# Execute handler
|
# Execute handler
|
||||||
handler = self._commands.get(command_name)
|
handler = self._commands.get(command_name)
|
||||||
|
|||||||
@@ -230,6 +230,31 @@ class TestUpdateHandling:
|
|||||||
await handler._handle_update(update)
|
await handler._handle_update(update)
|
||||||
assert executed is False
|
assert executed is False
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_handle_command_with_botname(self) -> None:
|
||||||
|
"""Commands with @botname suffix are handled correctly."""
|
||||||
|
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||||
|
handler = TelegramCommandHandler(client)
|
||||||
|
|
||||||
|
executed = False
|
||||||
|
|
||||||
|
async def test_command() -> None:
|
||||||
|
nonlocal executed
|
||||||
|
executed = True
|
||||||
|
|
||||||
|
handler.register_command("start", test_command)
|
||||||
|
|
||||||
|
update = {
|
||||||
|
"update_id": 1,
|
||||||
|
"message": {
|
||||||
|
"chat": {"id": 456},
|
||||||
|
"text": "/start@mybot",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
await handler._handle_update(update)
|
||||||
|
assert executed is True
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_handle_update_error_isolation(self) -> None:
|
async def test_handle_update_error_isolation(self) -> None:
|
||||||
"""Errors in handlers don't crash the system."""
|
"""Errors in handlers don't crash the system."""
|
||||||
@@ -638,51 +663,6 @@ class TestStatusCommands:
|
|||||||
class TestBasicCommands:
|
class TestBasicCommands:
|
||||||
"""Test basic command implementations."""
|
"""Test basic command implementations."""
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_start_command_content(self) -> None:
|
|
||||||
"""Start command contains welcome message and command list."""
|
|
||||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
|
||||||
handler = TelegramCommandHandler(client)
|
|
||||||
|
|
||||||
mock_resp = AsyncMock()
|
|
||||||
mock_resp.status = 200
|
|
||||||
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
|
||||||
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
|
||||||
|
|
||||||
async def mock_start() -> None:
|
|
||||||
"""Mock /start handler."""
|
|
||||||
message = (
|
|
||||||
"<b>🤖 The Ouroboros Trading Bot</b>\n\n"
|
|
||||||
"AI-powered global stock trading agent with real-time notifications.\n\n"
|
|
||||||
"<b>Available commands:</b>\n"
|
|
||||||
"/help - Show this help message\n"
|
|
||||||
"/status - Current trading status\n"
|
|
||||||
"/positions - View holdings\n"
|
|
||||||
"/stop - Pause trading\n"
|
|
||||||
"/resume - Resume trading"
|
|
||||||
)
|
|
||||||
await client.send_message(message)
|
|
||||||
|
|
||||||
handler.register_command("start", mock_start)
|
|
||||||
|
|
||||||
with patch("aiohttp.ClientSession.post", return_value=mock_resp) as mock_post:
|
|
||||||
update = {
|
|
||||||
"update_id": 1,
|
|
||||||
"message": {
|
|
||||||
"chat": {"id": 456},
|
|
||||||
"text": "/start",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
await handler._handle_update(update)
|
|
||||||
|
|
||||||
# Verify message was sent
|
|
||||||
assert mock_post.call_count == 1
|
|
||||||
payload = mock_post.call_args.kwargs["json"]
|
|
||||||
assert "Ouroboros Trading Bot" in payload["text"]
|
|
||||||
assert "/help" in payload["text"]
|
|
||||||
assert "/status" in payload["text"]
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_help_command_content(self) -> None:
|
async def test_help_command_content(self) -> None:
|
||||||
"""Help command lists all available commands."""
|
"""Help command lists all available commands."""
|
||||||
@@ -698,7 +678,6 @@ class TestBasicCommands:
|
|||||||
"""Mock /help handler."""
|
"""Mock /help handler."""
|
||||||
message = (
|
message = (
|
||||||
"<b>📖 Available Commands</b>\n\n"
|
"<b>📖 Available Commands</b>\n\n"
|
||||||
"/start - Welcome message\n"
|
|
||||||
"/help - Show available commands\n"
|
"/help - Show available commands\n"
|
||||||
"/status - Trading status (mode, markets, P&L)\n"
|
"/status - Trading status (mode, markets, P&L)\n"
|
||||||
"/positions - Current holdings\n"
|
"/positions - Current holdings\n"
|
||||||
@@ -724,7 +703,6 @@ class TestBasicCommands:
|
|||||||
assert mock_post.call_count == 1
|
assert mock_post.call_count == 1
|
||||||
payload = mock_post.call_args.kwargs["json"]
|
payload = mock_post.call_args.kwargs["json"]
|
||||||
assert "Available Commands" in payload["text"]
|
assert "Available Commands" in payload["text"]
|
||||||
assert "/start" in payload["text"]
|
|
||||||
assert "/help" in payload["text"]
|
assert "/help" in payload["text"]
|
||||||
assert "/status" in payload["text"]
|
assert "/status" in payload["text"]
|
||||||
assert "/positions" in payload["text"]
|
assert "/positions" in payload["text"]
|
||||||
|
|||||||
Reference in New Issue
Block a user