diff --git a/main.go b/main.go index 04ef4ff..ae1c527 100644 --- a/main.go +++ b/main.go @@ -11,11 +11,12 @@ import ( "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/robfig/cron/v3" "github.com/spf13/viper" "gorm.io/driver/sqlite" "gorm.io/gorm" + + "broodjeaap.net/go-watch/notifiers" ) var baseHTML = filepath.Join("templates", "base.html") @@ -29,7 +30,7 @@ type Web struct { urlCache map[string]string cronWatch map[uint]cron.Entry db *gorm.DB - Bot *tgbotapi.BotAPI + notifiers map[string]notifiers.Notifier } func newWeb() *Web { @@ -110,33 +111,21 @@ func (web *Web) initCronJobs() { } func (web *Web) initNotifiers() { - bot, _ := tgbotapi.NewBotAPI(viper.GetString("telegram.token")) - bot.Debug = true - web.Bot = bot - log.Printf("Authorized on account %s", bot.Self.UserName) - go web.passiveBot() -} + web.notifiers = make(map[string]notifiers.Notifier, 5) + if viper.IsSet("telegram") { + telegramBot := notifiers.TelegramNotifier{} + telegramBot.Open() + web.notifiers["Telegram"] = telegramBot -func (web *Web) passiveBot() { - u := tgbotapi.NewUpdate(0) - u.Timeout = 60 - updates := web.Bot.GetUpdatesChan(u) - - for update := range updates { - if update.Message != nil { // If we got a message - log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) - - msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text) - msg.ReplyToMessageID = update.Message.MessageID - - web.Bot.Send(msg) - } } } -func (web *Web) notify(message string) { - msg := tgbotapi.NewMessage(viper.GetInt64("telegram.chat"), message) - web.Bot.Send(msg) +func (web *Web) notify(notifierKey string, message string) { + notifier, exists := web.notifiers[notifierKey] + if !exists { + log.Println("Could not find notifier with key:", notifierKey) + } + notifier.Message(message) } func (web *Web) run() { diff --git a/notifiers/notifier.go b/notifiers/notifier.go new file mode 100644 index 0000000..b68fbc9 --- /dev/null +++ b/notifiers/notifier.go @@ -0,0 +1,7 @@ +package notifiers + +type Notifier interface { + Open() bool + Message(message string) bool + Close() bool +} diff --git a/notifiers/telegram.go b/notifiers/telegram.go new file mode 100644 index 0000000..5117b94 --- /dev/null +++ b/notifiers/telegram.go @@ -0,0 +1,40 @@ +package notifiers + +import ( + "log" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "github.com/spf13/viper" +) + +type TelegramNotifier struct { + Bot *tgbotapi.BotAPI + Token string + Debug bool +} + +func (telegram TelegramNotifier) Open() bool { + bot, err := tgbotapi.NewBotAPI(viper.GetString("telegram.token")) + if err != nil { + log.Println("Could not start Telegram notifier:\n", err) + return false + } + telegram.Bot = bot + bot.Debug = viper.GetBool("telegram.debug") + log.Printf("Authorized telegram bot: %s", bot.Self.UserName) + return true +} + +func (telegram TelegramNotifier) Message(message string) bool { + msg := tgbotapi.NewMessage(viper.GetInt64("telegram.chat"), message) + _, err := telegram.Bot.Send(msg) + if err != nil { + log.Println("Could not send Telegram message:\n", err) + return false + } + return true +} + +func (telegram TelegramNotifier) Close() bool { + return true +} diff --git a/scraping.go b/scraping.go index 62e7186..f0e5b4e 100644 --- a/scraping.go +++ b/scraping.go @@ -779,7 +779,7 @@ func notifyFilter(filters []Filter, filter *Filter, watch *Watch, web *Web, debu if debug { log.Println(buffer.String()) } else { - web.notify(buffer.String()) + web.notify("telegram", buffer.String()) } }