feat(channels): include event verb in ntfy title + smtp subject
CI / Build (windows/amd64) (pull_request) Successful in 30s
CI / Build (linux/amd64) (pull_request) Successful in 23s
CI / Build (linux/arm64) (pull_request) Successful in 22s
CI / Lint (pull_request) Successful in 1m19s
CI / Test (linux/amd64) (pull_request) Successful in 1m42s
CI / Build (windows/amd64) (pull_request) Successful in 30s
CI / Build (linux/amd64) (pull_request) Successful in 23s
CI / Build (linux/arm64) (pull_request) Successful in 22s
CI / Lint (pull_request) Successful in 1m19s
CI / Test (linux/amd64) (pull_request) Successful in 1m42s
Raise / ack / resolve all rendered with the same title and body on ntfy and SMTP, so a recovery looked identical to the original alert. Webhook was already fine because the JSON envelope carries 'event'. ntfy: Title '[raised · warning] dev backup_failed' (was '[warning] …') Tags 'raised,warning,backup_failed' (was 'warning,backup_failed') Body 'Resolved · <message>' / 'Acknowledged · <message>' on those events SMTP: Subject '[restic-manager] [raised · warning] dev: backup_failed' Plus: cmd/_fake_alert now accepts the ref as a positional argument (go run ./cmd/_fake_alert steve-001) instead of silently ignoring unknown positional args. Refuses ambiguous '-ref X positional Y'.
This commit is contained in:
@@ -58,14 +58,34 @@ func (c *NtfyChannel) Send(ctx context.Context, p Payload) (int, time.Duration,
|
||||
server := strings.TrimRight(c.cfg.ServerURL, "/")
|
||||
url := server + "/" + c.cfg.Topic
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBufferString(p.Message))
|
||||
// Body carries the event verb so the body alone is unambiguous when
|
||||
// it shows up on a phone lockscreen without the title.
|
||||
body := p.Message
|
||||
switch p.Event {
|
||||
case EventResolved:
|
||||
body = "Resolved · " + p.Message
|
||||
case EventAcknowledged:
|
||||
body = "Acknowledged · " + p.Message
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBufferString(body))
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("ntfy: build request: %w", err)
|
||||
}
|
||||
|
||||
// Title prefix tracks the event so raise vs ack vs resolve are
|
||||
// visually distinct in the ntfy notification list.
|
||||
verb := "raised"
|
||||
switch p.Event {
|
||||
case EventAcknowledged:
|
||||
verb = "ack"
|
||||
case EventResolved:
|
||||
verb = "resolved"
|
||||
case EventTest:
|
||||
verb = "test"
|
||||
}
|
||||
req.Header.Set("Content-Type", "text/plain")
|
||||
req.Header.Set("Title", fmt.Sprintf("[%s] %s %s", p.Severity, p.HostName, p.Kind))
|
||||
req.Header.Set("Tags", p.Severity+","+p.Kind)
|
||||
req.Header.Set("Title", fmt.Sprintf("[%s · %s] %s %s", verb, p.Severity, p.HostName, p.Kind))
|
||||
req.Header.Set("Tags", verb+","+p.Severity+","+p.Kind)
|
||||
req.Header.Set("Priority", priorityForSeverity(p.Severity, c.defaultPriority))
|
||||
if p.Link != "" {
|
||||
req.Header.Set("Click", p.Link)
|
||||
|
||||
Reference in New Issue
Block a user