Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
- internal/ghl/oauth.go: acknowledge fmt.Fprint return (errcheck) - internal/ghl/api.go: handle io.ReadAll error instead of discarding (errcheck) - internal/cast/client.go: replace defer-in-loop with explicit Body.Close after ReadAll (gocritic defer-in-loop) - internal/phone/normalize.go: move inline regexp.MustCompile to package-level var e164Pattern (gocritic / performance) Co-Authored-By: Paperclip <noreply@paperclip.ing>
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package ghl
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const ghlAPIBase = "https://services.leadconnectorhq.com"
|
|
const ghlAPIVersion = "2021-04-15"
|
|
|
|
type APIClient struct {
|
|
baseURL string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func NewAPIClient() *APIClient {
|
|
return &APIClient{
|
|
baseURL: ghlAPIBase,
|
|
httpClient: &http.Client{Timeout: 30 * time.Second},
|
|
}
|
|
}
|
|
|
|
func (c *APIClient) UpdateMessageStatus(ctx context.Context, accessToken, messageID, status string) error {
|
|
body, err := json.Marshal(MessageStatusUpdate{Status: status})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
u := fmt.Sprintf("%s/conversations/messages/%s/status", c.baseURL, messageID)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, u, bytes.NewReader(body))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Version", ghlAPIVersion)
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return fmt.Errorf("ghl update status: failed to read response body: %w", err)
|
|
}
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
return fmt.Errorf("ghl update status returned %d: %s", resp.StatusCode, string(respBody))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *APIClient) PostInboundMessage(ctx context.Context, accessToken string, msg *InboundMessage) (*InboundMessageResponse, error) {
|
|
body, err := json.Marshal(msg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
u := fmt.Sprintf("%s/conversations/messages/inbound", c.baseURL)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, bytes.NewReader(body))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Version", ghlAPIVersion)
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
return nil, fmt.Errorf("ghl inbound message returned %d: %s", resp.StatusCode, string(respBody))
|
|
}
|
|
|
|
var result InboundMessageResponse
|
|
if err := json.Unmarshal(respBody, &result); err != nil {
|
|
return nil, fmt.Errorf("failed to parse inbound message response: %w", err)
|
|
}
|
|
return &result, nil
|
|
}
|