package client import ( "bytes" "fmt" "io" "net/http" "net/url" "time" "apskel-pos-be/config" ) type FonnteClient interface { SendWhatsAppMessage(target string, message string) error } type fonnteClient struct { httpClient *http.Client apiUrl string token string } type FonnteResponse struct { Status bool `json:"status"` Message string `json:"message"` } func NewFonnteClient(cfg *config.Fonnte) FonnteClient { return &fonnteClient{ httpClient: &http.Client{ Timeout: time.Duration(cfg.GetTimeout()) * time.Second, }, apiUrl: cfg.GetApiUrl(), token: cfg.GetToken(), } } func (c *fonnteClient) SendWhatsAppMessage(target string, message string) error { // Prepare form data data := url.Values{} data.Set("target", target) data.Set("message", message) // Create request req, err := http.NewRequest("POST", c.apiUrl, bytes.NewBufferString(data.Encode())) if err != nil { return fmt.Errorf("failed to create request: %w", err) } // Set headers req.Header.Set("Authorization", c.token) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") // Send request resp, err := c.httpClient.Do(req) if err != nil { return fmt.Errorf("failed to send request: %w", err) } defer resp.Body.Close() // Read response body body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read response body: %w", err) } // Check HTTP status if resp.StatusCode != http.StatusOK { return fmt.Errorf("fonnte API returned status %d: %s", resp.StatusCode, string(body)) } // Log the response for debugging fmt.Printf("Fonnte API response: %s\n", string(body)) return nil }