2025-04-08 14:33:15 +00:00
|
|
|
package clients
|
|
|
|
|
|
|
|
import (
|
2025-04-08 14:34:01 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2025-04-08 14:33:15 +00:00
|
|
|
"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"`
|
2025-07-05 17:26:55 +00:00
|
|
|
Final bool `json:"is_final"`
|
2025-04-08 14:33:15 +00:00
|
|
|
Session string `json:"session"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAiClient(baseURL string) *AIClient {
|
|
|
|
return &AIClient{
|
|
|
|
baseURL: baseURL,
|
|
|
|
httpClient: fiber.AcquireClient(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-08 14:34:01 +00:00
|
|
|
func (client *AIClient) SendAnswerer(final bool, tipe, message, session string) (string, error) {
|
|
|
|
//req := SendAnswerRequest{
|
|
|
|
// Tipe: tipe,
|
|
|
|
// Message: message,
|
|
|
|
// Final: final,
|
|
|
|
// Session: session,
|
|
|
|
//}
|
|
|
|
|
2025-07-05 17:26:55 +00:00
|
|
|
clownRequest := SendAnswerRequest{
|
|
|
|
Message: message,
|
|
|
|
Tipe: "text",
|
|
|
|
Final: false,
|
2025-06-30 11:40:51 +00:00
|
|
|
Session: session,
|
2025-04-08 14:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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...)
|
2025-04-08 14:33:15 +00:00
|
|
|
}
|
2025-04-08 14:34:01 +00:00
|
|
|
|
2025-07-05 17:26:55 +00:00
|
|
|
if statusCode != fiber.StatusOK {
|
2025-04-08 14:34:01 +00:00
|
|
|
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
|
2025-04-08 14:33:15 +00:00
|
|
|
}
|