verification/internal/client/telegram.go
2024-11-21 10:29:18 +03:00

87 lines
1.9 KiB
Go

package client
import (
"bytes"
_ "embed"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"go.uber.org/zap"
"gitea.pena/PenaSide/verification/internal/models"
"text/template"
)
//go:embed assets/new_verification.txt
var NewVerification string
//go:embed assets/updated_verification.txt
var UpdatedVerification string
type Deps struct {
Logger *zap.Logger
Bot *tgbotapi.BotAPI
ChatID int64
StagingURL string
}
type Telegram struct {
logger *zap.Logger
bot *tgbotapi.BotAPI
chatID int64
stagingURL string
}
func NewTelegram(deps Deps) *Telegram {
return &Telegram{logger: deps.Logger, bot: deps.Bot, chatID: deps.ChatID, stagingURL: deps.StagingURL}
}
func (t *Telegram) SendVerification(data *models.Verification, url string, isUpdate bool) error {
fmt.Println("VERT", data, url,isUpdate)
var tplPath string
if isUpdate {
tplPath = UpdatedVerification
} else {
tplPath = NewVerification
}
fmt.Println("VERT1", tplPath)
var userURL string
userURL = fmt.Sprintf("%s/users/%s", t.stagingURL, data.UserID)
fmt.Println("VERT2", userURL)
tpl, err := template.New("verification_template").Parse(tplPath)
fmt.Println("VERT333", tpl,err)
if err != nil {
return fmt.Errorf("error parsing template: %w", err)
}
var text bytes.Buffer
toTG := models.ToTelegram{
ID: data.ID,
UserID: data.UserID,
UserURL: userURL,
Accepted: data.Accepted,
Status: data.Status,
UpdatedAt: data.UpdatedAt,
Comment: data.Comment,
Files: data.Files,
TaxNumber: data.TaxNumber,
}
err = tpl.Execute(&text, toTG)
fmt.Println("VERT433", err)
if err != nil {
return fmt.Errorf("error executing template: %w", err)
}
msg := tgbotapi.NewMessage(t.chatID, text.String())
fmt.Println("VERT433", err, t.chatID, text.String())
_, err = t.bot.Send(msg)
if err != nil {
return fmt.Errorf("error sending message: %w", err)
}
return nil
}