diff --git a/evcc.dist.yaml b/evcc.dist.yaml index 194a2785c..e12023d05 100644 --- a/evcc.dist.yaml +++ b/evcc.dist.yaml @@ -213,3 +213,7 @@ messaging: # - # list of chat ids # - type: email # uri: smtp://:@:/?fromAddress=&toAddresses= + # - type: ntfy + # uri: https:/// + # priority: + # tags: diff --git a/push/config.go b/push/config.go index f416d6893..5660dc2d2 100644 --- a/push/config.go +++ b/push/config.go @@ -37,6 +37,11 @@ func NewMessengerFromConfig(typ string, other map[string]interface{}) (res Sende if err = util.DecodeOther(other, &cc); err == nil { res, err = NewScriptMessenger(cc.CmdLine, cc.Timeout, cc.Scale, cc.Cache) } + case "ntfy": + var cc ntfyConfig + if err = util.DecodeOther(other, &cc); err == nil { + res, err = NewNtfyMessenger(cc.URI, cc.Priority, cc.Tags) + } default: err = fmt.Errorf("unknown messenger type: %s", typ) } diff --git a/push/ntfy.go b/push/ntfy.go new file mode 100644 index 000000000..bc656434e --- /dev/null +++ b/push/ntfy.go @@ -0,0 +1,53 @@ +package push + +import ( + "errors" + "net/http" + "strings" + + "github.com/evcc-io/evcc/util/request" +) + +// Ntfy implements the ntfy messaging aggregator +type Ntfy struct { + uri string + priority string + tags string +} + +type ntfyConfig struct { + URI string + Priority string + Tags string +} + +// NewNtfyMessenger creates new Ntfy messenger +func NewNtfyMessenger(uri string, priority string, tags string) (*Ntfy, error) { + if uri == "" { + return nil, errors.New("ntfy: missing uri") + } + + m := &Ntfy{ + uri: uri, + priority: priority, + tags: tags, + } + + return m, nil +} + +// Send sends to all receivers +func (m *Ntfy) Send(title, msg string) { + req, err := request.New("POST", m.uri, strings.NewReader(msg), map[string]string{ + "Priority": m.priority, + "Title": title, + "Tags": m.tags, + }) + if err != nil { + log.ERROR.Printf("ntfy: %v", err) + } + + if _, err := http.DefaultClient.Do(req); err != nil { + log.ERROR.Printf("ntfy: %v", err) + } +}