From b428b04fef2a26c2a07e74f49fa0030f1294ada1 Mon Sep 17 00:00:00 2001 From: Tien Ngo Date: Tue, 30 Dec 2025 11:46:50 +0700 Subject: [PATCH] opt: moderm message --- notify.sh | 306 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 265 insertions(+), 41 deletions(-) diff --git a/notify.sh b/notify.sh index 118f070..35c8d60 100644 --- a/notify.sh +++ b/notify.sh @@ -7,33 +7,65 @@ if [ "$STATUS" = "failure" ]; then ICON="❌" fi -# Prepare Text for Telegram (Markdown) -TEXT="$ICON *$TITLE*%0A%0A$MESSAGE" +echo "" +echo -e "${GREEN}╔════════════════════════════════════════╗${NC}" +echo -e "${GREEN}║ 🚀 Deployment Notification System ║${NC}" +echo -e "${GREEN}╚════════════════════════════════════════╝${NC}" +echo "" -echo "Starting notifications..." - -# Function: Send to Ntfy +# Function: Send to Ntfy (Modern Push Notification) send_ntfy() { if [ -n "$NTFY_URL" ] && [ -n "$NTFY_TOPIC" ]; then + echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" + echo -e "${YELLOW}🔔 Ntfy Push Notification${NC}" + echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" + echo -e " Server: ${GREEN}$NTFY_URL${NC}" + echo -e " Topic: ${GREEN}$NTFY_TOPIC${NC}" + echo "" + local click_url="$GIT_URL/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" local auth_args="" + local priority=4 + local tags="rocket,deployment" + + # Adjust priority and tags based on status + if [ "$STATUS" = "failure" ]; then + priority=5 + tags="x,warning,deployment" + fi # Check for Basic Auth if [ -n "$NTFY_USERNAME" ] && [ -n "$NTFY_PASSWORD" ]; then auth_args="-u $NTFY_USERNAME:$NTFY_PASSWORD" fi - echo "[Ntfy] Sending..." - if curl -s $auth_args -X POST "$NTFY_URL/$NTFY_TOPIC" \ + # Build rich message with metadata + local rich_message=$(cat <&1) + + http_code=$(echo "$response" | tail -n1) + + if [ "$http_code" = "200" ] || [ "$http_code" = "201" ]; then + echo -e " ${GREEN}✓${NC} Notification sent successfully" else - echo "[Ntfy] Failed." + echo -e " ${RED}✗${NC} Failed to send notification (HTTP $http_code)" fi + echo "" fi } @@ -42,53 +74,245 @@ GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' -# Function: Send to Gmail +# Function: Send to Gmail (Modern HTML Email) send_gmail() { if [ -n "$GMAIL_USER" ] && [ -n "$GMAIL_TO" ] && [ -n "$GMAIL_PASS" ]; then - echo -e "${YELLOW}======================================${NC}" - echo -e "${YELLOW}Gmail SMTP Email${NC}" - echo -e "${YELLOW}======================================${NC}" - echo "" - echo -e "From: ${GREEN}$GMAIL_USER${NC}" - echo -e "To: ${GREEN}$GMAIL_TO${NC}" - echo -e "SMTP: ${GREEN}smtp.gmail.com:587${NC}" + echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" + echo -e "${YELLOW}📧 Gmail SMTP Email${NC}" + echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" + echo -e " From: ${GREEN}$GMAIL_USER${NC}" + echo -e " To: ${GREEN}$GMAIL_TO${NC}" + echo -e " SMTP: ${GREEN}smtp.gmail.com:587${NC}" echo "" - SUBJECT="$ICON $TITLE" - BODY="$MESSAGE" + # Determine status color and badge + local status_color="#22c55e" + local status_bg="#dcfce7" + local status_text="SUCCESS" + + if [ "$STATUS" = "failure" ]; then + status_color="#ef4444" + status_bg="#fee2e2" + status_text="FAILURE" + fi - email_content="To: $GMAIL_TO\nSubject: $SUBJECT\n\n$MESSAGE" + # Build modern HTML email with inline CSS + local html_body=$(cat < + + + + + $TITLE + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

