Adds initial implementation of the Telegram bot
This commit is contained in:
parent
38e78cc02b
commit
9bc9e4f60c
7 changed files with 71 additions and 7 deletions
|
@ -1,22 +1,52 @@
|
|||
package notification
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
||||
"github.com/charmbracelet/log"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
type TelegramNotificationService struct {
|
||||
logger *log.Logger
|
||||
config *model.TelegramNotificationsConfig
|
||||
bot *tgbotapi.BotAPI
|
||||
}
|
||||
|
||||
func NewTelegramNotificationService(logger *log.Logger, config *model.TelegramNotificationsConfig) *TelegramNotificationService {
|
||||
func NewTelegramNotificationService(logger *log.Logger, config *model.TelegramNotificationsConfig) (*TelegramNotificationService, error) {
|
||||
bot, err := tgbotapi.NewBotAPI(config.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
|
||||
}
|
||||
|
||||
func (tns *TelegramNotificationService) Notify(birthday *model.Birthday) error {
|
||||
// ToDo: introduce templates here
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot parse ChannelID: %w", err)
|
||||
}
|
||||
|
||||
msg := tgbotapi.NewMessage(int64(chatID), msgText)
|
||||
if _, err := tns.bot.Send(msg); err != nil {
|
||||
return fmt.Errorf("error sending message: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue