Skip to content

Commit 0e97b47

Browse files
committed
fix: webhook message parsing error in custom text format.
Signed-off-by: 孙林耀 <[email protected]>
1 parent d56dfeb commit 0e97b47

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

config/notifiers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ type WebhookConfig struct {
401401
MaxAlerts uint64 `yaml:"max_alerts" json:"max_alerts"`
402402

403403
// Webhook content format
404-
Json string `yaml:"json,omitempty" json:"json,omitempty"`
404+
JSON string `yaml:"json,omitempty" json:"json,omitempty"`
405405
Text string `yaml:"text,omitempty" json:"text,omitempty"`
406406
}
407407

notify/webhook/webhook.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"bytes"
1818
"context"
1919
"encoding/json"
20+
"fmt"
2021
"io"
2122
"net/http"
2223

@@ -96,24 +97,21 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er
9697
}
9798

9899
var buf bytes.Buffer
99-
if n.conf.Json != "" {
100-
body, err := n.tmpl.ExecuteTextString(n.conf.Json, data)
100+
if n.conf.JSON != "" {
101+
body, err := n.tmpl.ExecuteTextString(n.conf.JSON, data)
101102
if err != nil {
102103
return false, err
103104
}
104105
if !json.Valid([]byte(body)) {
105-
return false, fmt.Errorf("Json格式异常: %s ", body)
106+
return false, fmt.Errorf("json format error: %s ", body)
106107
}
107108
if err := json.Indent(&buf, []byte(body), "", " "); err != nil {
108-
return false, fmt.Errorf("Json格式异常: %s ", body)
109+
return false, fmt.Errorf("json format error: %s ", body)
109110
}
110111
} else if n.conf.Text != "" {
111-
body, err := n.tmpl.ExecuteTextString(n.conf.Json, data)
112-
if err != nil {
112+
if body, err := n.tmpl.ExecuteTextString(n.conf.Text, data); err != nil {
113113
return false, err
114-
}
115-
err = json.NewEncoder(&buf).Encode(body)
116-
if err != nil {
114+
} else if _, err = buf.WriteString(body); err != nil {
117115
return false, err
118116
}
119117
} else {

0 commit comments

Comments
 (0)