feedback/internal/client/telegram.go
2023-04-20 07:03:21 +05:00

58 lines
1.2 KiB
Go

package client
import (
"bytes"
"github.com/go-telegram-bot-api/telegram-bot-api/v5"
"go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/backend/templategen_feedback/internal/models"
"text/template"
)
type Telegram struct {
logger *zap.Logger
bot *tgbotapi.BotAPI
chatID int64
templatePath string
}
func NewTelegram(logger *zap.Logger, bot *tgbotapi.BotAPI, chatID int64, templatePath string) *Telegram {
return &Telegram{logger: logger, bot: bot, chatID: chatID, templatePath: templatePath}
}
func (t *Telegram) SendFeedback(data *models.Feedback) error {
tpl, err := template.ParseFiles(t.templatePath)
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
}
func (t *Telegram) SendMessage(data string) error {
msg := tgbotapi.NewMessage(t.chatID, data)
_, err := t.bot.Send(msg)
if err != nil {
t.logger.Error("ClientTelegram", zap.Error(err))
return err
}
return nil
}