verification/internal/client/telegram.go

51 lines
1.0 KiB
Go
Raw Normal View History

2023-06-12 14:19:10 +00:00
package client
import (
"bytes"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/backend/verification/internal/models"
2023-06-12 14:19:10 +00:00
"text/template"
)
type Telegram struct {
logger *zap.Logger
bot *tgbotapi.BotAPI
chatID int64
}
func NewTelegram(logger *zap.Logger, bot *tgbotapi.BotAPI, chatID int64) *Telegram {
return &Telegram{logger: logger, bot: bot, chatID: chatID}
}
func (t *Telegram) SendVerification(data *models.Verification, isUpdate bool) error {
tplPath := "assets/new_verification.txt"
if isUpdate {
tplPath = "assets/updated_verification.txt"
}
tpl, err := template.ParseFiles(tplPath)
if err != nil {
return err
}
var text bytes.Buffer
err = tpl.Execute(&text, data)
if err != nil {
t.logger.Error("ClientTelegram", zap.Error(err))
return err
}
msg := tgbotapi.NewMessage(t.chatID, text.String())
_, err = t.bot.Send(msg)
if err != nil {
t.logger.Error("ClientTelegram", zap.Error(err))
return err
}
return nil
}