fix: handle Discord webhook grouped alerts properly

- 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.
This commit is contained in:
Pulse Monitor
2025-08-17 09:30:19 +00:00
parent c06fc309a3
commit 97c3e49cb0
+12 -2
View File
@@ -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"))
}
}
}