Messaging: Add ntfy (ntfy.sh) support (#5364)

This commit is contained in:
Martin Reindl 2022-12-04 13:23:03 +01:00 • committed by GitHub
parent e2200917ea
commit f603c7ac8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 0 deletions

View file

@ -213,3 +213,7 @@ messaging:
# - # list of chat ids
# - type: email
# uri: smtp://<user>:<password>@<host>:<port>/?fromAddress=<from>&toAddresses=<to>
# - type: ntfy
# uri: https://<host>/<topics>
# priority: <priority>
# tags: <tags>

View file

@ -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)
}

53
push/ntfy.go Normal file
View file

@ -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)
}
}