Adds pictures to telegram messages
This commit is contained in:
parent
ec2cdfdeaa
commit
e2aacc527b
4 changed files with 48 additions and 4 deletions
14
README.md
14
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
|
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.
|
[birthdays example CSV file](./birthdays.csv) to load the data.
|
||||||
|
|
||||||
## Template file
|
### Template file
|
||||||
|
|
||||||
The template can be configured using the `birthday_template` config
|
The template can be configured using the `birthday_template` config
|
||||||
parameter to point to the template file. The template uses the Go
|
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.
|
- `.MonthOfBirth`: the month that the person was born, as number.
|
||||||
- `.DayOfBirth`: the day 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
|
## Run the bot
|
||||||
|
|
||||||
To get help for the bot command, run:
|
To get help for the bot command, run:
|
||||||
|
|
|
@ -15,6 +15,10 @@ type Birthday struct {
|
||||||
DayOfBirth int
|
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 {
|
func (b *Birthday) ToMap() map[string]any {
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
"Name": b.Name,
|
"Name": b.Name,
|
||||||
|
|
|
@ -61,7 +61,7 @@ func (c *Config) SetDefaults() {
|
||||||
type BirthdaysConfig struct {
|
type BirthdaysConfig struct {
|
||||||
File string `yaml:"file"`
|
File string `yaml:"file"`
|
||||||
Template string `yaml:"template"`
|
Template string `yaml:"template"`
|
||||||
Picures string `yaml:"pictures"`
|
Pictures string `yaml:"pictures"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bc *BirthdaysConfig) IsValid() error {
|
func (bc *BirthdaysConfig) IsValid() error {
|
||||||
|
|
|
@ -3,6 +3,8 @@ package notification
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
@ -37,6 +39,18 @@ func NewTelegramNotificationService(logger *log.Logger, config *model.Config) (*
|
||||||
}, nil
|
}, 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 {
|
func (tns *TelegramNotificationService) Notify(birthday *model.Birthday, template *template.Template) error {
|
||||||
var msgText string
|
var msgText string
|
||||||
if template != nil {
|
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)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot parse ChannelID: %w", err)
|
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 {
|
if _, err := tns.bot.Send(msg); err != nil {
|
||||||
return fmt.Errorf("error sending message: %w", err)
|
return fmt.Errorf("error sending message: %w", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue