Skip to content

Commit 30e3782

Browse files
committed
Fix markdown formatter for telegram notification
- Always convert context field value to string before pipeline
1 parent 1613a90 commit 30e3782

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

alert/formatter.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ type TelegramMarkdownFormatter struct {
192192

193193
func NewTelegramMarkdownFormatter(tags, atUsers []string) (f *TelegramMarkdownFormatter, err error) {
194194
funcMap := template.FuncMap{
195+
"toString": toString,
195196
"escapeMarkdown": escapeMarkdown,
196197
"formatRFC3339": formatRFC3339,
197198
"truncateStringWithTail": truncateStringWithTail,
@@ -207,6 +208,13 @@ func NewTelegramMarkdownFormatter(tags, atUsers []string) (f *TelegramMarkdownFo
207208
return &TelegramMarkdownFormatter{markdownFormatter: mf}, nil
208209
}
209210

211+
func toString(val interface{}) string {
212+
if str, ok := val.(string); ok {
213+
return str
214+
}
215+
return fmt.Sprintf("%v", val)
216+
}
217+
210218
func escapeMarkdown(v interface{}) string {
211219
return bot.EscapeMarkdown(fmt.Sprintf("%v", v))
212220
}

alert/templates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Reason
9191
{{.Error | escapeMarkdown}}
9292
9393
{{else}}{{ end }}{{ if .CtxFields }}*Context Fields*:{{ range $Key, $Val := .CtxFields }}
94-
*{{$Key | escapeMarkdown}}*: {{$Val | truncateStringWithTail | escapeMarkdown}}{{ end }}{{ end }}
94+
*{{$Key | escapeMarkdown}}*: {{$Val | toString | truncateStringWithTail | escapeMarkdown}}{{ end }}{{ end }}
9595
{{ range mentions }}@{{ . }} {{ end }}
9696
`,
9797
}

0 commit comments

Comments
 (0)