diff --git a/src/bot/bot.py b/src/bot/bot.py index 5da2c19..0be9c17 100644 --- a/src/bot/bot.py +++ b/src/bot/bot.py @@ -816,6 +816,23 @@ async def _purge_on_startup(app: Application) -> None: await db_conn.close() +async def _safe_send(bot, chat_id, text: str): + """Envía un mensaje, con fallback a texto plano si falla el parseo Markdown. + + Los resúmenes generados por Claude suelen contener entidades Markdown + desbalanceadas (*, _, [, `) que hacen que Telegram rechace el mensaje con + BadRequest. Sin este fallback, el envío fallaba en silencio. + """ + try: + await bot.send_message(chat_id, text, parse_mode=ParseMode.MARKDOWN) + except Exception as e: + logger.warning("Envío Markdown falló, reintentando en texto plano", error=str(e)) + try: + await bot.send_message(chat_id, text) + except Exception as e2: + logger.error("Envío en texto plano también falló", chat_id=chat_id, error=str(e2)) + + async def _scheduler_loop(app: Application): while True: db_conn = None @@ -858,24 +875,23 @@ async def _scheduler_loop(app: Application): ) if summary: - await app.bot.send_message( - c, summary, parse_mode=ParseMode.MARKDOWN - ) + await _safe_send(app.bot, c, summary) else: - await app.bot.send_message( - c, - f"🔄 *{t}* — sin novedades significativas esta vez.", - parse_mode=ParseMode.MARKDOWN + await _safe_send( + app.bot, c, + f"🔄 *{t}* — sin novedades significativas esta vez." ) + except Exception as e: + logger.error("Tarea programada falló", + topic=t, session_id=s, error=str(e)) finally: await inner_db_conn.close() task = asyncio.create_task(_task()) _active_tasks[chat_id] = task - await app.bot.send_message( - chat_id, - f"🔄 Investigación automática iniciada: `{topic}`", - parse_mode=ParseMode.MARKDOWN + await _safe_send( + app.bot, chat_id, + f"🔄 Investigación automática iniciada: `{topic}`" ) except Exception as e: logger.warning("Scheduler loop error", error=str(e))