added webhook sender

This commit is contained in:
pasha1coil 2025-07-07 11:39:44 +03:00
parent e46acea3f1
commit 02a3f4ebb7
6 changed files with 77 additions and 3 deletions

2
go.mod

@ -8,7 +8,7 @@ require (
gitea.pena/PenaSide/common v0.0.0-20250609100303-3b7c00cc97bc gitea.pena/PenaSide/common v0.0.0-20250609100303-3b7c00cc97bc
gitea.pena/PenaSide/customer v0.0.0-20250518194954-882ec684be86 gitea.pena/PenaSide/customer v0.0.0-20250518194954-882ec684be86
gitea.pena/PenaSide/hlog v0.0.0-20241125221102-a54c29c002a9 gitea.pena/PenaSide/hlog v0.0.0-20241125221102-a54c29c002a9
gitea.pena/SQuiz/common v0.0.0-20250610100937-ce7096a3dd37 gitea.pena/SQuiz/common v0.0.0-20250707081926-0ad481f2ad6b
github.com/caarlos0/env/v8 v8.0.0 github.com/caarlos0/env/v8 v8.0.0
github.com/go-redis/redis/v8 v8.11.5 github.com/go-redis/redis/v8 v8.11.5
github.com/go-resty/resty/v2 v2.16.5 github.com/go-resty/resty/v2 v2.16.5

2
go.sum

@ -16,6 +16,8 @@ gitea.pena/SQuiz/common v0.0.0-20250606140534-fb272e7a1763 h1:v0X5uSedghzFgu+ohU
gitea.pena/SQuiz/common v0.0.0-20250606140534-fb272e7a1763/go.mod h1:zCrUwDh0APpztKk6NUqTZv+zhjVbWpGBJiJ5z9dAH0U= gitea.pena/SQuiz/common v0.0.0-20250606140534-fb272e7a1763/go.mod h1:zCrUwDh0APpztKk6NUqTZv+zhjVbWpGBJiJ5z9dAH0U=
gitea.pena/SQuiz/common v0.0.0-20250610100937-ce7096a3dd37 h1:JD5YDqRrwvZ8AmxEZnek0O5vCTXFtfjw+7RGx/yKA8c= gitea.pena/SQuiz/common v0.0.0-20250610100937-ce7096a3dd37 h1:JD5YDqRrwvZ8AmxEZnek0O5vCTXFtfjw+7RGx/yKA8c=
gitea.pena/SQuiz/common v0.0.0-20250610100937-ce7096a3dd37/go.mod h1:zCrUwDh0APpztKk6NUqTZv+zhjVbWpGBJiJ5z9dAH0U= gitea.pena/SQuiz/common v0.0.0-20250610100937-ce7096a3dd37/go.mod h1:zCrUwDh0APpztKk6NUqTZv+zhjVbWpGBJiJ5z9dAH0U=
gitea.pena/SQuiz/common v0.0.0-20250707081926-0ad481f2ad6b h1:Y8aNl/d/g2MwCpn/QG2oNe00RAOUZreClByy3cztCJk=
gitea.pena/SQuiz/common v0.0.0-20250707081926-0ad481f2ad6b/go.mod h1:zCrUwDh0APpztKk6NUqTZv+zhjVbWpGBJiJ5z9dAH0U=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/ClickHouse/clickhouse-go v1.5.4 h1:cKjXeYLNWVJIx2J1K6H2CqyRmfwVJVY1OV1coaaFcI0= github.com/ClickHouse/clickhouse-go v1.5.4 h1:cKjXeYLNWVJIx2J1K6H2CqyRmfwVJVY1OV1coaaFcI0=
github.com/ClickHouse/clickhouse-go v1.5.4/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI= github.com/ClickHouse/clickhouse-go v1.5.4/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=

@ -61,12 +61,14 @@ func NewSendToClient(deps DepsSendToClient, errChan chan<- error) *SendToClient
model.LeadTargetEmail: toClientTemplate, model.LeadTargetEmail: toClientTemplate,
model.LeadTargetTg: toClientTgTemplate, model.LeadTargetTg: toClientTgTemplate,
model.LeadTargetWhatsapp: toClientWhatsAppTemplate, model.LeadTargetWhatsapp: toClientWhatsAppTemplate,
model.LeadTargetWebhook: "success",
} }
toReminderTemplates := map[model.LeadTargetType]string{ toReminderTemplates := map[model.LeadTargetType]string{
model.LeadTargetEmail: reminderTemplate, model.LeadTargetEmail: reminderTemplate,
model.LeadTargetTg: reminderTgTemplate, model.LeadTargetTg: reminderTgTemplate,
model.LeadTargetWhatsapp: reminderWhatsAppTemplate, model.LeadTargetWhatsapp: reminderWhatsAppTemplate,
model.LeadTargetWebhook: "reject",
} }
return &SendToClient{ return &SendToClient{
@ -104,7 +106,6 @@ func (w *SendToClient) processPendingAnswer(ctx context.Context) {
return return
} }
for _, key := range pendingAnswers { for _, key := range pendingAnswers {
func() { func() {
answerJSON, err := w.redis.GetDel(ctx, key).Result() answerJSON, err := w.redis.GetDel(ctx, key).Result()
@ -386,6 +387,7 @@ func (w *SendToClient) ProcessMessageToClient(ctx context.Context, constructData
AnswerContent: constructData.AnswerContent, AnswerContent: constructData.AnswerContent,
AllAnswers: constructData.AllAnswers, AllAnswers: constructData.AllAnswers,
QuestionsMap: constructData.QuestionsMap, QuestionsMap: constructData.QuestionsMap,
QuizID: int64(constructData.Quiz.Id),
} }
dayOfWeek := wctools.DaysOfWeek[constructData.AnswerTime.Format("Monday")] dayOfWeek := wctools.DaysOfWeek[constructData.AnswerTime.Format("Monday")]

@ -94,7 +94,8 @@ func New(ctx context.Context, cfg initialize.Config, build Build) error {
// return nil, err // return nil, err
// } // }
mailSender := senders.NewMailLeadSender(clients.MailClient) mailSender := senders.NewMailLeadSender(clients.MailClient)
leadSenders := []senders.LeadSender{mailSender /* , tgSender */} webhookSender := senders.NewWebhookSender()
leadSenders := []senders.LeadSender{mailSender, webhookSender /* , tgSender */}
pgdal, err := dal.New(ctx, cfg.PostgresURL, minioClient) pgdal, err := dal.New(ctx, cfg.PostgresURL, minioClient)
if err != nil { if err != nil {

@ -0,0 +1,69 @@
package senders
import (
"encoding/json"
"errors"
"fmt"
"gitea.pena/SQuiz/common/model"
"github.com/gofiber/fiber/v2"
)
type WebhookSender struct {
client *fiber.Client
}
func NewWebhookSender() *WebhookSender {
return &WebhookSender{
client: fiber.AcquireClient(),
}
}
type WebhookPayload struct {
QuizConfig model.ResultInfo `json:"quiz_config"`
AnswerContent model.ResultContent `json:"answer_content"`
AllAnswers []model.ResultAnswer `json:"all_answers"`
QuestionsMap map[uint64]string `json:"questions_map"`
AnswerTime string `json:"answer_time"`
QuizID int64 `json:"quiz_id"`
Subject string `json:"subject"`
}
func (w *WebhookSender) SendLead(data LeadData) error {
if data.Template == "reject" { // это у нас для неуплоченных, смысл отправлять пост с напомиашкой есть?
return nil
}
webhookData := WebhookPayload{
QuizConfig: data.TemplateData.QuizConfig,
AnswerContent: data.TemplateData.AnswerContent,
AllAnswers: data.TemplateData.AllAnswers,
QuestionsMap: data.TemplateData.QuestionsMap,
AnswerTime: data.TemplateData.AnswerTime,
QuizID: data.TemplateData.QuizID,
Subject: data.Subject,
}
jsonData, err := json.Marshal(webhookData)
if err != nil {
return fmt.Errorf("error marshaling webhook data: %w", err)
}
url := data.To.(string)
agent := w.client.Post(url)
agent.Set("Content-Type", "application/json").Body(jsonData)
statusCode, _, errs := agent.Bytes()
if len(errs) > 0 {
return fmt.Errorf("error send webhook request: %w", errors.Join(errs...))
}
if statusCode != fiber.StatusOK {
return fmt.Errorf("received an incorrect status code from webhook: %d", statusCode)
}
return nil
}
func (w *WebhookSender) Name() string {
return "webhook"
}