From 7ed3fa7a09d2f1ef07f75c3b4caf9e12af3f0737 Mon Sep 17 00:00:00 2001 From: anor Date: Mon, 9 Mar 2026 06:41:46 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20reply=20keyboard=20(=D0=BA=D0=BD=D0=BE?= =?UTF-8?q?=D0=BF=D0=BA=D0=B8=20=D0=B2=20=D1=81=D1=82=D1=80=D0=BE=D0=BA?= =?UTF-8?q?=D0=B5=20=D0=B2=D0=B2=D0=BE=D0=B4=D0=B0)=20=D0=B2=D0=BC=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=BE=20inline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Persistent кнопки внизу экрана: Статус, Баланс, Плечо, Помощь. Co-Authored-By: Claude Opus 4.6 --- src/remizov_trader/cli.py | 59 ++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/src/remizov_trader/cli.py b/src/remizov_trader/cli.py index a3636e5..326dd72 100644 --- a/src/remizov_trader/cli.py +++ b/src/remizov_trader/cli.py @@ -257,15 +257,26 @@ def _send_with_keyboard(cfg, text: str, keyboard=None): "parse_mode": "HTML", } if keyboard: - payload["reply_markup"] = {"inline_keyboard": keyboard} + payload["reply_markup"] = reply_markup try: httpx.post(url, json=payload, timeout=10) except Exception: pass +# Persistent reply keyboard (bottom of screen) +_REPLY_KEYBOARD = { + "keyboard": [ + [{"text": "\U0001f4ca Статус"}, {"text": "\U0001f4b0 Баланс"}], + [{"text": "\u2696\ufe0f Плечо"}, {"text": "\u2753 Помощь"}], + ], + "resize_keyboard": True, + "is_persistent": True, +} + + def _send_help(cfg): - """Send help message with command buttons.""" + """Send help message with persistent reply keyboard.""" text = ( "\U0001f916 RemizovTrader — Команды\n" "\n" @@ -275,13 +286,7 @@ def _send_help(cfg): "/leverage N — установить плечо (1-10)\n" "/help — эта справка" ) - keyboard = [ - [{"text": "\U0001f4ca Статус", "callback_data": "/status"}, - {"text": "\U0001f4b0 Баланс", "callback_data": "/balance"}], - [{"text": "\u2696\ufe0f Плечо", "callback_data": "/leverage"}, - {"text": "\u2753 Помощь", "callback_data": "/help"}], - ] - _send_with_keyboard(cfg, text, keyboard) + _send_with_keyboard(cfg, text, _REPLY_KEYBOARD) def _send_balance(cfg): @@ -335,15 +340,25 @@ def _start_telegram_listener(cfg, get_db_fn): from .notify import format_status_report, send_telegram + # Map button text to commands + _button_map = { + "\U0001f4ca статус": "/status", + "\U0001f4b0 баланс": "/balance", + "\u2696\ufe0f плечо": "/leverage", + "\u2753 помощь": "/help", + } + def handle_command(text: str): - cmd = text.strip().split()[0].lower() + # Resolve button text to command + mapped = _button_map.get(text.strip().lower(), text.strip()) + cmd = mapped.split()[0].lower() if cmd == "/status": conn = get_db_fn() report = format_status_report(cfg, conn) conn.close() send_telegram(cfg.telegram, report) elif cmd == "/leverage": - _handle_leverage(cfg, text.strip(), send_telegram) + _handle_leverage(cfg, mapped, send_telegram) elif cmd == "/balance": _send_balance(cfg) elif cmd in ("/help", "/start"): @@ -354,10 +369,8 @@ def _start_telegram_listener(cfg, get_db_fn): base_url = f"https://api.telegram.org/bot{cfg.telegram.bot_token}" while True: try: - # Poll for both messages and callback queries (button presses) resp = httpx.get(f"{base_url}/getUpdates", - params={"offset": offset, "timeout": 30, - "allowed_updates": '["message","callback_query"]'}, + params={"offset": offset, "timeout": 30}, timeout=35) if resp.status_code != 200: time.sleep(5) @@ -365,29 +378,11 @@ def _start_telegram_listener(cfg, get_db_fn): updates = resp.json().get("result", []) for upd in updates: offset = upd["update_id"] + 1 - - # Regular message msg = upd.get("message", {}) text = msg.get("text", "") chat_id = str(msg.get("chat", {}).get("id", "")) if text and chat_id == cfg.telegram.chat_id: handle_command(text) - continue - - # Callback query (inline button press) - cb = upd.get("callback_query", {}) - if cb: - cb_chat = str(cb.get("message", {}).get("chat", {}).get("id", "")) - cb_data = cb.get("data", "") - cb_id = cb.get("id", "") - if cb_chat == cfg.telegram.chat_id and cb_data: - # Answer callback to remove loading indicator - try: - httpx.post(f"{base_url}/answerCallbackQuery", - json={"callback_query_id": cb_id}, timeout=5) - except Exception: - pass - handle_command(cb_data) except Exception: time.sleep(10)