refactor of notifiers, it's now a list in the config

This commit is contained in:
BroodjeAap 2022-12-28 12:15:09 +00:00
parent d4e59b176a
commit bcf42571e5
7 changed files with 103 additions and 52 deletions

58
main.go
View file

@ -162,28 +162,48 @@ 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.telegram") { if !viper.IsSet("notifiers") {
telegramBot := notifiers.TelegramNotifier{} log.Panicln("No notifiers set!")
if telegramBot.Open() { return
web.notifiers["Telegram"] = &telegramBot
}
} }
if viper.IsSet("notifiers.discord") { notifiersMap := viper.GetStringMap("notifiers")
discordBot := notifiers.DiscordNotifier{} for name := range notifiersMap {
if discordBot.Open() { notifierPath := fmt.Sprintf("notifiers.%s", name)
web.notifiers["Discord"] = &discordBot notifierMap := viper.GetStringMapString(notifierPath)
notifierType, exists := notifierMap["type"]
if !exists {
log.Printf("No 'type' for '%s' notifier!", name)
continue
} }
} success := false
if viper.IsSet("notifiers.email") { var notifier notifiers.Notifier
emailBot := notifiers.EmailNotifier{} switch notifierType {
if emailBot.Open() { case "telegram":
web.notifiers["Email"] = &emailBot {
notifier = &notifiers.TelegramNotifier{}
success = notifier.Open(notifierPath)
break
}
case "discord":
{
notifier = &notifiers.DiscordNotifier{}
success = notifier.Open(notifierPath)
break
}
case "email":
{
notifier = &notifiers.EmailNotifier{}
success = notifier.Open(notifierPath)
}
case "shoutrrr":
{
notifier = &notifiers.ShoutrrrNotifier{}
success = notifier.Open(notifierPath)
}
} }
} if success {
if viper.IsSet("notifiers.shoutrrr") { web.notifiers[name] = notifier
shoutrrrBot := notifiers.ShoutrrrNotifier{}
if shoutrrrBot.Open() {
web.notifiers["Shoutrrr"] = &shoutrrrBot
} }
} }
} }

View file

