worker/internal/senders/webhook_sender.go
pasha1coil 899d0d54ef
Some checks failed
Deploy / ValidateConfig (push) Has been cancelled
Deploy / MigrateDatabase (push) Has been cancelled
Deploy / DeployService (push) Has been cancelled
Deploy / CreateImage (push) Has been cancelled
minimize WebhookPayload only QAPairs
2025-07-07 11:49:23 +03:00

66 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package senders
import (
"encoding/json"
"errors"
"fmt"
"github.com/gofiber/fiber/v2"
)
type WebhookSender struct {
client *fiber.Client
}
func NewWebhookSender() *WebhookSender {
return &WebhookSender{
client: fiber.AcquireClient(),
}
}
type WebhookPayload struct {
QAPairs map[string]string `json:"qa_pairs"` // вопрос -> ответ
}
func (w *WebhookSender) SendLead(data LeadData) error {
if data.Template == "reject" { // это у нас для неуплоченных, смысл отправлять пост с напомиашкой есть?
return nil
}
qaPairs := make(map[string]string)
for _, answer := range data.TemplateData.AllAnswers {
questionTitle, exists := data.TemplateData.QuestionsMap[answer.QuestionID]
if exists {
qaPairs[questionTitle] = answer.Content
}
}
webhookData := WebhookPayload{
QAPairs: qaPairs,
}
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"
}