answerer/clients/aiClient.go

70 lines
1.3 KiB
Go

package clients
import (
"encoding/json"
"errors"
"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) (string, error) {
//req := SendAnswerRequest{
// Tipe: tipe,
// Message: message,
// Final: final,
// Session: session,
//}
clownRequest := struct {
Text string `json:"text"`
}{
Text: message,
}
body, err := json.Marshal(clownRequest)
if err != nil {
return "", err
}
agent := client.httpClient.Post(client.baseURL)
agent.Set("Content-Type", "application/json").Body(body)
statusCode, respBody, errs := agent.Bytes()
if len(errs) > 0 {
return "", errors.Join(errs...)
}
if statusCode != fiber.StatusCreated {
return "", fmt.Errorf("invalid response status code: %d", statusCode)
}
resp := struct {
Text string `json:"text"`
}{}
if err := json.Unmarshal(respBody, &resp); err != nil {
return "", err
}
return resp.Text, nil
}