From 9918a1e68367cfdb1ae1c8d199edd9c6eb38f4aa Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Thu, 29 Dec 2022 10:44:25 +0000 Subject: [PATCH] added apprise notifier --- main.go | 11 +++++- notifiers/apprise.go | 94 ++++++++++++++++++++++++++++++++++++++++++++ todo.md | 2 +- 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 notifiers/apprise.go diff --git a/main.go b/main.go index cfff320..5d6dcb0 100644 --- a/main.go +++ b/main.go @@ -163,7 +163,7 @@ func (web *Web) initCronJobs() { func (web *Web) initNotifiers() { web.notifiers = make(map[string]notifiers.Notifier, 5) if !viper.IsSet("notifiers") { - log.Panicln("No notifiers set!") + log.Println("No notifiers set!") return } notifiersMap := viper.GetStringMap("notifiers") @@ -201,6 +201,15 @@ func (web *Web) initNotifiers() { notifier = ¬ifiers.ShoutrrrNotifier{} success = notifier.Open(notifierPath) } + case "apprise": + { + notifier = ¬ifiers.AppriseNotifier{} + success = notifier.Open(notifierPath) + } + default: + { + log.Println("Did not recognize notifier type:", notifierType) + } } if success { web.notifiers[name] = notifier diff --git a/notifiers/apprise.go b/notifiers/apprise.go new file mode 100644 index 0000000..69ac739 --- /dev/null +++ b/notifiers/apprise.go @@ -0,0 +1,94 @@ +package notifiers + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + + "github.com/spf13/viper" +) + +type AppriseNotifier struct { + URL string + Title string + Type string + Format string + URLs []string +} + +func (apprise *AppriseNotifier) Open(configPath string) bool { + urlPath := fmt.Sprintf("%s.url", configPath) + if !viper.IsSet(urlPath) { + log.Println("Need 'url' for Apprise") + return false + } + apprise.URL = viper.GetString(urlPath) + + urlsPath := fmt.Sprintf("%s.urls", configPath) + if !viper.IsSet(urlsPath) { + log.Println("Need 'urls' for Apprise") + return false + } + + apprise.Title = "GoWatch Notification" + titlePath := fmt.Sprintf("%s.title", configPath) + if viper.IsSet(titlePath) { + apprise.Title = viper.GetString(titlePath) + } + apprise.Type = "info" + typePath := fmt.Sprintf("%s.mtype", configPath) + if viper.IsSet(typePath) { + apprise.Type = viper.GetString(typePath) + } + apprise.Format = "text" + formatPath := fmt.Sprintf("%s.format", configPath) + if viper.IsSet(formatPath) { + apprise.Format = viper.GetString(formatPath) + } + apprise.URLs = viper.GetStringSlice(urlsPath) + log.Println("Apprise notifier:", apprise.URL, apprise.Type, apprise.Format) + return true +} + +type ApprisePostData struct { + Title string `json:"title"` + Type string `json:"type"` + Format string `json:"format"` + URLs []string `json:"urls"` + Body string `json:"body"` +} + +func (apprise *AppriseNotifier) Message(message string) bool { + data := ApprisePostData{ + URLs: apprise.URLs, + Title: apprise.Title, + Type: apprise.Type, + Format: apprise.Format, + Body: message, + } + jsn, err := json.Marshal(data) + if err != nil { + log.Panicln("Could not create JSON post data:", err) + return false + } + + resp, err := http.Post(apprise.URL, "application/json", bytes.NewBuffer(jsn)) + if err != nil { + log.Println("Could not send Apprise notification:", err) + return false + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println("Could not parse Apprise response:", err) + return false + } + log.Println(string(body)) + return true +} + +func (apprise *AppriseNotifier) Close() bool { + return true +} diff --git a/todo.md b/todo.md index 4c824f2..c3b68f1 100644 --- a/todo.md +++ b/todo.md @@ -1,6 +1,6 @@ # Todo - make generic 'notifier' interface - - apprise service + - ~~apprise service~~ - apprise compose example - comments - run/fix staticcheck