fix(telegram): corrige escape de caracteres MarkdownV2 usando python3

This commit is contained in:
Evert Daniel Romero Garrido
2026-04-15 16:17:24 -06:00
parent 0c0126f3de
commit f8ee2a218e
+31 -19
View File
@@ -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 <start|success|failure> [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 <<EOF
${EMOJI} Pipeline *${REPO}* ${STATUS}
STATUS_ESC="$(telegram_escape "$STATUS")"
*Branch:* \`${BRANCH}\`
*Commit:* \`${COMMIT_SHORT}\`
*Build:* #${BUILD_NUMBER}
*Autor:* ${AUTHOR}
*Hora:* ${TIMESTAMP}
EOF
)
MESSAGE="${EMOJI} Pipeline *${REPO_ESC}* *${STATUS_ESC}*
*Branch:* \`${BRANCH_ESC}\`
*Commit:* \`${COMMIT_ESC}\`
*Build:* \#${BUILD_ESC}
*Autor:* ${AUTHOR_ESC}
*Hora:* ${TIMESTAMP_ESC}"
if [[ -n "$EXTRA_MESSAGE" ]]; then
MESSAGE="${MESSAGE}
*Detalle:* ${EXTRA_MESSAGE}"
*Detalle:* ${EXTRA_ESC}"
fi
# -------------------------------------------------------------------------------
@@ -101,9 +114,8 @@ RESPONSE=$(curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/se
-d "parse_mode=MarkdownV2")
if echo "$RESPONSE" | grep -q '"ok":true'; then
echo "[INFO] Notificación enviada a Telegram"
echo "[INFO] Notificacion enviada a Telegram"
else
echo "[WARN] Error enviando notificación: $RESPONSE" >&2
# No fallamos el pipeline por un error de notificación
echo "[WARN] Error enviando notificacion: $RESPONSE" >&2
exit 0
fi