From f8ee2a218e19dcde27e979d63853a17b97eb225a Mon Sep 17 00:00:00 2001 From: Evert Daniel Romero Garrido Date: Wed, 15 Apr 2026 16:17:24 -0600 Subject: [PATCH] fix(telegram): corrige escape de caracteres MarkdownV2 usando python3 --- scripts/telegram-pipeline-notify.sh | 50 ++++++++++++++++++----------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/scripts/telegram-pipeline-notify.sh b/scripts/telegram-pipeline-notify.sh index 5013058..f3eef7d 100755 --- a/scripts/telegram-pipeline-notify.sh +++ b/scripts/telegram-pipeline-notify.sh @@ -1,14 +1,14 @@ #!/usr/bin/env bash # =============================================================================================================== # telegram-pipeline-notify.sh - Notificaciones enriquecidas de Telegram para pipelines de Bitbucket -# Descripción: +# Descripcion: # Envía alertas detalladas a Telegram incluyendo branch, commit, build number, # autor y estado del despliegue. Requiere TELEGRAM_BOT_TOKEN y TELEGRAM_CHAT_ID. # # Uso: # bash scripts/telegram-pipeline-notify.sh [mensaje_extra] # -# Autor: Área de Tecnología y Desarrollo - CCsoft +# Autor: Area de Tecnologia y Desarrollo - CCsoft # =============================================================================================================== set -euo pipefail @@ -20,20 +20,25 @@ TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN:-${DEV_TELEGRAM_BOT_TOKEN:-${PROD_TELEG TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID:-${DEV_TELEGRAM_CHAT_ID:-${PROD_TELEGRAM_CHAT_ID:-}}}" if [[ -z "$TELEGRAM_BOT_TOKEN" ]]; then - echo "[WARN] TELEGRAM_BOT_TOKEN no definido. Saltando notificación." >&2 + echo "[WARN] TELEGRAM_BOT_TOKEN no definido. Saltando notificacion." >&2 exit 0 fi if [[ -z "$TELEGRAM_CHAT_ID" ]]; then - echo "[WARN] TELEGRAM_CHAT_ID no definido. Saltando notificación." >&2 + echo "[WARN] TELEGRAM_CHAT_ID no definido. Saltando notificacion." >&2 exit 0 fi if [[ -z "$NOTIFY_TYPE" ]]; then - echo "[ERROR] Debe proporcionar el tipo de notificación: start, success o failure" >&2 + echo "[ERROR] Debe proporcionar el tipo de notificacion: start, success o failure" >&2 exit 1 fi +# Escapa caracteres reservados en MarkdownV2: _ * [ ] ( ) ~ ` > # + - = | { } . ! +telegram_escape() { + printf '%s' "$1" | python3 -c 'import re, sys; text = sys.stdin.read(); print(re.sub(r"([_*\[\]()~`>#+=|{}.!-])", r"\\\1", text), end="")' +} + # ------------------------------------------------------------------------------- # Variables de contexto del pipeline # ------------------------------------------------------------------------------- @@ -54,8 +59,17 @@ fi # Timestamp actual TIMESTAMP="$(date '+%Y-%m-%d %H:%M:%S %Z')" +# Escapar variables dinamicas para MarkdownV2 +BRANCH_ESC="$(telegram_escape "$BRANCH")" +COMMIT_ESC="$(telegram_escape "$COMMIT_SHORT")" +BUILD_ESC="$(telegram_escape "$BUILD_NUMBER")" +REPO_ESC="$(telegram_escape "$REPO")" +AUTHOR_ESC="$(telegram_escape "$AUTHOR")" +TIMESTAMP_ESC="$(telegram_escape "$TIMESTAMP")" +EXTRA_ESC="$(telegram_escape "$EXTRA_MESSAGE")" + # ------------------------------------------------------------------------------- -# Armar mensaje según tipo +# Armar mensaje segun tipo # ------------------------------------------------------------------------------- case "$NOTIFY_TYPE" in start) @@ -76,20 +90,19 @@ case "$NOTIFY_TYPE" in ;; esac -MESSAGE=$(cat <&2 - # No fallamos el pipeline por un error de notificación + echo "[WARN] Error enviando notificacion: $RESPONSE" >&2 exit 0 fi