$ICON Deployment Notification

+
+
+ $status_text +
+
+

$TITLE

+
+
+

$MESSAGE

+
+
+ + + + + + + + + + +
+ Repository: ${GITHUB_REPOSITORY:-N/A} +
+ Run ID: ${GITHUB_RUN_ID:-N/A} +
+ Timestamp: $(date -u +"%Y-%m-%d %H:%M:%S UTC") +
+
+ + View Deployment Logs + +
+

+ Automated deployment notification from ${GITHUB_REPOSITORY:-Your Repository} +

+
+
+ + +EOF +) + + # Create MIME email with proper headers + local boundary="----=_Part_$(date +%s)_$(( RANDOM ))" + local email_content=$(cat < +Reply-To: $GMAIL_USER +To: $GMAIL_TO +Subject: $ICON $TITLE +MIME-Version: 1.0 +Content-Type: multipart/alternative; boundary="$boundary" + +--$boundary +Content-Type: text/plain; charset=UTF-8 + +$ICON $TITLE + +Status: $STATUS +Message: $MESSAGE + +Repository: ${GITHUB_REPOSITORY:-N/A} +Run ID: ${GITHUB_RUN_ID:-N/A} +Timestamp: $(date -u +"%Y-%m-%d %H:%M:%S UTC") + +View logs: $GIT_URL/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID} + +--$boundary +Content-Type: text/html; charset=UTF-8 + +$html_body +--$boundary-- +EOF +) + + # Send email via SMTP + echo -e " ${YELLOW}→${NC} Sending HTML email..." response=$(echo "$email_content" | curl --ssl-reqd \ - --url "smtp://smtp.gmail.com:587" \ - --user "$GMAIL_USER:$GMAIL_PASS" \ - --mail-from "$GMAIL_USER" \ - --mail-rcpt "$GMAIL_TO" \ - --upload-file - \ - --silent \ - --show-error 2>&1) + --url "smtp://smtp.gmail.com:587" \ + --user "$GMAIL_USER:$GMAIL_PASS" \ + --mail-from "$GMAIL_USER" \ + --mail-rcpt "$GMAIL_TO" \ + --upload-file - \ + --silent \ + --show-error 2>&1) exit_code=$? if [ $exit_code -eq 0 ]; then - echo "[Gmail] Sent." + echo -e " ${GREEN}✓${NC} Email sent successfully" else - echo "[Gmail] Failed." + echo -e " ${RED}✗${NC} Failed to send email" + echo -e " ${RED}Error:${NC} $response" fi + echo "" fi } -# Function: Send to Telegram +# Function: Send to Telegram (Modern Rich Message) send_telegram() { if [ -n "$TELEGRAM_BOT_TOKEN" ] && [ -n "$TELEGRAM_CHAT_ID" ]; then - echo "[Telegram] Sending..." - if curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \ - -d chat_id="$TELEGRAM_CHAT_ID" \ - -d parse_mode="Markdown" \ - -d text="$TEXT"; then - echo "[Telegram] Sent." - else - echo "[Telegram] Failed." + echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" + echo -e "${YELLOW}✈️ Telegram Message${NC}" + echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" + echo -e " Chat ID: ${GREEN}$TELEGRAM_CHAT_ID${NC}" + echo "" + + # Determine status emoji and formatting + local status_emoji="✅" + local status_text="SUCCESS" + if [ "$STATUS" = "failure" ]; then + status_emoji="❌" + status_text="FAILURE" fi + + # Build rich Telegram message with MarkdownV2 formatting + local telegram_message=$(cat <&1) + + # Check if message was sent successfully + if echo "$response" | grep -q '"ok":true'; then + echo -e " ${GREEN}✓${NC} Message sent successfully" + else + echo -e " ${RED}✗${NC} Failed to send message" + # Try to extract error description + error_desc=$(echo "$response" | grep -o '"description":"[^"]*"' | cut -d'"' -f4) + if [ -n "$error_desc" ]; then + echo -e " ${RED}Error:${NC} $error_desc" + fi + fi + echo "" fi }