opt: moderm message

This commit is contained in:
2025-12-30 11:46:50 +07:00
parent 7c86049049
commit b428b04fef

306
notify.sh
View File

@@ -7,33 +7,65 @@ if [ "$STATUS" = "failure" ]; then
ICON="❌" ICON="❌"
fi fi
# Prepare Text for Telegram (Markdown) echo ""
TEXT="$ICON *$TITLE*%0A%0A$MESSAGE" echo -e "${GREEN}╔════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ 🚀 Deployment Notification System ║${NC}"
echo -e "${GREEN}╚════════════════════════════════════════╝${NC}"
echo ""
echo "Starting notifications..." # Function: Send to Ntfy (Modern Push Notification)
# Function: Send to Ntfy
send_ntfy() { send_ntfy() {
if [ -n "$NTFY_URL" ] && [ -n "$NTFY_TOPIC" ]; then 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 click_url="$GIT_URL/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
local auth_args="" 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 # Check for Basic Auth
if [ -n "$NTFY_USERNAME" ] && [ -n "$NTFY_PASSWORD" ]; then if [ -n "$NTFY_USERNAME" ] && [ -n "$NTFY_PASSWORD" ]; then
auth_args="-u $NTFY_USERNAME:$NTFY_PASSWORD" auth_args="-u $NTFY_USERNAME:$NTFY_PASSWORD"
fi fi
echo "[Ntfy] Sending..." # Build rich message with metadata
if curl -s $auth_args -X POST "$NTFY_URL/$NTFY_TOPIC" \ local rich_message=$(cat <<EOF
$MESSAGE
📦 Repository: ${GITHUB_REPOSITORY:-N/A}
🔢 Run ID: ${GITHUB_RUN_ID:-N/A}
⏰ Time: $(date -u +"%Y-%m-%d %H:%M:%S UTC")
EOF
)
echo -e " ${YELLOW}${NC} Sending push notification..."
response=$(curl -s -w "\n%{http_code}" $auth_args -X POST "$NTFY_URL/$NTFY_TOPIC" \
-H "Title: $ICON $TITLE" \ -H "Title: $ICON $TITLE" \
-H "Priority: 4" \ -H "Priority: $priority" \
-H "Tags: $tags" \
-H "Click: $click_url" \ -H "Click: $click_url" \
-H "Actions: view, Open Logs, $click_url" \ -H "Actions: view, Open Logs, $click_url, clear=true" \
-d "$MESSAGE"; then -d "$rich_message" 2>&1)
echo "[Ntfy] Sent."
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "200" ] || [ "$http_code" = "201" ]; then
echo -e " ${GREEN}${NC} Notification sent successfully"
else else
echo "[Ntfy] Failed." echo -e " ${RED}${NC} Failed to send notification (HTTP $http_code)"
fi fi
echo ""
fi fi
} }
@@ -42,53 +74,245 @@ GREEN='\033[0;32m'
YELLOW='\033[1;33m' YELLOW='\033[1;33m'
NC='\033[0m' NC='\033[0m'
# Function: Send to Gmail # Function: Send to Gmail (Modern HTML Email)
send_gmail() { send_gmail() {
if [ -n "$GMAIL_USER" ] && [ -n "$GMAIL_TO" ] && [ -n "$GMAIL_PASS" ]; then if [ -n "$GMAIL_USER" ] && [ -n "$GMAIL_TO" ] && [ -n "$GMAIL_PASS" ]; then
echo -e "${YELLOW}======================================${NC}" echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${YELLOW}Gmail SMTP Email${NC}" echo -e "${YELLOW}📧 Gmail SMTP Email${NC}"
echo -e "${YELLOW}======================================${NC}" echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo "" echo -e " From: ${GREEN}$GMAIL_USER${NC}"
echo -e "From: ${GREEN}$GMAIL_USER${NC}" echo -e " To: ${GREEN}$GMAIL_TO${NC}"
echo -e "To: ${GREEN}$GMAIL_TO${NC}" echo -e " SMTP: ${GREEN}smtp.gmail.com:587${NC}"
echo -e "SMTP: ${GREEN}smtp.gmail.com:587${NC}"
echo "" echo ""
SUBJECT="$ICON $TITLE" # Determine status color and badge
BODY="$MESSAGE" 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 <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>$TITLE</title>
</head>
<body style="margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; background-color: #f3f4f6;">
<table width="100%" cellpadding="0" cellspacing="0" style="background-color: #f3f4f6; padding: 40px 20px;">
<tr>
<td align="center">
<table width="600" cellpadding="0" cellspacing="0" style="background-color: #ffffff; border-radius: 12px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); overflow: hidden;">
<!-- Header -->
<tr>
<td style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 32px 40px; text-align: center;">
<h1 style="margin: 0; color: #ffffff; font-size: 28px; font-weight: 700;">$ICON Deployment Notification</h1>
</td>
</tr>
<!-- Status Badge -->
<tr>
<td style="padding: 32px 40px 24px; text-align: center;">
<div style="display: inline-block; background-color: $status_bg; color: $status_color; padding: 8px 20px; border-radius: 20px; font-weight: 600; font-size: 14px; letter-spacing: 0.5px;">
$status_text
</div>
</td>
</tr>
<!-- Title -->
<tr>
<td style="padding: 0 40px 24px; text-align: center;">
<h2 style="margin: 0; color: #111827; font-size: 24px; font-weight: 600;">$TITLE</h2>
</td>
</tr>
<!-- Message -->
<tr>
<td style="padding: 0 40px 32px;">
<div style="background-color: #f9fafb; border-left: 4px solid $status_color; padding: 20px; border-radius: 6px;">
<p style="margin: 0; color: #374151; font-size: 15px; line-height: 1.6; white-space: pre-wrap;">$MESSAGE</p>
</div>
</td>
</tr>
<!-- Metadata -->
<tr>
<td style="padding: 0 40px 32px;">
<table width="100%" cellpadding="8" cellspacing="0" style="border-top: 1px solid #e5e7eb;">
<tr>
<td style="color: #6b7280; font-size: 13px; padding-top: 16px;">
<strong>Repository:</strong> ${GITHUB_REPOSITORY:-N/A}
</td>
</tr>
<tr>
<td style="color: #6b7280; font-size: 13px;">
<strong>Run ID:</strong> ${GITHUB_RUN_ID:-N/A}
</td>
</tr>
<tr>
<td style="color: #6b7280; font-size: 13px;">
<strong>Timestamp:</strong> $(date -u +"%Y-%m-%d %H:%M:%S UTC")
</td>
</tr>
</table>
</td>
</tr>
<!-- Action Button -->
<tr>
<td style="padding: 0 40px 40px; text-align: center;">
<a href="$GIT_URL/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" style="display: inline-block; background-color: #667eea; color: #ffffff; text-decoration: none; padding: 12px 32px; border-radius: 6px; font-weight: 600; font-size: 14px; transition: background-color 0.3s;">
View Deployment Logs
</a>
</td>
</tr>
<!-- Footer -->
<tr>
<td style="background-color: #f9fafb; padding: 24px 40px; text-align: center; border-top: 1px solid #e5e7eb;">
<p style="margin: 0; color: #9ca3af; font-size: 12px;">
Automated deployment notification from <strong>${GITHUB_REPOSITORY:-Your Repository}</strong>
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
EOF
)
# Create MIME email with proper headers
local boundary="----=_Part_$(date +%s)_$(( RANDOM ))"
local email_content=$(cat <<EOF
From: Act Runner Deployment <noreply@actrunner>
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 \ response=$(echo "$email_content" | curl --ssl-reqd \
--url "smtp://smtp.gmail.com:587" \ --url "smtp://smtp.gmail.com:587" \
--user "$GMAIL_USER:$GMAIL_PASS" \ --user "$GMAIL_USER:$GMAIL_PASS" \
--mail-from "$GMAIL_USER" \ --mail-from "$GMAIL_USER" \
--mail-rcpt "$GMAIL_TO" \ --mail-rcpt "$GMAIL_TO" \
--upload-file - \ --upload-file - \
--silent \ --silent \
--show-error 2>&1) --show-error 2>&1)
exit_code=$? exit_code=$?
if [ $exit_code -eq 0 ]; then if [ $exit_code -eq 0 ]; then
echo "[Gmail] Sent." echo -e " ${GREEN}${NC} Email sent successfully"
else else
echo "[Gmail] Failed." echo -e " ${RED}${NC} Failed to send email"
echo -e " ${RED}Error:${NC} $response"
fi fi
echo ""
fi fi
} }
# Function: Send to Telegram # Function: Send to Telegram (Modern Rich Message)
send_telegram() { send_telegram() {
if [ -n "$TELEGRAM_BOT_TOKEN" ] && [ -n "$TELEGRAM_CHAT_ID" ]; then if [ -n "$TELEGRAM_BOT_TOKEN" ] && [ -n "$TELEGRAM_CHAT_ID" ]; then
echo "[Telegram] Sending..." echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
if curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \ echo -e "${YELLOW}✈️ Telegram Message${NC}"
-d chat_id="$TELEGRAM_CHAT_ID" \ echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
-d parse_mode="Markdown" \ echo -e " Chat ID: ${GREEN}$TELEGRAM_CHAT_ID${NC}"
-d text="$TEXT"; then echo ""
echo "[Telegram] Sent."
else # Determine status emoji and formatting
echo "[Telegram] Failed." local status_emoji="✅"
local status_text="SUCCESS"
if [ "$STATUS" = "failure" ]; then
status_emoji="❌"
status_text="FAILURE"
fi fi
# Build rich Telegram message with MarkdownV2 formatting
local telegram_message=$(cat <<EOF
$ICON *$TITLE*
━━━━━━━━━━━━━━━━━━━━━━
$status_emoji *Status:* \`$status_text\`
━━━━━━━━━━━━━━━━━━━━━━
📝 *Message:*
$MESSAGE
📊 *Details:*
• 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]($GIT_URL/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID})
EOF
)
echo -e " ${YELLOW}${NC} Sending Telegram message..."
response=$(curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d "{
\"chat_id\": \"$TELEGRAM_CHAT_ID\",
\"parse_mode\": \"Markdown\",
\"text\": $(echo "$telegram_message" | jq -Rs .),
\"disable_web_page_preview\": false,
\"reply_markup\": {
\"inline_keyboard\": [[
{
\"text\": \"📋 View Logs\",
\"url\": \"$GIT_URL/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}\"
}
]]
}
}" 2>&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 fi
} }