""" telegram.py — Telegram bot helper za reels-app. Pošilja obvestila o končanih jobih, batch summary, napakah. Credentials se preberejo iz env vars (TELEGRAM_TOKEN, TELEGRAM_CHAT_ID). """ import os import urllib.request import urllib.parse import json def send_message(text, parse_mode="Markdown", disable_notification=False): """Pošlji sporočilo na Telegram. Vrne True ob uspehu, False ob napaki. text: ne sme biti daljši od 4096 znakov. parse_mode: 'Markdown' ali 'HTML' ali None. disable_notification: True za tiho obvestilo (brez zvonca). """ token = os.environ.get("TELEGRAM_TOKEN") chat_id = os.environ.get("TELEGRAM_CHAT_ID") if not token or not chat_id: return False # Telegram limit: 4096 chars if len(text) > 4090: text = text[:4087] + "..." data_dict = { "chat_id": chat_id, "text": text, } if parse_mode: data_dict["parse_mode"] = parse_mode if disable_notification: data_dict["disable_notification"] = "true" data = urllib.parse.urlencode(data_dict).encode() req = urllib.request.Request( f"https://api.telegram.org/bot{token}/sendMessage", data=data, method="POST", ) try: with urllib.request.urlopen(req, timeout=10) as resp: result = json.loads(resp.read().decode()) return result.get("ok", False) except Exception as e: print(f"⚠️ Telegram send failed: {e}", flush=True) return False def notify_job_done(job, base_url="https://reels.biba.live"): """Obvesti o končanem reelu.""" artist = job.get("parsed_artist", "") title = job.get("parsed_title", "") job_id = job.get("id", "") if artist and title: name = f"*{artist} — {title}*" elif title: name = f"*{title}*" else: name = f"`{job_id}`" duration = job.get("duration", 0) output_size_mb = job.get("output_size_mb") or round((job.get("output_size", 0) / 1024 / 1024), 1) text = ( f"✅ Reel pripravljen\n\n" f"{name}\n" f"⏱ {duration:.0f}s · 📦 {output_size_mb} MB\n\n" f"[Predogled & Download]({base_url})" ) return send_message(text) def notify_job_failed(job, error_msg=""): """Obvesti o neuspehu joba.""" artist = job.get("parsed_artist", "") title = job.get("parsed_title", "") fname = job.get("filename", "") if artist and title: name = f"*{artist} — {title}*" elif fname: name = f"`{fname}`" else: name = f"`{job.get('id', '?')}`" err_short = (error_msg or "neznana napaka")[:200] text = f"⚠️ Reel ni uspel\n\n{name}\n\n```\n{err_short}\n```" return send_message(text) def notify_batch_complete(batch_id, total, succeeded, failed, base_url="https://reels.biba.live"): """Obvesti o končanem batch-u (več jobov hkrati).""" if total <= 1: return False # ne pošiljaj batch summary za en sam job if failed == 0: emoji = "🎉" head = f"Batch končan: vsi {total} reelov pripravljeni" else: emoji = "✅" head = f"Batch končan: {succeeded}/{total} uspelih" if failed > 0: head += f", {failed} neuspelih" text = f"{emoji} {head}\n\n[Vsi reels →]({base_url})" return send_message(text) def notify_batch_started(batch_id, total): """Obvesti o začetku batch-a (samo če > 1 job).""" if total <= 1: return False text = f"🎬 Batch za {total} datotek dodan v vrsto…" return send_message(text, disable_notification=True)