diff --git a/clients/aiClient.go b/clients/aiClient.go new file mode 100644 index 0000000..7fea6bb --- /dev/null +++ b/clients/aiClient.go @@ -0,0 +1,36 @@ +package clients + +import ( + "fmt" + "github.com/gofiber/fiber/v2" +) + +type AIClient struct { + baseURL string + httpClient *fiber.Client +} + +type SendAnswerRequest struct { + Tipe string `json:"type"` + Message string `json:"message"` + Final bool `json:"final"` + Session string `json:"session"` +} + +func NewAiClient(baseURL string) *AIClient { + return &AIClient{ + baseURL: baseURL, + httpClient: fiber.AcquireClient(), + } +} + +func (client *AIClient) SendAnswerer(final bool, tipe, message, session string) error { + req := SendAnswerRequest{ + Tipe: tipe, + Message: message, + Final: final, + Session: session, + } + fmt.Println(req) + return nil +} diff --git a/service/service.go b/service/service.go index c483d8c..f0a9cb5 100644 --- a/service/service.go +++ b/service/service.go @@ -33,6 +33,7 @@ type Service struct { workerSendClientCh chan<- model.Answer encrypt *utils.Encrypt redirectURl string + aiClient *clients.AIClient } type ServiceDeps struct { @@ -42,6 +43,7 @@ type ServiceDeps struct { WorkerSendClientCh chan<- model.Answer Encrypt *utils.Encrypt RedirectURl string + AiClient *clients.AIClient } func New(deps ServiceDeps) *Service { @@ -54,6 +56,7 @@ func New(deps ServiceDeps) *Service { workerSendClientCh: deps.WorkerSendClientCh, encrypt: deps.Encrypt, redirectURl: deps.RedirectURl, + aiClient: deps.AiClient, } } @@ -362,6 +365,33 @@ func (s *Service) PutAnswersOnePiece(c *fiber.Ctx) error { for _, ans := range answersRaw { + if quiz.Status == model.StatusAI { + final := false + if finalStr, exists := form.Value["final"]; exists && len(finalStr) > 0 { + parsedFinal, err := strconv.ParseBool(finalStr[0]) + if err != nil { + return c.Status(fiber.StatusBadRequest).SendString("invalid final value") + } + final = parsedFinal + } + + question, err := s.dal.QuestionRepo.GetQuestionListByIDs(c.Context(), []int32{int32(ans.QuestionId)}) + if err != nil { + return c.Status(fiber.StatusInternalServerError).SendString("can not get questions") + } + + if len(question) == 0 { + return c.Status(fiber.StatusNotFound).SendString("no questions found") + } + + err = s.aiClient.SendAnswerer(final, question[0].Type, ans.Content, cs) + if err != nil { + return c.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("can not send answer to ai, err: %s", err.Error())) + } + + // todo тут надо создать вопрос по ответу от ai + } + ans.DeviceType = deviceType ans.OS = os ans.Browser = browser