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

@ -6,6 +6,7 @@ package mocks
import (
reflect "reflect"
template "text/template"
model "git.ctrlz.es/mgdelacroix/birthdaybot/model"
gomock "github.com/golang/mock/gomock"
@ -35,15 +36,15 @@ func (m *MockNotificationService) EXPECT() *MockNotificationServiceMockRecorder
}
// Notify mocks base method.
func (m *MockNotificationService) Notify(arg0 *model.Birthday) error {
func (m *MockNotificationService) Notify(arg0 *model.Birthday, arg1 *template.Template) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Notify", arg0)
ret := m.ctrl.Call(m, "Notify", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
}
// Notify indicates an expected call of Notify.
func (mr *MockNotificationServiceMockRecorder) Notify(arg0 interface{}) *gomock.Call {
func (mr *MockNotificationServiceMockRecorder) Notify(arg0, arg1 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Notify", reflect.TypeOf((*MockNotificationService)(nil).Notify), arg0)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Notify", reflect.TypeOf((*MockNotificationService)(nil).Notify), arg0, arg1)
}

View file

@ -2,9 +2,11 @@
package notification
import (
"text/template"
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
)
type NotificationService interface {
Notify(*model.Birthday) error
Notify(*model.Birthday, *template.Template) error
}

View file

@ -1,8 +1,10 @@
package notification
import (
"bytes"
"fmt"
"strconv"
"text/template"
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
"github.com/charmbracelet/log"
@ -35,9 +37,19 @@ func NewTelegramNotificationService(logger *log.Logger, config *model.TelegramNo
}, nil
}
func (tns *TelegramNotificationService) Notify(birthday *model.Birthday) error {
// ToDo: introduce templates here
msgText := fmt.Sprintf("It's %s's birthday! You can reach them out at %s or %s", birthday.Name, birthday.Email, birthday.Phone)
func (tns *TelegramNotificationService) Notify(birthday *model.Birthday, template *template.Template) error {
var msgText string
if template != nil {
var stringBuffer bytes.Buffer
if err := template.Execute(&stringBuffer, birthday.ToMap()); err != nil {
return fmt.Errorf("cannot execute template for birthday: %w", err)
}
msgText = stringBuffer.String()
} else {
msgText = fmt.Sprintf("It's %s's birthday! You can reach them out at %s or %s", birthday.Name, birthday.Email, birthday.Phone)
}
chatID, err := strconv.Atoi(tns.config.ChannelID)
if err != nil {
return fmt.Errorf("cannot parse ChannelID: %w", err)