Adds initial implementation of the Telegram bot
This commit is contained in:
parent
38e78cc02b
commit
9bc9e4f60c
7 changed files with 71 additions and 7 deletions
|
@ -25,7 +25,10 @@ func createNotificationServices(logger *log.Logger, config *model.Config) ([]not
|
|||
notificationServices := []notification.NotificationService{}
|
||||
|
||||
if config.TelegramNotifications != nil {
|
||||
telegramNotificationService := notification.NewTelegramNotificationService(logger, config.TelegramNotifications)
|
||||
telegramNotificationService, err := notification.NewTelegramNotificationService(logger, config.TelegramNotifications)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create telegram notification service: %w", err)
|
||||
}
|
||||
notificationServices = append(notificationServices, telegramNotificationService)
|
||||
}
|
||||
|
||||
|
@ -61,9 +64,9 @@ func New(config *model.Config) (*Server, error) {
|
|||
Logger: logger,
|
||||
config: config,
|
||||
birthdays: birthdays,
|
||||
workers: []*Worker{NewWorker(logger)},
|
||||
notificationServices: notificationServices,
|
||||
}
|
||||
server.workers = []*Worker{NewWorker(server)}
|
||||
|
||||
return server, nil
|
||||
}
|
||||
|
@ -83,3 +86,22 @@ func (s *Server) Stop() {
|
|||
}
|
||||
s.Logger.Info("server stopped", "workers", len(s.workers))
|
||||
}
|
||||
|
||||
func (s *Server) Notify(birthday *model.Birthday) error {
|
||||
errs := []error{}
|
||||
for _, service := range s.notificationServices {
|
||||
err := service.Notify(birthday)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(errs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (s *Server) Birthdays() []*model.Birthday {
|
||||
return s.birthdays
|
||||
}
|
||||
|
|
|
@ -7,14 +7,16 @@ import (
|
|||
)
|
||||
|
||||
type Worker struct {
|
||||
server *Server
|
||||
logger *log.Logger
|
||||
stop chan bool
|
||||
stopped chan bool
|
||||
}
|
||||
|
||||
func NewWorker(logger *log.Logger) *Worker {
|
||||
func NewWorker(server *Server) *Worker {
|
||||
return &Worker{
|
||||
logger: logger,
|
||||
server: server,
|
||||
logger: server.Logger,
|
||||
stop: make(chan bool, 1),
|
||||
stopped: make(chan bool, 1),
|
||||
}
|
||||
|
@ -39,6 +41,9 @@ func (w *Worker) run() {
|
|||
select {
|
||||
case t := <-ticker.C:
|
||||
w.logger.Info("ticker ticked for worker", "tick", t)
|
||||
// ToDo: improve logic here
|
||||
b := w.server.Birthdays()[0]
|
||||
w.server.Notify(b)
|
||||
case <-w.stop:
|
||||
w.logger.Info("received stop signal")
|
||||
w.stopped <- true
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue