fix: reply keyboard (кнопки в строке ввода) вместо inline
Persistent кнопки внизу экрана: Статус, Баланс, Плечо, Помощь. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+27
-32
@@ -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 <b>RemizovTrader — Команды</b>\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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user