From e2aacc527b2e11493c5f6e40488c79ecf1a1ffa6 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Mon, 10 Jul 2023 21:25:54 +0200 Subject: [PATCH] Adds pictures to telegram messages --- README.md | 14 +++++++++++++- model/birthdays.go | 4 ++++ model/config.go | 2 +- notification/service_telegram.go | 32 ++++++++++++++++++++++++++++++-- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 137adbc..1f45f75 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ There is an [example configuration file](./example-config.yml) that can be used as a base to create your own configuration, and there is a [birthdays example CSV file](./birthdays.csv) to load the data. -## Template file +### Template file The template can be configured using the `birthday_template` config parameter to point to the template file. The template uses the Go @@ -24,6 +24,18 @@ Template Format, and has the following properties available: - `.MonthOfBirth`: the month that the person was born, as number. - `.DayOfBirth`: the day that the person was born, as number. +### Pictures + +Alongside the notification for each birthday, the bot can send a +picture for the person with the notification message. For this to +happen, you need to put the pictures in a folder, and point the +configuration to this folder using the property `birthdays.pictures`. + +Each file should be named accordingly of the birthday it corresponds +to. The naming schema is `YEAR_MONTH_DAY_PHONE.png`, so for a person +born the 1st of July of 2020, with the phone 4567, the file name for +their picture would be `2020_6_1_4567.png` + ## Run the bot To get help for the bot command, run: diff --git a/model/birthdays.go b/model/birthdays.go index 1de971a..cbd64d6 100644 --- a/model/birthdays.go +++ b/model/birthdays.go @@ -15,6 +15,10 @@ type Birthday struct { DayOfBirth int } +func (b *Birthday) Filename() string { + return fmt.Sprintf("%d_%d_%d_%s.png", b.YearOfBirth, b.MonthOfBirth, b.DayOfBirth, b.Phone) +} + func (b *Birthday) ToMap() map[string]any { return map[string]any{ "Name": b.Name, diff --git a/model/config.go b/model/config.go index b7d563c..a0adc7e 100644 --- a/model/config.go +++ b/model/config.go @@ -61,7 +61,7 @@ func (c *Config) SetDefaults() { type BirthdaysConfig struct { File string `yaml:"file"` Template string `yaml:"template"` - Picures string `yaml:"pictures"` + Pictures string `yaml:"pictures"` } func (bc *BirthdaysConfig) IsValid() error { diff --git a/notification/service_telegram.go b/notification/service_telegram.go index 82d363d..79511c8 100644 --- a/notification/service_telegram.go +++ b/notification/service_telegram.go @@ -3,6 +3,8 @@ package notification import ( "bytes" "fmt" + "os" + "path/filepath" "strconv" "text/template" @@ -37,6 +39,18 @@ func NewTelegramNotificationService(logger *log.Logger, config *model.Config) (* }, nil } +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 +} + func (tns *TelegramNotificationService) Notify(birthday *model.Birthday, template *template.Template) error { var msgText string if template != nil { @@ -50,12 +64,26 @@ func (tns *TelegramNotificationService) Notify(birthday *model.Birthday, templat 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) + chatID, err := strconv.Atoi(tns.config.TelegramNotifications.ChannelID) if err != nil { return fmt.Errorf("cannot parse ChannelID: %w", err) } - msg := tgbotapi.NewMessage(int64(chatID), msgText) + 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 + } + if _, err := tns.bot.Send(msg); err != nil { return fmt.Errorf("error sending message: %w", err) }