Adds pictures to telegram messages
This commit is contained in:
parent
ec2cdfdeaa
commit
e2aacc527b
4 changed files with 48 additions and 4 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue