YouTubeLoader / bot /ui /keyboards.py
understanding's picture
Update bot/ui/keyboards.py
42291ba verified
# PATH: bot/ui/keyboards.py
from __future__ import annotations
from hydrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from bot.ui.callbacks import (
make,
AUTH_JSON, AUTH_CI, CANCEL, BACK,
MENU_HELP, MENU_AUTH, MENU_PROFILES, MENU_SPEEDTEST,
UP_GO, UP_EDIT, UP_PRIV, UP_CANCEL,
NAME_ORIGINAL, NAME_CAPTION, NAME_CUSTOM,
)
def main_menu_keyboard() -> InlineKeyboardMarkup:
return InlineKeyboardMarkup(
[
[
InlineKeyboardButton("πŸ” Add Profile", callback_data=make(MENU_AUTH)),
InlineKeyboardButton("πŸ‘€ Profiles", callback_data=make(MENU_PROFILES)),
],
[
InlineKeyboardButton("⚑ Speedtest", callback_data=make(MENU_SPEEDTEST)),
InlineKeyboardButton("❓ Help", callback_data=make(MENU_HELP)),
],
]
)
def auth_menu_keyboard() -> InlineKeyboardMarkup:
return InlineKeyboardMarkup(
[
[InlineKeyboardButton("πŸ“„ Send .json / Paste JSON", callback_data=make(AUTH_JSON))],
[InlineKeyboardButton("πŸ”‘ Send Client ID + Secret", callback_data=make(AUTH_CI))],
[InlineKeyboardButton("⬅️ Back", callback_data=make(BACK))],
]
)
def profiles_keyboard(profiles: list[dict], default_profile_id: str = "") -> InlineKeyboardMarkup:
"""
Must match handlers.py:
profiles_keyboard(profiles, default_id)
Expected callback actions:
pdef:<profile_id> set default
plog:<profile_id> get login url
pdel:<profile_id> delete profile (confirm by YES message)
"""
rows: list[list[InlineKeyboardButton]] = []
default_profile_id = str(default_profile_id or "")
for i, p in enumerate(profiles or [], start=1):
pid = str(p.get("profile_id") or "")
label = (p.get("channel_title") or p.get("label") or "Profile").strip()
has_refresh = bool(p.get("has_refresh"))
channel_id = str(p.get("channel_id") or "")
last_error = (p.get("last_error") or "").strip()
is_connected = has_refresh and bool(channel_id)
is_default = bool(pid and pid == default_profile_id)
prefix = "⭐ " if is_default else ""
status = "βœ…" if is_connected else ("πŸ”‘" if not has_refresh else "⚠️")
title = f"{prefix}{status} [{i}] {label}"[:50]
# Title/info row (noop)
rows.append([InlineKeyboardButton(title, callback_data=make("noop", pid[:8] or "p"))])
# Action row
btns: list[InlineKeyboardButton] = []
if pid:
btns.append(InlineKeyboardButton("βœ… Set default", callback_data=make("pdef", pid)))
btns.append(InlineKeyboardButton("πŸ”— Login", callback_data=make("plog", pid)))
btns.append(InlineKeyboardButton("πŸ—‘ Delete", callback_data=make("pdel", pid)))
if btns:
rows.append(btns)
# Optional error row
if last_error:
rows.append([InlineKeyboardButton(f"⚠️ {last_error[:48]}", callback_data=make("noop", "err"))])
rows.append([InlineKeyboardButton("⬅️ Back", callback_data=make(BACK))])
return InlineKeyboardMarkup(rows)
def upload_confirm_keyboard(privacy: str) -> InlineKeyboardMarkup:
priv_label = {"private": "πŸ”’ Private", "unlisted": "πŸ”— Unlisted", "public": "🌍 Public"}.get(
(privacy or "private").lower(), "πŸ”’ Private"
)
return InlineKeyboardMarkup(
[
[InlineKeyboardButton("βœ… Upload", callback_data=make(UP_GO))],
[
InlineKeyboardButton("✏️ Edit title/desc", callback_data=make(UP_EDIT)),
InlineKeyboardButton(priv_label, callback_data=make(UP_PRIV)),
],
[
InlineKeyboardButton("❌ Cancel", callback_data=make(UP_CANCEL)),
InlineKeyboardButton("⬅️ Back", callback_data=make(BACK)),
],
]
)
def filename_keyboard() -> InlineKeyboardMarkup:
"""
Used before upload to let user pick title source:
- Original filename
- Caption
- Custom
"""
return InlineKeyboardMarkup(
[
[InlineKeyboardButton("πŸ“„ Original filename", callback_data=make(NAME_ORIGINAL))],
[InlineKeyboardButton("πŸ“ From caption", callback_data=make(NAME_CAPTION))],
[InlineKeyboardButton("✍️ Custom name", callback_data=make(NAME_CUSTOM))],
[InlineKeyboardButton("❌ Cancel", callback_data=make(CANCEL))],
]
)