@ -1,6 +1,7 @@
package notifiers package notifiers
import ( import (
"fmt"
"log" "log"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -18,8 +19,10 @@ type DiscordNotifier struct {
Debug bool Debug bool
} }
func (discord *DiscordNotifier) Open() bool { func (discord *DiscordNotifier) Open(configPath string) bool {
if !viper.IsSet("notifiers.discord.userID") && !viper.IsSet("notifiers.discord.server") { userIDPath := fmt.Sprintf("%s.userID", configPath)
serverPath := fmt.Sprintf("%s.server", configPath)
if !viper.IsSet(userIDPath) && !viper.IsSet(serverPath) {
log.Println("Net either 'serverID' or 'userID' for Discord") log.Println("Net either 'serverID' or 'userID' for Discord")
return false return false
} }
@ -28,8 +31,8 @@ func (discord *DiscordNotifier) Open() bool {
log.Println("Could not start Discord notifier:\n", err) log.Println("Could not start Discord notifier:\n", err)
return false return false
} }
if viper.IsSet("notifiers.discord.userID") { if viper.IsSet(userIDPath) {
discord.UserID = viper.GetString("notifiers.discord.userID") discord.UserID = viper.GetString(userIDPath)
channel, err := bot.UserChannelCreate(discord.UserID) channel, err := bot.UserChannelCreate(discord.UserID)
if err != nil { if err != nil {
log.Println("Could not connect to user channel:", discord.UserID, err) log.Println("Could not connect to user channel:", discord.UserID, err)
@ -38,9 +41,11 @@ func (discord *DiscordNotifier) Open() bool {
discord.UserChannel = channel discord.UserChannel = channel
log.Println("Authorized discord bot for:", channel.Recipients) log.Println("Authorized discord bot for:", channel.Recipients)
} }
if viper.IsSet("notifiers.discord.server") { if viper.IsSet(serverPath) {
discord.ServerID = viper.GetString("notifiers.discord.server.ID") serverIDPath := fmt.Sprintf("%s.server.ID", configPath)
discord.ChannelID = viper.GetString("notifiers.discord.server.channel") serverChannelPath := fmt.Sprintf("%s.server.channel", configPath)
discord.ServerID = viper.GetString(serverIDPath)
discord.ChannelID = viper.GetString(serverChannelPath)
channels, err := bot.GuildChannels(discord.ServerID) channels, err := bot.GuildChannels(discord.ServerID)
if err != nil { if err != nil {
log.Println("Could not connect to server channel:", discord.ServerID, err) log.Println("Could not connect to server channel:", discord.ServerID, err)
@ -61,7 +66,8 @@ func (discord *DiscordNotifier) Open() bool {
} }
log.Println("Authorized discord bot for:", discord.ServerChannel.Name) log.Println("Authorized discord bot for:", discord.ServerChannel.Name)
} }
discord.Debug = viper.GetBool("notifiers.discord.debug") debugPath := fmt.Sprintf("%s.debug", configPath)
discord.Debug = viper.GetBool(debugPath)
if discord.Debug { if discord.Debug {
bot.LogLevel = discordgo.LogDebug bot.LogLevel = discordgo.LogDebug
} else { } else {

View file

@ -1,6 +1,7 @@
package notifiers package notifiers
import ( import (
"fmt"
"log" "log"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -17,39 +18,45 @@ type EmailNotifier struct {
Debug bool Debug bool
} }
func (email *EmailNotifier) Open() bool { func (email *EmailNotifier) Open(configPath string) bool {
if viper.IsSet("notifiers.email.server") { serverPath := fmt.Sprintf("%s.server", configPath)
email.Server = viper.GetString("notifiers.email.server") if viper.IsSet(serverPath) {
email.Server = viper.GetString(serverPath)
} else { } else {
log.Println("Need 'server' var for email notifier") log.Println("Need 'server' var for email notifier")
return false return false
} }
if viper.IsSet("notifiers.email.port") { portPath := fmt.Sprintf("%s.port", configPath)
email.Port = viper.GetInt("notifiers.email.port") if viper.IsSet(portPath) {
email.Port = viper.GetInt(portPath)
} else { } else {
log.Println("Need 'port' var for email notifier") log.Println("Need 'port' var for email notifier")
return false return false
} }
if viper.IsSet("notifiers.email.user") { userPath := fmt.Sprintf("%s.user", configPath)
email.User = viper.GetString("notifiers.email.user") if viper.IsSet(userPath) {
email.User = viper.GetString(userPath)
} else { } else {
log.Println("Need 'user' var for email notifier") log.Println("Need 'user' var for email notifier")
return false return false
} }
if viper.IsSet("notifiers.email.from") { fromPath := fmt.Sprintf("%s.from", configPath)
email.From = viper.GetString("notifiers.email.from") if viper.IsSet(fromPath) {
email.From = viper.GetString(fromPath)
} else { } else {
log.Println("Need 'from' var for email notifier") log.Println("Need 'from' var for email notifier")
return false return false
} }
if viper.IsSet("notifiers.email.to") { toPath := fmt.Sprintf("%s.to", configPath)
email.To = viper.GetString("notifiers.email.to") if viper.IsSet(toPath) {
email.To = viper.GetString(toPath)
} else { } else {
log.Println("Need 'to' var for email notifier") log.Println("Need 'to' var for email notifier")
return false return false
} }
if viper.IsSet("notifiers.email.password") { passwordPath := fmt.Sprintf("%s.password", configPath)
email.Password = viper.GetString("notifiers.email.password") if viper.IsSet(passwordPath) {
email.Password = viper.GetString(passwordPath)
} else { } else {
log.Println("Need 'password' var for email notifier") log.Println("Need 'password' var for email notifier")
return false return false

View file

@ -1,7 +1,7 @@
package notifiers package notifiers
type Notifier interface { type Notifier interface {
Open() bool Open(configPath string) bool
Message(message string) bool Message(message string) bool
Close() bool Close() bool
} }

View file

@ -1,6 +1,7 @@
package notifiers package notifiers
import ( import (
"fmt"
"log" "log"
"github.com/containrrr/shoutrrr" "github.com/containrrr/shoutrrr"
@ -12,13 +13,14 @@ type ShoutrrrNotifier struct {
URLs []string URLs []string
} }
func (shoutr *ShoutrrrNotifier) Open() bool { func (shoutr *ShoutrrrNotifier) Open(configPath string) bool {
log.Println("Shoutrrr version:", shoutrrr.Version()) urlsPath := fmt.Sprintf("%s.urls", configPath)
if !viper.IsSet("notifiers.shoutrrr.urls") { if !viper.IsSet(urlsPath) {
log.Println("Need 'urls' for Shoutrrr") log.Println("Need 'urls' for Shoutrrr")
return false return false
} }
shoutr.URLs = viper.GetStringSlice("notifiers.shoutrrr.urls") shoutr.URLs = viper.GetStringSlice(urlsPath)
log.Println("Shoutrrr version:", shoutrrr.Version())
return true return true
} }

View file

@ -1,6 +1,7 @@
package notifiers package notifiers
import ( import (
"fmt"
"log" "log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@ -8,17 +9,31 @@ import (
) )
type TelegramNotifier struct { type TelegramNotifier struct {
Bot *tgbotapi.BotAPI Bot *tgbotapi.BotAPI
Token string Token string
Debug bool ChatID int64
Debug bool
} }
func (telegram *TelegramNotifier) Open() bool { func (telegram *TelegramNotifier) Open(configPath string) bool {
bot, err := tgbotapi.NewBotAPI(viper.GetString("notifiers.telegram.token")) tokenPath := fmt.Sprintf("%s.token", configPath)
if !viper.IsSet(tokenPath) {
log.Println("Telegram needs 'token' value set")
return false
}
telegram.Token = viper.GetString(tokenPath)
bot, err := tgbotapi.NewBotAPI(telegram.Token)
if err != nil { if err != nil {
log.Println("Could not start Telegram notifier:\n", err) log.Println("Could not start Telegram notifier:\n", err)
return false return false
} }
chatIDPath := fmt.Sprintf("%s.chat", configPath)
if !viper.IsSet(chatIDPath) {
log.Panicln("Telegram needs 'chat' ID value")
return false
}
telegram.ChatID = viper.GetInt64(chatIDPath)
telegram.Bot = bot telegram.Bot = bot
bot.Debug = viper.GetBool("notifiers.telegram.debug") bot.Debug = viper.GetBool("notifiers.telegram.debug")
log.Printf("Authorized telegram bot: %s", bot.Self.UserName) log.Printf("Authorized telegram bot: %s", bot.Self.UserName)
@ -26,7 +41,7 @@ func (telegram *TelegramNotifier) Open() bool {
} }
func (telegram *TelegramNotifier) Message(message string) bool { func (telegram *TelegramNotifier) Message(message string) bool {
msg := tgbotapi.NewMessage(viper.GetInt64("notifiers.telegram.chat"), message) msg := tgbotapi.NewMessage(telegram.ChatID, message)
_, err := telegram.Bot.Send(msg) _, err := telegram.Bot.Send(msg)
if err != nil { if err != nil {
log.Println("Could not send Telegram message:\n", err) log.Println("Could not send Telegram message:\n", err)

View file

@ -4,4 +4,5 @@
- comments - comments
- run/fix staticcheck - run/fix staticcheck
- add browserless support ? - add browserless support ?
- refactor notifiers to be a list, with 'type' value? - ~~refactor notifiers to be a list, with 'type' value?~~
- readme