added apprise notifier
This commit is contained in:
parent
1202d3ccd9
commit
9918a1e683
3 changed files with 105 additions and 2 deletions
11
main.go
11
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
|
||||
|
|
94
notifiers/apprise.go
Normal file
94
notifiers/apprise.go
Normal 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
|
||||
}
|
2
todo.md
2
todo.md
|
@ -1,6 +1,6 @@
|
|||
# Todo
|
||||
- make generic 'notifier' interface
|
||||
- apprise service
|
||||
- ~~apprise service~~
|
||||
- apprise compose example
|
||||
- comments
|
||||
- run/fix staticcheck
|
||||
|
|
Loading…
Add table
Reference in a new issue