birthdaybot/notification/service_telegram.go

81 lines
2.1 KiB
Go
Raw Normal View History

package notification
import (
2023-07-10 19:46:12 +01:00
"bytes"
"fmt"
2023-07-10 20:25:54 +01:00
"os"
"strconv"
2023-07-10 19:46:12 +01:00
"text/template"
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
"git.ctrlz.es/mgdelacroix/birthdaybot/utils"
"github.com/charmbracelet/log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type TelegramNotificationService struct {
logger *log.Logger
config *model.Config
bot *tgbotapi.BotAPI
}
func NewTelegramNotificationService(logger *log.Logger, config *model.Config) (*TelegramNotificationService, error) {
bot, err := tgbotapi.NewBotAPI(config.TelegramNotifications.BotToken)
if err != nil {
return nil, fmt.Errorf("cannot create bot: %w", err)
}
botUser, err := bot.GetMe()
if err != nil {
return nil, fmt.Errorf("cannot get bot information: %w", err)
}
logger.Info("telegram bot initialized", "id", botUser.ID, "username", botUser.UserName)
return &TelegramNotificationService{
logger: logger,
config: config,
bot: bot,
}, nil
}
2023-07-10 19:46:12 +01:00
func (tns *TelegramNotificationService) Notify(birthday *model.Birthday, template *template.Template) error {
var msgText string
if template != nil {
var stringBuffer bytes.Buffer
2023-07-11 09:21:53 +01:00
if err := template.Execute(&stringBuffer, birthday); err != nil {
2023-07-10 19:46:12 +01:00
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)
}
2023-07-10 20:25:54 +01:00
chatID, err := strconv.Atoi(tns.config.TelegramNotifications.ChannelID)
if err != nil {
return fmt.Errorf("cannot parse ChannelID: %w", err)
}
picture, err := utils.GetPictureForBirthday(birthday, tns.config.Birthdays.Pictures)
2023-07-10 20:25:54 +01:00
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("cannot get picture for birthday: %w", err)
}
var msg tgbotapi.Chattable
if picture == "" {
msg = tgbotapi.NewMessage(int64(chatID), msgText)
} else {
photo := tgbotapi.NewPhoto(int64(chatID), tgbotapi.FilePath(picture))
photo.Caption = msgText
msg = photo
}
if _, err := tns.bot.Send(msg); err != nil {
return fmt.Errorf("error sending message: %w", err)
}
return nil
}