feat: telegram bot command receiver (closes #135)#153
Merged
Conversation
Add inbound command receiving via the Telegram getUpdates long-polling API so users can interact with qracer remotely from chat. Builds on the existing TelegramAdapter (urllib only — no new dependencies). - TelegramBotPoller: long-polls getUpdates, tracks offset, filters by authorised chat_id, parses messages into BotCommand objects, and can send plain-text replies (auto-truncated to fit Telegram's limit). - BotCommand.parse: parses "/action arg1 arg2" with @botName suffix stripping and case folding. - Server: optional telegram_poller wired into _tick(); dispatches /status, /alerts, /alert, /tasks, /schedule, /help (and /start) using the existing alert and task stores. /analyze and /portfolio reply with a "use the CLI" message until deeper engine plumbing exists. - build_telegram_poller factory in notifications.factory. - cli serve: instantiates the poller from credentials and announces bot mode at startup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds inbound command receiving via the Telegram
getUpdateslong-polling API so users can interact withqracerremotely from chat. Builds on the existingTelegramAdapter— stdliburllibonly, no new dependencies.TelegramBotPollerinqracer/notifications/telegram_poller.py:getUpdates, tracks the update offset so messages are never returned twice.chat_id(other chats are silently ignored).BotCommandobjects.send_reply()posts plain-text replies, auto-truncating to fit Telegram's per-message limit (default 4000 chars).BotCommanddataclass with aparse(text)classmethod that strips the@botnamesuffix groups append (e.g./analyze@qracerbot AAPL) and lower-cases the action.Servergains an optionaltelegram_pollerconstructor param._tick()polls when configured (failures are logged, never raised) and calls_handle_bot_commandfor each command, replying viapoller.send_reply.Server._dispatch_bot_command:/help(and/start) — usage text/status— uptime, notification channels, autonomous on/off/alerts— list active alerts via the existingAlertStore/alert TICKER above|below PRICE— create a price alert/tasks— list active scheduled tasks via the existingTaskStore/schedule ACTION TICKER SCHEDULE— schedule a task (e.g./schedule analyze AAPL every 1h)/analyzeand/portfolio— reply with a friendly "use the qracer CLI" message; deeper engine plumbing for these is out of scope for this issue./helphint.build_telegram_pollerfactory inqracer/notifications/factory.pymirrorsbuild_notification_registryand returnsNonewhen credentials are missing.cli serveinstantiates the poller fromTELEGRAM_BOT_TOKEN/TELEGRAM_CHAT_IDand announces "Telegram bot: receiving commands" at startup.Closes #135.
How to test it
Automated:
I added 30 unit tests for the poller (
tests/notifications/test_telegram_poller.py) covering:BotCommand.parsehappy paths and rejection cases (empty, whitespace, non-slash, lone slash,/@bot).poll()parsing, offset tracking, chat-id filtering, non-text message skipping, non-command-text skipping.ok=falseerrors all return[]without raising.send_reply()success, truncation, empty-text short-circuit, HTTP / URL errors.I extended
tests/test_server.pywith 22 tests covering:_tickpolls Telegram when configured, skips when not, dispatches commands and replies._cmd_*handler in isolation, plus_format_duration.Manual smoke test (requires real bot):
You should see each command echoed back as a reply within ~1s of sending.