Skip to content

Commit

Permalink
use pango for message formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
equinox0815 committed Oct 24, 2023
1 parent da86920 commit 0aa5777
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
20 changes: 18 additions & 2 deletions notifier/backend_smsmodem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions notifier/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0aa5777

Please sign in to comment.