verification/internal/client/telegram.go

87 lines
1.9 KiB
Go
Raw Normal View History

2023-06-12 14:19:10 +00:00
package client
import (
"bytes"
2024-02-11 18:22:37 +00:00
_ "embed"
"fmt"
2023-06-12 14:19:10 +00:00
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"go.uber.org/zap"
2024-11-21 07:29:18 +00:00
"gitea.pena/PenaSide/verification/internal/models"
2023-06-12 14:19:10 +00:00
"text/template"
)
2024-02-11 18:22:37 +00:00
//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
2023-06-12 14:19:10 +00:00
}
2024-02-11 18:22:37 +00:00
type Telegram struct {
logger *zap.Logger
bot *tgbotapi.BotAPI
chatID int64
stagingURL string
2023-06-12 14:19:10 +00:00
}
2024-02-11 18:22:37 +00:00
func NewTelegram(deps Deps) *Telegram {
return &Telegram{logger: deps.Logger, bot: deps.Bot, chatID: deps.ChatID, stagingURL: deps.StagingURL}
}
2023-06-12 14:19:10 +00:00
2024-02-11 18:22:37 +00:00
func (t *Telegram) SendVerification(data *models.Verification, url string, isUpdate bool) error {
2024-02-16 12:43:26 +00:00
fmt.Println("VERT", data, url,isUpdate)
2024-02-11 18:22:37 +00:00
var tplPath string
2023-06-12 14:19:10 +00:00
if isUpdate {
2024-02-11 18:22:37 +00:00
tplPath = UpdatedVerification
} else {
tplPath = NewVerification
2023-06-12 14:19:10 +00:00
}
2024-02-16 12:43:26 +00:00
fmt.Println("VERT1", tplPath)
2024-02-11 18:22:37 +00:00
var userURL string
userURL = fmt.Sprintf("%s/users/%s", t.stagingURL, data.UserID)
2024-02-16 12:43:26 +00:00
fmt.Println("VERT2", userURL)
2024-02-11 18:22:37 +00:00
tpl, err := template.New("verification_template").Parse(tplPath)
2024-02-16 12:43:26 +00:00
fmt.Println("VERT333", tpl,err)
2023-06-12 14:19:10 +00:00
if err != nil {
2024-02-11 18:22:37 +00:00
return fmt.Errorf("error parsing template: %w", err)
2023-06-12 14:19:10 +00:00
}
var text bytes.Buffer
2024-02-11 18:22:37 +00:00
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)
2024-02-16 12:43:26 +00:00
fmt.Println("VERT433", err)
2023-06-12 14:19:10 +00:00
if err != nil {
2024-02-11 18:22:37 +00:00
return fmt.Errorf("error executing template: %w", err)
2023-06-12 14:19:10 +00:00
}
msg := tgbotapi.NewMessage(t.chatID, text.String())
fmt.Println("VERT433", err, t.chatID, text.String())
2023-06-12 14:19:10 +00:00
_, err = t.bot.Send(msg)
if err != nil {
2024-02-11 18:22:37 +00:00
return fmt.Errorf("error sending message: %w", err)
2023-06-12 14:19:10 +00:00
}
return nil
}