51 lines
991 B
Go
51 lines
991 B
Go
![]() |
package client
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||
|
"go.uber.org/zap"
|
||
|
"text/template"
|
||
|
"verification/internal/models"
|
||
|
)
|
||
|
|
||
|
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
|
||
|
}
|