fix(bot): migrate message formatting from MarkdownV2 to HTML

This commit is contained in:
dasemu committed 2026-03-12 16:17:11 +01:00
1 parent 1afe2b73e0
commit dabadb105b
1 file changed
+29 -30
+29 -30
View File
@@ -113,15 +113,14 @@ def _open_label(probability: float) -> str:
def format_station_prediction(station_key: str, prediction: dict) -> str:
"""Build the Telegram Markdown message for a single station prediction.
"""Build the Telegram HTML message for a single station prediction.
Args:
station_key: One of 'pajares', 'sanisidro', 'fuentes'.
prediction: Sub-dictionary for this station from predictions_latest.json.
Returns:
Formatted string ready to be sent with ParseMode.MARKDOWN_V2
(uses MarkdownV2 escaping where needed).
Formatted string ready to be sent with ParseMode.HTML.
"""
meta = STATIONS[station_key]
station_name = meta["name"]
@@ -166,16 +165,16 @@ def format_station_prediction(station_key: str, prediction: dict) -> str:
weather_block = ""
if weather_lines:
weather_block = "\n*Tiempo previsto:*\n" + "\n".join(weather_lines) + "\n"
weather_block = "\n<b>Tiempo previsto:</b>\n" + "\n".join(weather_lines) + "\n"
message = (
f"*{station_name} \\- Prediccion para manana*\n"
f"<b>{station_name} - Prediccion para manana</b>\n"
f"\n"
f"Estado: {open_str}\n"
f"Km esquiables: {km_str}\n"
f"Nivel de afluencia: {crowd_str}\n"
f"{weather_block}"
f"\n_Fuente: {source}_"
f"\n<i>Fuente: {source}</i>"
)
return message
@@ -184,19 +183,19 @@ def format_station_prediction(station_key: str, prediction: dict) -> str:
def _no_prediction_message(station_name: str) -> str:
"""Return a friendly error message when prediction data is unavailable."""
return (
f"Lo siento, no hay prediccion disponible para *{station_name}* en este "
f"momento\\.\n\n"
f"_El pipeline de datos se ejecuta diariamente\\. "
f"Intentalo de nuevo mas tarde\\._"
f"Lo siento, no hay prediccion disponible para <b>{station_name}</b> en este "
f"momento.\n\n"
f"<i>El pipeline de datos se ejecuta diariamente. "
f"Intentalo de nuevo mas tarde.</i>"
)
def _unavailable_message() -> str:
"""Return a message when the predictions file cannot be loaded at all."""
return (
"No se han podido cargar las predicciones\\.\n\n"
"_Asegurate de que el pipeline diario se ha ejecutado correctamente\\. "
"Intentalo de nuevo mas tarde\\._"
"No se han podido cargar las predicciones.\n\n"
"<i>Asegurate de que el pipeline diario se ha ejecutado correctamente. "
"Intentalo de nuevo mas tarde.</i>"
)
@@ -208,30 +207,30 @@ def _unavailable_message() -> str:
async def cmd_start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Handle the /start command with a welcome message."""
text = (
"*Bienvenido al bot de prediccion de estaciones de esqui* \\!\n\n"
"<b>Bienvenido al bot de prediccion de estaciones de esqui!</b>\n\n"
"Este bot te informa sobre las condiciones previstas para el dia "
"siguiente en tres estaciones de esqui del norte de Espana:\n\n"
" \\- *Valgrande\\-Pajares*\n"
" \\- *San Isidro*\n"
" \\- *Fuentes de Invierno*\n\n"
" - <b>Valgrande-Pajares</b>\n"
" - <b>San Isidro</b>\n"
" - <b>Fuentes de Invierno</b>\n\n"
"Las predicciones combinan reglas operacionales y modelos de "
"aprendizaje automatico basados en datos meteorologicos\\.\n\n"
"Escribe /help para ver los comandos disponibles\\."
"aprendizaje automatico basados en datos meteorologicos.\n\n"
"Escribe /help para ver los comandos disponibles."
)
await update.message.reply_text(text, parse_mode=ParseMode.MARKDOWN_V2)
await update.message.reply_text(text, parse_mode=ParseMode.HTML)
async def cmd_help(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Handle the /help command with a list of available commands."""
text = (
"*Comandos disponibles:*\n\n"
"/pajares \\— Prediccion para Valgrande\\-Pajares\n"
"/sanisidro \\— Prediccion para San Isidro\n"
"/fuentes \\— Prediccion para Fuentes de Invierno\n"
"/all \\— Predicciones para las tres estaciones\n"
"/help \\— Mostrar esta ayuda\n"
"<b>Comandos disponibles:</b>\n\n"
"/pajares — Prediccion para Valgrande-Pajares\n"
"/sanisidro — Prediccion para San Isidro\n"
"/fuentes — Prediccion para Fuentes de Invierno\n"
"/all — Predicciones para las tres estaciones\n"
"/help — Mostrar esta ayuda\n"
)
await update.message.reply_text(text, parse_mode=ParseMode.MARKDOWN_V2)
await update.message.reply_text(text, parse_mode=ParseMode.HTML)
async def _send_station(
@@ -249,14 +248,14 @@ async def _send_station(
if predictions is None:
text = _unavailable_message()
await update.message.reply_text(text, parse_mode=ParseMode.MARKDOWN_V2)
await update.message.reply_text(text, parse_mode=ParseMode.HTML)
return
station_pred = predictions.get(station_key)
if not station_pred:
logger.warning("No prediction entry for station '%s'", station_key)
text = _no_prediction_message(station_name)
await update.message.reply_text(text, parse_mode=ParseMode.MARKDOWN_V2)
await update.message.reply_text(text, parse_mode=ParseMode.HTML)
return
try:
@@ -265,7 +264,7 @@ async def _send_station(
logger.exception("Error formatting prediction for '%s': %s", station_key, exc)
text = _no_prediction_message(station_name)
await update.message.reply_text(text, parse_mode=ParseMode.MARKDOWN_V2)
await update.message.reply_text(text, parse_mode=ParseMode.HTML)
async def cmd_pajares(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: