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