mirror of
https://github.com/anten-ka/gotelegram_pro.git
synced 2026-05-19 14:36:05 +00:00
Fix Telegram bot template callbacks and telemt version checks
This commit is contained in:
@@ -402,6 +402,43 @@ def template_display_name(template_id: str) -> str:
|
||||
return template_id
|
||||
|
||||
|
||||
def pro_template_map(context: ContextTypes.DEFAULT_TYPE) -> Dict[str, str]:
|
||||
"""Return the short callback key -> template id map for this chat."""
|
||||
mapping = context.user_data.setdefault("pro_template_map", {})
|
||||
if not isinstance(mapping, dict):
|
||||
mapping = {}
|
||||
context.user_data["pro_template_map"] = mapping
|
||||
return mapping
|
||||
|
||||
|
||||
def resolve_pro_template_id(context: ContextTypes.DEFAULT_TYPE, key_or_id: str) -> str:
|
||||
"""Resolve a short Telegram callback key back to the real template id."""
|
||||
mapped = pro_template_map(context).get(key_or_id)
|
||||
if mapped:
|
||||
return str(mapped)
|
||||
|
||||
catalog = load_json(TEMPLATES_CATALOG) or {}
|
||||
for cat in catalog.get("categories", []):
|
||||
for tpl in cat.get("templates", []):
|
||||
template_id = str(tpl.get("id", ""))
|
||||
if hashlib.sha1(template_id.encode("utf-8")).hexdigest()[:12] == key_or_id:
|
||||
return template_id
|
||||
|
||||
return str(key_or_id)
|
||||
|
||||
|
||||
def pro_template_key_for_id(context: ContextTypes.DEFAULT_TYPE, template_id: str) -> str:
|
||||
"""Store a template id behind a short key that fits Telegram callback limits."""
|
||||
mapping = pro_template_map(context)
|
||||
template_id = str(template_id)
|
||||
for key, stored_id in mapping.items():
|
||||
if stored_id == template_id:
|
||||
return str(key)
|
||||
key = hashlib.sha1(template_id.encode("utf-8")).hexdigest()[:12]
|
||||
mapping[key] = template_id
|
||||
return key
|
||||
|
||||
|
||||
async def safe_edit_message(
|
||||
query,
|
||||
text: str,
|
||||
@@ -449,9 +486,11 @@ async def check_service_status(service: str) -> bool:
|
||||
|
||||
async def get_telemt_version() -> str:
|
||||
"""Get telemt version."""
|
||||
code, stdout, _ = await sh("telemt", "-v")
|
||||
if code == 0:
|
||||
return stdout.strip().split()[-1] if stdout else "unknown"
|
||||
for command in ("telemt", "/usr/local/bin/telemt", "/usr/bin/telemt"):
|
||||
for args in (("--version",), ("-V",)):
|
||||
code, stdout, _ = await sh(command, *args, timeout=5)
|
||||
if code == 0 and stdout.strip():
|
||||
return stdout.strip().split()[-1]
|
||||
return "unknown"
|
||||
|
||||
|
||||
@@ -1258,10 +1297,11 @@ async def cb_pro_category(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
|
||||
buttons = []
|
||||
for tpl in templates:
|
||||
key = pro_template_key_for_id(context, tpl["id"])
|
||||
buttons.append(
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
f"🎨 {tpl['name']}", callback_data=f"pro_tpl_{tpl['id']}"
|
||||
f"🎨 {tpl['name']}", callback_data=f"pro_tpl_{key}"
|
||||
)
|
||||
]
|
||||
)
|
||||
@@ -1276,7 +1316,8 @@ async def cb_pro_template(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
"""Show template preview and confirm."""
|
||||
query = update.callback_query
|
||||
data = query.data
|
||||
tpl_id = data.removeprefix("pro_tpl_")
|
||||
tpl_key = data.removeprefix("pro_tpl_")
|
||||
tpl_id = resolve_pro_template_id(context, tpl_key)
|
||||
|
||||
await query.answer()
|
||||
|
||||
@@ -1315,7 +1356,7 @@ async def cb_pro_template(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
buttons = [
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
"✅ Install", callback_data=f"pro_confirm_{tpl_id}"
|
||||
"✅ Install", callback_data=f"pro_confirm_{pro_template_key_for_id(context, tpl_id)}"
|
||||
)
|
||||
],
|
||||
[InlineKeyboardButton("« Back", callback_data="install_mode_pro")],
|
||||
@@ -1341,7 +1382,8 @@ async def cb_pro_confirm(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
"""
|
||||
query = update.callback_query
|
||||
data = query.data
|
||||
tpl_id = data.removeprefix("pro_confirm_")
|
||||
tpl_key = data.removeprefix("pro_confirm_")
|
||||
tpl_id = resolve_pro_template_id(context, tpl_key)
|
||||
|
||||
await query.answer()
|
||||
|
||||
@@ -2570,9 +2612,7 @@ async def cb_menu_update(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
|
||||
await safe_edit_message(query,"⏳ Checking for telemt updates...")
|
||||
|
||||
# Get current version
|
||||
cur_code, cur_out, _ = await sh("telemt", "--version")
|
||||
current = cur_out.strip() if cur_code == 0 else "unknown"
|
||||
current = await get_telemt_version()
|
||||
|
||||
# Check latest release from GitHub
|
||||
code, stdout, stderr = await sh(
|
||||
@@ -2586,7 +2626,7 @@ async def cb_menu_update(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
try:
|
||||
release = json.loads(stdout)
|
||||
latest = release.get("tag_name", "unknown")
|
||||
if latest == current:
|
||||
if latest.lstrip("v") == current.lstrip("v"):
|
||||
text = f"✅ telemt is already up to date ({html.escape(current)})"
|
||||
else:
|
||||
text = (
|
||||
|
||||
Reference in New Issue
Block a user