added apprise notifier

This commit is contained in:
BroodjeAap 2022-12-29 10:44:25 +00:00
parent 1202d3ccd9
commit 9918a1e683
3 changed files with 105 additions and 2 deletions

11
main.go
View file

@ -163,7 +163,7 @@ func (web *Web) initCronJobs() {
func (web *Web) initNotifiers() { func (web *Web) initNotifiers() {
web.notifiers = make(map[string]notifiers.Notifier, 5) web.notifiers = make(map[string]notifiers.Notifier, 5)
if !viper.IsSet("notifiers") { if !viper.IsSet("notifiers") {
log.Panicln("No notifiers set!") log.Println("No notifiers set!")
return return
} }
notifiersMap := viper.GetStringMap("notifiers") notifiersMap := viper.GetStringMap("notifiers")
@ -201,6 +201,15 @@ func (web *Web) initNotifiers() {
notifier = &notifiers.ShoutrrrNotifier{} notifier = &notifiers.ShoutrrrNotifier{}
success = notifier.Open(notifierPath) success = notifier.Open(notifierPath)
} }
case "apprise":
{
notifier = &notifiers.AppriseNotifier{}
success = notifier.Open(notifierPath)
}
default:
{
log.Println("Did not recognize notifier type:", notifierType)
}
} }
if success { if success {
web.notifiers[name] = notifier web.notifiers[name] = notifier

94
notifiers/apprise.go Normal file
View file

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

View file

@ -1,6 +1,6 @@
# Todo # Todo
- make generic 'notifier' interface - make generic 'notifier' interface
- apprise service - ~~apprise service~~
- apprise compose example - apprise compose example
- comments - comments
- run/fix staticcheck - run/fix staticcheck