verification/internal/client/telegram.go
Danil Solovyov 77b57819db Version 0.2
Changes:
  - rename project to remote gitlab
  - changes envs in internal\config\config.go
  - added deployments\staging\docker-compose.yaml
  - added Dockerfile
  - added staging.env
2023-07-03 16:40:20 +05:00

51 lines
1.0 KiB
Go

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"
"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
}