Add support for Gotify notifications (#346)
This adds support for Gotify (https://gotify.net) notifications. Work items: * Two flags have been added to internal/flags/flags.go: "notification-gotify-url" and "notification-gotify-token". * A Gotify notification driver has been added in notifications/gotify.go. * "gotify" has been added to notification driver choices in notifications/notifier.go. * Docs have been updatedpull/353/head
parent
d744b5ddf7
commit
dff16dc639
@ -0,0 +1,100 @@
|
|||||||
|
package notifications
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
gotifyType = "gotify"
|
||||||
|
)
|
||||||
|
|
||||||
|
type gotifyTypeNotifier struct {
|
||||||
|
gotifyURL string
|
||||||
|
gotifyAppToken string
|
||||||
|
logLevels []log.Level
|
||||||
|
}
|
||||||
|
|
||||||
|
func newGotifyNotifier(c *cobra.Command, acceptedLogLevels []log.Level) typeNotifier {
|
||||||
|
flags := c.PersistentFlags()
|
||||||
|
|
||||||
|
gotifyURL, _ := flags.GetString("notification-gotify-url")
|
||||||
|
if len(gotifyURL) < 1 {
|
||||||
|
log.Fatal("Required argument --notification-gotify-url(cli) or WATCHTOWER_NOTIFICATION_GOTIFY_URL(env) is empty.")
|
||||||
|
} else if !(strings.HasPrefix(gotifyURL, "http://") || strings.HasPrefix(gotifyURL, "https://")) {
|
||||||
|
log.Fatal("Gotify URL must start with \"http://\" or \"https://\"")
|
||||||
|
} else if strings.HasPrefix(gotifyURL, "http://") {
|
||||||
|
log.Warn("Using an HTTP url fpr Gotify is insecure")
|
||||||
|
}
|
||||||
|
|
||||||
|
gotifyToken, _ := flags.GetString("notification-gotify-token")
|
||||||
|
if len(gotifyToken) < 1 {
|
||||||
|
log.Fatal("Required argument --notification-gotify-token(cli) or WATCHTOWER_NOTIFICATION_GOTIFY_TOKEN(env) is empty.")
|
||||||
|
}
|
||||||
|
|
||||||
|
n := &gotifyTypeNotifier{
|
||||||
|
gotifyURL: gotifyURL,
|
||||||
|
gotifyAppToken: gotifyToken,
|
||||||
|
logLevels: acceptedLogLevels,
|
||||||
|
}
|
||||||
|
|
||||||
|
log.AddHook(n)
|
||||||
|
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *gotifyTypeNotifier) StartNotification() {}
|
||||||
|
|
||||||
|
func (n *gotifyTypeNotifier) SendNotification() {}
|
||||||
|
|
||||||
|
func (n *gotifyTypeNotifier) Levels() []log.Level {
|
||||||
|
return n.logLevels
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *gotifyTypeNotifier) getURL() string {
|
||||||
|
url := n.gotifyURL
|
||||||
|
if !strings.HasSuffix(url, "/") {
|
||||||
|
url += "/"
|
||||||
|
}
|
||||||
|
return url + "message?token=" + n.gotifyAppToken
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *gotifyTypeNotifier) Fire(entry *log.Entry) error {
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
jsonBody, err := json.Marshal(gotifyMessage{
|
||||||
|
Message: "(" + entry.Level.String() + "): " + entry.Message,
|
||||||
|
Title: "Watchtower",
|
||||||
|
Priority: 0,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to create JSON body for Gotify notification: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonBodyBuffer := bytes.NewBuffer([]byte(jsonBody))
|
||||||
|
resp, err := http.Post(n.getURL(), "application/json", jsonBodyBuffer)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to send Gotify notification: ", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
fmt.Printf("Gotify notification returned %d HTTP status code", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type gotifyMessage struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Priority int `json:"priority"`
|
||||||
|
}
|
Loading…
Reference in New Issue