Adds telegram notification service and notifications services scaffold

This commit is contained in:
Miguel de la Cruz 2023-07-01 18:00:48 +02:00
parent 12c74a0f74
commit 8825837e14
3 changed files with 60 additions and 9 deletions

4
notification/service.go Normal file
View file

@ -0,0 +1,4 @@
package notification
type NotificationService interface {
}

View file

@ -0,0 +1,18 @@
package notification
import (
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
"github.com/charmbracelet/log"
)
type TelegramNotificationService struct {
logger *log.Logger
config *model.TelegramNotificationsConfig
}
func NewTelegramNotificationService(logger *log.Logger, config *model.TelegramNotificationsConfig) *TelegramNotificationService {
return &TelegramNotificationService{
logger: logger,
config: config,
}
}

View file

@ -1,25 +1,45 @@
package server package server
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"git.ctrlz.es/mgdelacroix/birthdaybot/model" "git.ctrlz.es/mgdelacroix/birthdaybot/model"
"git.ctrlz.es/mgdelacroix/birthdaybot/notification"
"git.ctrlz.es/mgdelacroix/birthdaybot/parser" "git.ctrlz.es/mgdelacroix/birthdaybot/parser"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
) )
var ErrNoNotificationServices = errors.New("there are no notification services configured")
type Server struct { type Server struct {
Logger *log.Logger Logger *log.Logger
config *model.Config config *model.Config
workers []*Worker workers []*Worker
birthdays []*model.Birthday birthdays []*model.Birthday
notificationServices []notification.NotificationService
}
func createNotificationServices(logger *log.Logger, config *model.Config) ([]notification.NotificationService, error) {
notificationServices := []notification.NotificationService{}
if config.TelegramNotifications != nil {
telegramNotificationService := notification.NewTelegramNotificationService(logger, config.TelegramNotifications)
notificationServices = append(notificationServices, telegramNotificationService)
}
if len(notificationServices) == 0 {
return nil, ErrNoNotificationServices
}
return notificationServices, nil
} }
func New(config *model.Config) (*Server, error) { func New(config *model.Config) (*Server, error) {
logger := log.New(os.Stderr) logger := log.New(os.Stderr)
logger.Info("Parsing CSV file", "birthdayFile", config.BirthdayFile) logger.Debug("parsing CSV file", "birthdayFile", config.BirthdayFile)
birthdays, err := parser.ParseCSV(config.BirthdayFile) birthdays, err := parser.ParseCSV(config.BirthdayFile)
if err != nil { if err != nil {
@ -27,6 +47,14 @@ func New(config *model.Config) (*Server, error) {
return nil, fmt.Errorf("cannot parse CSV file %s: %w", config.BirthdayFile, err) return nil, fmt.Errorf("cannot parse CSV file %s: %w", config.BirthdayFile, err)
} }
logger.Debug("creating notification services from config")
notificationServices, err := createNotificationServices(logger, config)
if err != nil {
logger.Error("error creating notification services", "error", err)
return nil, err
}
logger.Info("creating server") logger.Info("creating server")
server := &Server{ server := &Server{
@ -34,6 +62,7 @@ func New(config *model.Config) (*Server, error) {
config: config, config: config,
birthdays: birthdays, birthdays: birthdays,
workers: []*Worker{NewWorker(logger)}, workers: []*Worker{NewWorker(logger)},
notificationServices: notificationServices,
} }
return server, nil return server, nil