Adds templates to birthday message

This commit is contained in:
Miguel de la Cruz 2023-07-10 20:46:12 +02:00
parent 7a88cf62ac
commit c7399eb9da
9 changed files with 64 additions and 11 deletions

View file

@ -3,6 +3,7 @@ package server
import (
"errors"
"fmt"
"text/template"
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
"git.ctrlz.es/mgdelacroix/birthdaybot/notification"
@ -22,6 +23,7 @@ type Server struct {
workers []Worker
birthdays []*model.Birthday
notificationServices []notification.NotificationService
tmpl *template.Template
}
func createNotificationServices(logger *log.Logger, config *model.Config) ([]notification.NotificationService, error) {
@ -84,6 +86,16 @@ func New(options ...Option) (*Server, error) {
srv.workers = []Worker{NewSimpleWorker(srv)}
}
if srv.config.BirthdayTemplate != "" {
srv.Logger.Debug("parsing birthday template", "file", srv.config.BirthdayTemplate)
var err error
srv.tmpl, err = template.ParseFiles(srv.config.BirthdayTemplate)
if err != nil {
return nil, fmt.Errorf("cannot parse template file %q: %w", srv.config.BirthdayTemplate, err)
}
}
return srv, nil
}
@ -106,7 +118,7 @@ func (s *Server) Stop() {
func (s *Server) Notify(birthday *model.Birthday) error {
errs := []error{}
for _, service := range s.notificationServices {
err := service.Notify(birthday)
err := service.Notify(birthday, s.tmpl)
if err != nil {
errs = append(errs, err)
}