refactor of notifiers, it's now a list in the config
This commit is contained in:
parent
d4e59b176a
commit
bcf42571e5
7 changed files with 103 additions and 52 deletions
58
main.go
58
main.go
|
@ -162,28 +162,48 @@ func (web *Web) initCronJobs() {
|
|||
|
||||
func (web *Web) initNotifiers() {
|
||||
web.notifiers = make(map[string]notifiers.Notifier, 5)
|
||||
if viper.IsSet("notifiers.telegram") {
|
||||
telegramBot := notifiers.TelegramNotifier{}
|
||||
if telegramBot.Open() {
|
||||
web.notifiers["Telegram"] = &telegramBot
|
||||
}
|
||||
if !viper.IsSet("notifiers") {
|
||||
log.Panicln("No notifiers set!")
|
||||
return
|
||||
}
|
||||
if viper.IsSet("notifiers.discord") {
|
||||
discordBot := notifiers.DiscordNotifier{}
|
||||
if discordBot.Open() {
|
||||
web.notifiers["Discord"] = &discordBot
|
||||
notifiersMap := viper.GetStringMap("notifiers")
|
||||
for name := range notifiersMap {
|
||||
notifierPath := fmt.Sprintf("notifiers.%s", name)
|
||||
notifierMap := viper.GetStringMapString(notifierPath)
|
||||
|
||||
notifierType, exists := notifierMap["type"]
|
||||
if !exists {
|
||||
log.Printf("No 'type' for '%s' notifier!", name)
|
||||
continue
|
||||
}
|
||||
}
|
||||
if viper.IsSet("notifiers.email") {
|
||||
emailBot := notifiers.EmailNotifier{}
|
||||
if emailBot.Open() {
|
||||
web.notifiers["Email"] = &emailBot
|
||||
success := false
|
||||
var notifier notifiers.Notifier
|
||||
switch notifierType {
|
||||
case "telegram":
|
||||
{
|
||||
notifier = ¬ifiers.TelegramNotifier{}
|
||||
success = notifier.Open(notifierPath)
|
||||
break
|
||||
}
|
||||
case "discord":
|
||||
{
|
||||
notifier = ¬ifiers.DiscordNotifier{}
|
||||
success = notifier.Open(notifierPath)
|
||||
break
|
||||
}
|
||||
case "email":
|
||||
{
|
||||
notifier = ¬ifiers.EmailNotifier{}
|
||||
success = notifier.Open(notifierPath)
|
||||
}
|
||||
case "shoutrrr":
|
||||
{
|
||||
notifier = ¬ifiers.ShoutrrrNotifier{}
|
||||
success = notifier.Open(notifierPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
if viper.IsSet("notifiers.shoutrrr") {
|
||||
shoutrrrBot := notifiers.ShoutrrrNotifier{}
|
||||
if shoutrrrBot.Open() {
|
||||
web.notifiers["Shoutrrr"] = &shoutrrrBot
|
||||
if success {
|
||||
web.notifiers[name] = notifier
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package notifiers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
@ -18,8 +19,10 @@ type DiscordNotifier struct {
|
|||
Debug bool
|
||||
}
|
||||
|
||||
func (discord *DiscordNotifier) Open() bool {
|
||||
if !viper.IsSet("notifiers.discord.userID") && !viper.IsSet("notifiers.discord.server") {
|
||||
func (discord *DiscordNotifier) Open(configPath string) bool {
|
||||
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")
|
||||
return false
|
||||
}
|
||||
|
@ -28,8 +31,8 @@ func (discord *DiscordNotifier) Open() bool {
|
|||
log.Println("Could not start Discord notifier:\n", err)
|
||||
return false
|
||||
}
|
||||
if viper.IsSet("notifiers.discord.userID") {
|
||||
discord.UserID = viper.GetString("notifiers.discord.userID")
|
||||
if viper.IsSet(userIDPath) {
|
||||
discord.UserID = viper.GetString(userIDPath)
|
||||
channel, err := bot.UserChannelCreate(discord.UserID)
|
||||
if err != nil {
|
||||
log.Println("Could not connect to user channel:", discord.UserID, err)
|
||||
|
@ -38,9 +41,11 @@ func (discord *DiscordNotifier) Open() bool {
|
|||
discord.UserChannel = channel
|
||||
log.Println("Authorized discord bot for:", channel.Recipients)
|
||||
}
|
||||
if viper.IsSet("notifiers.discord.server") {
|
||||
discord.ServerID = viper.GetString("notifiers.discord.server.ID")
|
||||
discord.ChannelID = viper.GetString("notifiers.discord.server.channel")
|
||||
if viper.IsSet(serverPath) {
|
||||
serverIDPath := fmt.Sprintf("%s.server.ID", configPath)
|
||||
serverChannelPath := fmt.Sprintf("%s.server.channel", configPath)
|
||||
discord.ServerID = viper.GetString(serverIDPath)
|
||||
discord.ChannelID = viper.GetString(serverChannelPath)
|
||||
channels, err := bot.GuildChannels(discord.ServerID)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
discord.Debug = viper.GetBool("notifiers.discord.debug")
|
||||
debugPath := fmt.Sprintf("%s.debug", configPath)
|
||||
discord.Debug = viper.GetBool(debugPath)
|
||||
if discord.Debug {
|
||||
bot.LogLevel = discordgo.LogDebug
|
||||
} else {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package notifiers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
@ -17,39 +18,45 @@ type EmailNotifier struct {
|
|||
Debug bool
|
||||
}
|
||||
|
||||
func (email *EmailNotifier) Open() bool {
|
||||
if viper.IsSet("notifiers.email.server") {
|
||||
email.Server = viper.GetString("notifiers.email.server")
|
||||
func (email *EmailNotifier) Open(configPath string) bool {
|
||||
serverPath := fmt.Sprintf("%s.server", configPath)
|
||||
if viper.IsSet(serverPath) {
|
||||
email.Server = viper.GetString(serverPath)
|
||||
} else {
|
||||
log.Println("Need 'server' var for email notifier")
|
||||
return false
|
||||
}
|
||||
if viper.IsSet("notifiers.email.port") {
|
||||
email.Port = viper.GetInt("notifiers.email.port")
|
||||
portPath := fmt.Sprintf("%s.port", configPath)
|
||||
if viper.IsSet(portPath) {
|
||||
email.Port = viper.GetInt(portPath)
|
||||
} else {
|
||||
log.Println("Need 'port' var for email notifier")
|
||||
return false
|
||||
}
|
||||
if viper.IsSet("notifiers.email.user") {
|
||||
email.User = viper.GetString("notifiers.email.user")
|
||||
userPath := fmt.Sprintf("%s.user", configPath)
|
||||
if viper.IsSet(userPath) {
|
||||
email.User = viper.GetString(userPath)
|
||||
} else {
|
||||
log.Println("Need 'user' var for email notifier")
|
||||
return false
|
||||
}
|
||||
if viper.IsSet("notifiers.email.from") {
|
||||
email.From = viper.GetString("notifiers.email.from")
|
||||
fromPath := fmt.Sprintf("%s.from", configPath)
|
||||
if viper.IsSet(fromPath) {
|
||||
email.From = viper.GetString(fromPath)
|
||||
} else {
|
||||
log.Println("Need 'from' var for email notifier")
|
||||
return false
|
||||
}
|
||||
if viper.IsSet("notifiers.email.to") {
|
||||
email.To = viper.GetString("notifiers.email.to")
|
||||
toPath := fmt.Sprintf("%s.to", configPath)
|
||||
if viper.IsSet(toPath) {
|
||||
email.To = viper.GetString(toPath)
|
||||
} else {
|
||||
log.Println("Need 'to' var for email notifier")
|
||||
return false
|
||||
}
|
||||
if viper.IsSet("notifiers.email.password") {
|
||||
email.Password = viper.GetString("notifiers.email.password")
|
||||
passwordPath := fmt.Sprintf("%s.password", configPath)
|
||||
if viper.IsSet(passwordPath) {
|
||||
email.Password = viper.GetString(passwordPath)
|
||||
} else {
|
||||
log.Println("Need 'password' var for email notifier")
|
||||
return false
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package notifiers
|
||||
|
||||
type Notifier interface {
|
||||
Open() bool
|
||||
Open(configPath string) bool
|
||||
Message(message string) bool
|
||||
Close() bool
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package notifiers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/containrrr/shoutrrr"
|
||||
|
@ -12,13 +13,14 @@ type ShoutrrrNotifier struct {
|
|||
URLs []string
|
||||
}
|
||||
|
||||
func (shoutr *ShoutrrrNotifier) Open() bool {
|
||||
log.Println("Shoutrrr version:", shoutrrr.Version())
|
||||
if !viper.IsSet("notifiers.shoutrrr.urls") {
|
||||
func (shoutr *ShoutrrrNotifier) Open(configPath string) bool {
|
||||
urlsPath := fmt.Sprintf("%s.urls", configPath)
|
||||
if !viper.IsSet(urlsPath) {
|
||||
log.Println("Need 'urls' for Shoutrrr")
|
||||
return false
|
||||
}
|
||||
shoutr.URLs = viper.GetStringSlice("notifiers.shoutrrr.urls")
|
||||
shoutr.URLs = viper.GetStringSlice(urlsPath)
|
||||
log.Println("Shoutrrr version:", shoutrrr.Version())
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package notifiers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
|
@ -8,17 +9,31 @@ import (
|
|||
)
|
||||
|
||||
type TelegramNotifier struct {
|
||||
Bot *tgbotapi.BotAPI
|
||||
Token string
|
||||
Debug bool
|
||||
Bot *tgbotapi.BotAPI
|
||||
Token string
|
||||
ChatID int64
|
||||
Debug bool
|
||||
}
|
||||
|
||||
func (telegram *TelegramNotifier) Open() bool {
|
||||
bot, err := tgbotapi.NewBotAPI(viper.GetString("notifiers.telegram.token"))
|
||||
func (telegram *TelegramNotifier) Open(configPath string) bool {
|
||||
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 {
|
||||
log.Println("Could not start Telegram notifier:\n", err)
|
||||
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
|
||||
bot.Debug = viper.GetBool("notifiers.telegram.debug")
|
||||
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 {
|
||||
msg := tgbotapi.NewMessage(viper.GetInt64("notifiers.telegram.chat"), message)
|
||||
msg := tgbotapi.NewMessage(telegram.ChatID, message)
|
||||
_, err := telegram.Bot.Send(msg)
|
||||
if err != nil {
|
||||
log.Println("Could not send Telegram message:\n", err)
|
||||
|
|
3
todo.md
3
todo.md
|
@ -4,4 +4,5 @@
|
|||
- comments
|
||||
- run/fix staticcheck
|
||||
- add browserless support ?
|
||||
- refactor notifiers to be a list, with 'type' value?
|
||||
- ~~refactor notifiers to be a list, with 'type' value?~~
|
||||
- readme
|
Loading…
Add table
Reference in a new issue