2023-07-01 17:00:48 +01:00
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
2023-07-10 19:46:12 +01:00
|
|
|
"bytes"
|
2023-07-04 11:29:13 +01:00
|
|
|
"fmt"
|
2023-07-10 20:25:54 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-07-04 11:29:13 +01:00
|
|
|
"strconv"
|
2023-07-10 19:46:12 +01:00
|
|
|
"text/template"
|
2023-07-04 11:29:13 +01:00
|
|
|
|
2023-07-01 17:00:48 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
|
|
|
"github.com/charmbracelet/log"
|
2023-07-04 11:29:13 +01:00
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
2023-07-01 17:00:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type TelegramNotificationService struct {
|
|
|
|
logger *log.Logger
|
2023-07-10 20:08:05 +01:00
|
|
|
config *model.Config
|
2023-07-04 11:29:13 +01:00
|
|
|
bot *tgbotapi.BotAPI
|
2023-07-01 17:00:48 +01:00
|
|
|
}
|
|
|
|
|
2023-07-10 20:08:05 +01:00
|
|
|
func NewTelegramNotificationService(logger *log.Logger, config *model.Config) (*TelegramNotificationService, error) {
|
|
|
|
bot, err := tgbotapi.NewBotAPI(config.TelegramNotifications.BotToken)
|
2023-07-04 11:29:13 +01:00
|
|
|
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)
|
|
|
|
|
2023-07-01 17:00:48 +01:00
|
|
|
return &TelegramNotificationService{
|
|
|
|
logger: logger,
|
|
|
|
config: config,
|
2023-07-04 11:29:13 +01:00
|
|
|
bot: bot,
|
|
|
|
}, nil
|
2023-07-01 17:00:48 +01:00
|
|
|
}
|
2023-07-01 17:03:15 +01:00
|
|
|
|
2023-07-10 20:25:54 +01:00
|
|
|
func (tns *TelegramNotificationService) getPictureFileForBirthday(birthday *model.Birthday) (string, error) {
|
|
|
|
filename := birthday.Filename()
|
|
|
|
path := filepath.Join(tns.config.Birthdays.Pictures, filename)
|
|
|
|
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, 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
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-10 20:25:54 +01:00
|
|
|
chatID, err := strconv.Atoi(tns.config.TelegramNotifications.ChannelID)
|
2023-07-04 11:29:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot parse ChannelID: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-10 20:25:54 +01:00
|
|
|
picture, err := tns.getPictureFileForBirthday(birthday)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-04 11:29:13 +01:00
|
|
|
if _, err := tns.bot.Send(msg); err != nil {
|
|
|
|
return fmt.Errorf("error sending message: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-01 17:03:15 +01:00
|
|
|
return nil
|
|
|
|
}
|