diff --git a/homelab-ai-bot/telegram_bot.py b/homelab-ai-bot/telegram_bot.py index 735d5420..9d1f1a2c 100644 --- a/homelab-ai-bot/telegram_bot.py +++ b/homelab-ai-bot/telegram_bot.py @@ -758,10 +758,14 @@ async def handle_callback(update: Update, ctx: ContextTypes.DEFAULT_TYPE): ) -async def _send_daily_filmtipps(app_context: ContextTypes.DEFAULT_TYPE): - """Täglicher Cronjob: Filmtipps via Telegram senden.""" +async def _send_daily_filmtipps(context): + """Täglicher Cronjob: Filmtipps via Telegram senden. + + context kann ein CallbackContext (JobQueue) oder eine Application (asyncio-Loop) sein. + """ if not CHAT_ID: return + bot = getattr(context, "bot", None) or context try: from tools import savetv telecasts = savetv._scrape_epg() @@ -772,7 +776,7 @@ async def _send_daily_filmtipps(app_context: ContextTypes.DEFAULT_TYPE): return header = f"🎬 TV-Filmtipps für heute ({datetime.now().strftime('%d.%m.%Y')})\n" - await app_context.bot.send_message(chat_id=CHAT_ID, text=header) + await bot.send_message(chat_id=CHAT_ID, text=header) for f in films[:6]: tid = int(f.get("ITELECASTID", 0)) @@ -793,7 +797,7 @@ async def _send_daily_filmtipps(app_context: ContextTypes.DEFAULT_TYPE): InlineKeyboardButton("⏭ Nein", callback_data=f"savetv_skip_{tid}"), ] ]) - await app_context.bot.send_message( + await bot.send_message( chat_id=CHAT_ID, text=text, reply_markup=keyboard, @@ -838,17 +842,38 @@ def main(): app.add_handler(MessageHandler(filters.Document.ALL, handle_document)) app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) - app.job_queue.run_daily( - _send_daily_filmtipps, - time=dtime(hour=14, minute=0), - name="daily_filmtipps", - ) - log.info("Täglicher Filmtipp-Job registriert (14:00 Uhr)") + if app.job_queue is not None: + app.job_queue.run_daily( + _send_daily_filmtipps, + time=dtime(hour=14, minute=0), + name="daily_filmtipps", + ) + log.info("Täglicher Filmtipp-Job registriert (14:00 Uhr)") + else: + log.warning("JobQueue nicht verfügbar — Filmtipps werden per asyncio-Loop gesendet") + + async def _filmtipp_loop(application): + """Fallback: asyncio-basierter täglicher Filmtipp (wenn kein JobQueue).""" + while True: + now = datetime.now() + target = now.replace(hour=14, minute=0, second=0, microsecond=0) + if now >= target: + from datetime import timedelta + target += timedelta(days=1) + wait_secs = (target - now).total_seconds() + log.info("Filmtipp-Loop: nächster Run in %.0f Sek (%s)", wait_secs, target) + await asyncio.sleep(wait_secs) + try: + await _send_daily_filmtipps(application) + except Exception: + log.exception("Fehler im Filmtipp-Loop") async def post_init(application): await application.bot.set_my_commands(BOT_COMMANDS) log.info("Kommandomenü registriert") asyncio.create_task(_watchdog_loop()) + if application.job_queue is None: + asyncio.create_task(_filmtipp_loop(application)) _sd_notify("READY=1") log.info("Systemd Watchdog aktiv (50s Intervall)")