74 lines
1.4 KiB
Go
74 lines
1.4 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 {
|
|
Title string `json:"title"`
|
|
Text string `json:"text"`
|
|
Final bool `json:"is_final"`
|
|
Session string `json:"string"`
|
|
}{
|
|
Text: message,
|
|
Session: session,
|
|
}
|
|
|
|
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
|
|
}
|