From 97c3e49cb0c7642ddac48e7659cae1f5eead0ad1 Mon Sep 17 00:00:00 2001 From: Pulse Monitor Date: Sun, 17 Aug 2025 09:30:19 +0000 Subject: [PATCH] fix: handle Discord webhook grouped alerts properly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Discord embeds don't support newlines in description field - Use comma-separated list format for Discord grouped alerts - Keep escaped newlines for other webhook providers (Telegram, Slack, Teams) - Prevents JSON parsing errors with Discord webhook API Discord now shows: "Alert | šŸ”” 5 alerts: • item1: 25.4%, • item2: 11.6%, ..." Other providers show multi-line format with proper escaping. --- internal/notifications/notifications.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/notifications/notifications.go b/internal/notifications/notifications.go index ee7c7486c..f5fa966ae 100644 --- a/internal/notifications/notifications.go +++ b/internal/notifications/notifications.go @@ -442,6 +442,8 @@ func (n *NotificationManager) sendGroupedWebhook(webhook WebhookConfig, alertLis otherAlerts = append(otherAlerts, fmt.Sprintf("• %s: %.1f%%", alertList[i].ResourceName, alertList[i].Value)) } if len(otherAlerts) > 0 { + // For custom templates, we need to escape newlines since they're likely + // used in shell commands or other contexts that need escaping alert.Message = fmt.Sprintf("%s\\n\\nšŸ”” All %d alerts:\\n%s", summary, len(alertList), strings.Join(otherAlerts, "\\n")) } } @@ -493,7 +495,7 @@ func (n *NotificationManager) sendGroupedWebhook(webhook WebhookConfig, alertLis } if templateFound { - // Modify message if multiple alerts - show full list with escaped newlines + // Modify message if multiple alerts - but format differently for Discord if len(alertList) > 1 { summary := alert.Message otherAlerts := []string{} @@ -501,7 +503,15 @@ func (n *NotificationManager) sendGroupedWebhook(webhook WebhookConfig, alertLis otherAlerts = append(otherAlerts, fmt.Sprintf("• %s: %.1f%%", alertList[i].ResourceName, alertList[i].Value)) } if len(otherAlerts) > 0 { - alert.Message = fmt.Sprintf("%s\\n\\nšŸ”” All %d alerts:\\n%s", summary, len(alertList), strings.Join(otherAlerts, "\\n")) + // For Discord, format as a single line list to avoid newline issues + // Discord embeds don't render \n in description anyway + if webhook.Service == "discord" { + // Use comma-separated list for Discord + alert.Message = fmt.Sprintf("%s | šŸ”” %d alerts: %s", summary, len(alertList), strings.Join(otherAlerts, ", ")) + } else { + // For other services, escape newlines properly + alert.Message = fmt.Sprintf("%s\\n\\nšŸ”” All %d alerts:\\n%s", summary, len(alertList), strings.Join(otherAlerts, "\\n")) + } } }