59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
|
package senders
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
_ "embed"
|
||
|
"fmt"
|
||
|
"html/template"
|
||
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
||
|
)
|
||
|
|
||
|
//go:embed template/client_mail.tmpl
|
||
|
var toClientMailTemplate string
|
||
|
|
||
|
//go:embed template/client_tg.tmpl
|
||
|
var toClientTgTemplate string
|
||
|
|
||
|
//go:embed template/client_whatsapp.tmpl
|
||
|
var toClientWhatsAppTemplate string
|
||
|
|
||
|
type LeadSender interface {
|
||
|
SendLead(leadData LeadData) error
|
||
|
Name() string
|
||
|
}
|
||
|
|
||
|
type LeadData struct {
|
||
|
To interface{}
|
||
|
Subject string
|
||
|
Template string
|
||
|
TemplateData TemplateData
|
||
|
}
|
||
|
|
||
|
type TemplateData struct {
|
||
|
QuizConfig model.ResultInfo
|
||
|
AnswerContent model.ResultContent
|
||
|
AllAnswers []model.ResultAnswer
|
||
|
QuestionsMap map[uint64]string
|
||
|
AnswerTime string
|
||
|
}
|
||
|
|
||
|
func generateTextFromTemplate(data TemplateData, tpl string) (string, error) {
|
||
|
t, err := template.New("email").Parse(tpl)
|
||
|
if err != nil {
|
||
|
return "", fmt.Errorf("error parsing template: %w", err)
|
||
|
}
|
||
|
|
||
|
var text bytes.Buffer
|
||
|
if err := t.Execute(&text, TemplateData{
|
||
|
QuizConfig: data.QuizConfig,
|
||
|
AnswerContent: data.AnswerContent,
|
||
|
AllAnswers: data.AllAnswers,
|
||
|
QuestionsMap: data.QuestionsMap,
|
||
|
AnswerTime: data.AnswerTime,
|
||
|
}); err != nil {
|
||
|
return "", fmt.Errorf("error executing template: %w", err)
|
||
|
}
|
||
|
|
||
|
return text.String(), nil
|
||
|
}
|