From 0aa5777ded3e990c05ea63577769f8e9430a6f66 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 24 Oct 2023 15:34:28 +0200 Subject: [PATCH] use pango for message formatting --- go.mod | 1 + go.sum | 2 ++ notifier/backend_smsmodem.go | 20 ++++++++++++++++++-- notifier/types.go | 1 + 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index cd251d8..605e354 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/enescakir/emoji v1.0.0 // indirect + github.com/flosch/pongo2/v6 v6.0.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-gonic/gin v1.9.1 // indirect diff --git a/go.sum b/go.sum index 40583a3..861ae16 100644 --- a/go.sum +++ b/go.sum @@ -86,6 +86,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/flosch/pongo2/v6 v6.0.0 h1:lsGru8IAzHgIAw6H2m4PCyleO58I40ow6apih0WprMU= +github.com/flosch/pongo2/v6 v6.0.0/go.mod h1:CuDpFm47R0uGGE7z13/tTlt1Y6zdxvr2RLT5LJhsHEU= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= diff --git a/notifier/backend_smsmodem.go b/notifier/backend_smsmodem.go index 11db34e..174d840 100644 --- a/notifier/backend_smsmodem.go +++ b/notifier/backend_smsmodem.go @@ -38,12 +38,18 @@ import ( "sync" "time" + "github.com/flosch/pongo2/v6" "github.com/warthog618/modem/at" "github.com/warthog618/modem/gsm" "github.com/warthog618/modem/serial" "github.com/whawty/alerts/store" ) +const ( + // TODO: improve alert formatting + defaultTemplate = "{{ alert.State.Emoji() }} {{ alert.State }} | {{ alert.Severity.Emoji() }} {{ alert.Severity }} | {{ alert.Name }}" +) + type SMSModemBackend struct { infoLog *log.Logger dbgLog *log.Logger @@ -128,8 +134,18 @@ func (smb *SMSModemBackend) Notify(ctx context.Context, target NotifierTarget, a return false, nil } - // TODO: improve alert formatting - message := fmt.Sprintf("%v %s | %v %s | %s", alert.State.Emoji(), alert.State, alert.Severity.Emoji(), alert.Severity, alert.Name) + tmplText := smb.conf.Template + if tmplText == "" { + tmplText = defaultTemplate + } + tpl, err := pongo2.FromString(tmplText) + if err != nil { + return false, err + } + message, err := tpl.Execute(pongo2.Context{"alert": alert}) + if err != nil { + return false, err + } resp, err := smb.sms.SendLongMessage(string(*target.SMS), message) if err != nil { diff --git a/notifier/types.go b/notifier/types.go index cbe7c63..3826bfa 100644 --- a/notifier/types.go +++ b/notifier/types.go @@ -48,6 +48,7 @@ type NotifierBackendConfigSMSModem struct { Baudrate int `yaml:"baudrate"` Timeout time.Duration `yaml:"timeout"` Pin *uint `yaml:"pin"` + Template string `yaml:"template"` } type NotifierBackendConfig struct {