#!/bin/bash # TSHARPS CI — Telegram Notification Sender # Sends CI results to the CICD Pipeline Telegram topic. # Called by ci-runner.sh with results. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" CONFIG="$SCRIPT_DIR/ci-config.json" BOT_TOKEN=$(python3 -c "import json; print(json.load(open('$CONFIG'))['telegram_bot_token'])") CHAT_ID=$(python3 -c "import json; print(json.load(open('$CONFIG'))['telegram_chat_id'])") TOPIC_ID=$(python3 -c "import json; print(json.load(open('$CONFIG'))['telegram_topic_id'])") TAG_USERS=$(python3 -c "import json; users=json.load(open('$CONFIG')).get('tag_users_on_fail',[]); print(' '.join('@'+u for u in users))") BRANCH="$1" COMMIT="$2" STATUS="$3" # "pass" or "fail" or "error" SUMMARY="$4" # e.g., "940 passed, 0 failed, 393 skipped" FEATURES="$5" # e.g., "31/31 features verified" DURATION="$6" # e.g., "12.3s" ACTOR="$7" # who pushed REPORT_FILE="$8" # optional HTML failure report path SUITE_INFO="$9" # e.g., "Suite: light | 202 tests not in suite" if [ "$STATUS" = "pass" ]; then ICON="✅" MSG="${ICON} CI PASSED on ${BRANCH} (${COMMIT}) Tests: ${SUMMARY} Features: ${FEATURES} Duration: ${DURATION} Pushed by: ${ACTOR} ${SUITE_INFO} @TSHARPSbm_bot" elif [ "$STATUS" = "fail" ]; then ICON="❌" if [ "$BRANCH" = "main" ]; then ICON="🚨" MSG="${ICON} PRODUCTION CI FAILED on main (${COMMIT}) ⚠️ PRODUCTION DEPLOY SHOULD BE BLOCKED — tests did not pass. If deploy-production.yml ran, it should have reverted automatically. Verify tsharps.com is still working! Tests: ${SUMMARY} Features: ${FEATURES} Duration: ${DURATION} Pushed by: ${ACTOR} ${SUITE_INFO} ${TAG_USERS} — PRODUCTION needs immediate attention. @TSHARPSbm_bot" else MSG="${ICON} CI FAILED on ${BRANCH} (${COMMIT}) Tests: ${SUMMARY} Features: ${FEATURES} Duration: ${DURATION} Pushed by: ${ACTOR} ${SUITE_INFO} ${TAG_USERS} — this build needs attention. @TSHARPSbm_bot" fi elif [ "$STATUS" = "error" ]; then ICON="🚨" MSG="${ICON} CI RUNNER ERROR on ${BRANCH} (${COMMIT}) The CI runner itself failed — not a test failure. Check logs: journalctl -u tsharps-ci --since '5 minutes ago' ${TAG_USERS} — runner needs attention. @TSHARPSbm_bot" fi curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" \ -d "chat_id=${CHAT_ID}" \ -d "message_thread_id=${TOPIC_ID}" \ -d "text=${MSG}" > /dev/null 2>&1 # Send HTML failure report as attachment if available if [ -n "$REPORT_FILE" ] && [ -f "$REPORT_FILE" ]; then curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendDocument" \ -F "chat_id=${CHAT_ID}" \ -F "message_thread_id=${TOPIC_ID}" \ -F "document=@${REPORT_FILE}" \ -F "caption=CI Failure Report — ${BRANCH} (${COMMIT})" > /dev/null 2>&1 rm -f "$REPORT_FILE" fi exit 0