fix(telegram): corrige escape de caracteres MarkdownV2 usando python3
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# ===============================================================================================================
|
# ===============================================================================================================
|
||||||
# telegram-pipeline-notify.sh - Notificaciones enriquecidas de Telegram para pipelines de Bitbucket
|
# 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,
|
# Envía alertas detalladas a Telegram incluyendo branch, commit, build number,
|
||||||
# autor y estado del despliegue. Requiere TELEGRAM_BOT_TOKEN y TELEGRAM_CHAT_ID.
|
# autor y estado del despliegue. Requiere TELEGRAM_BOT_TOKEN y TELEGRAM_CHAT_ID.
|
||||||
#
|
#
|
||||||
# Uso:
|
# Uso:
|
||||||
# bash scripts/telegram-pipeline-notify.sh <start|success|failure> [mensaje_extra]
|
# 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
|
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:-}}}"
|
TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID:-${DEV_TELEGRAM_CHAT_ID:-${PROD_TELEGRAM_CHAT_ID:-}}}"
|
||||||
|
|
||||||
if [[ -z "$TELEGRAM_BOT_TOKEN" ]]; then
|
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
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -z "$TELEGRAM_CHAT_ID" ]]; then
|
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
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -z "$NOTIFY_TYPE" ]]; then
|
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
|
exit 1
|
||||||
fi
|
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
|
# Variables de contexto del pipeline
|
||||||
# -------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------
|
||||||
@@ -54,8 +59,17 @@ fi
|
|||||||
# Timestamp actual
|
# Timestamp actual
|
||||||
TIMESTAMP="$(date '+%Y-%m-%d %H:%M:%S %Z')"
|
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
|
case "$NOTIFY_TYPE" in
|
||||||
start)
|
start)
|
||||||
@@ -76,20 +90,19 @@ case "$NOTIFY_TYPE" in
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
MESSAGE=$(cat <<EOF
|
STATUS_ESC="$(telegram_escape "$STATUS")"
|
||||||
${EMOJI} Pipeline *${REPO}* ${STATUS}
|
|
||||||
|
|
||||||
*Branch:* \`${BRANCH}\`
|
MESSAGE="${EMOJI} Pipeline *${REPO_ESC}* *${STATUS_ESC}*
|
||||||
*Commit:* \`${COMMIT_SHORT}\`
|
|
||||||
*Build:* #${BUILD_NUMBER}
|
*Branch:* \`${BRANCH_ESC}\`
|
||||||
*Autor:* ${AUTHOR}
|
*Commit:* \`${COMMIT_ESC}\`
|
||||||
*Hora:* ${TIMESTAMP}
|
*Build:* \#${BUILD_ESC}
|
||||||
EOF
|
*Autor:* ${AUTHOR_ESC}
|
||||||
)
|
*Hora:* ${TIMESTAMP_ESC}"
|
||||||
|
|
||||||
if [[ -n "$EXTRA_MESSAGE" ]]; then
|
if [[ -n "$EXTRA_MESSAGE" ]]; then
|
||||||
MESSAGE="${MESSAGE}
|
MESSAGE="${MESSAGE}
|
||||||
*Detalle:* ${EXTRA_MESSAGE}"
|
*Detalle:* ${EXTRA_ESC}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# -------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------
|
||||||
@@ -101,9 +114,8 @@ RESPONSE=$(curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/se
|
|||||||
-d "parse_mode=MarkdownV2")
|
-d "parse_mode=MarkdownV2")
|
||||||
|
|
||||||
if echo "$RESPONSE" | grep -q '"ok":true'; then
|
if echo "$RESPONSE" | grep -q '"ok":true'; then
|
||||||
echo "[INFO] Notificación enviada a Telegram"
|
echo "[INFO] Notificacion enviada a Telegram"
|
||||||
else
|
else
|
||||||
echo "[WARN] Error enviando notificación: $RESPONSE" >&2
|
echo "[WARN] Error enviando notificacion: $RESPONSE" >&2
|
||||||
# No fallamos el pipeline por un error de notificación
|
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user