// Package fiskonrelay is the Fiskon Relay Terminal SDK for Go (1.18+). Standard library only. // // client := fiskonrelay.New("YOUR_SECRET_KEY") // res, err := client.SendSms(ctx, map[string]any{"to": "256700000000", "message": "Hello"}) package fiskonrelay import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" "strings" "time" ) const DefaultBase = "https://relay.naweriindustries.com/api/v1" // Error is returned for any non-success reply. type Error struct { Message string Code string Status int Data any } func (e *Error) Error() string { return fmt.Sprintf("fiskonrelay: %s (%s, HTTP %d)", e.Message, e.Code, e.Status) } type Client struct { apiKey, publicKey, base string HTTP *http.Client } // New builds a client with your SECRET key (server side only). func New(apiKey string) *Client { return &Client{apiKey: apiKey, base: strings.TrimRight(DefaultBase, "/"), HTTP: &http.Client{Timeout: 30 * time.Second}} } // NewPublic builds a client with the PUBLISHABLE key (safe in apps; device registration only). func NewPublic(publicKey string) *Client { return &Client{publicKey: publicKey, base: strings.TrimRight(DefaultBase, "/"), HTTP: &http.Client{Timeout: 30 * time.Second}} } func (c *Client) do(ctx context.Context, method, path string, body map[string]any, client bool) (map[string]any, error) { var rd io.Reader if body != nil { b, err := json.Marshal(body) if err != nil { return nil, err } rd = bytes.NewReader(b) } req, err := http.NewRequestWithContext(ctx, method, c.base+path, rd) if err != nil { return nil, err } req.Header.Set("Accept", "application/json") if client && c.publicKey != "" && c.apiKey == "" { req.Header.Set("X-Public-Key", c.publicKey) } else if c.apiKey != "" { req.Header.Set("Authorization", "Bearer "+c.apiKey) } else { return nil, &Error{Message: "this call needs the secret key", Code: "no_key"} } if body != nil { req.Header.Set("Content-Type", "application/json") } resp, err := c.HTTP.Do(req) if err != nil { return nil, err } defer resp.Body.Close() raw, _ := io.ReadAll(resp.Body) var out map[string]any if json.Unmarshal(raw, &out) != nil { out = map[string]any{"error": float64(1), "code": "bad_response", "message": fmt.Sprintf("HTTP %d", resp.StatusCode)} } if resp.StatusCode >= 400 || out["error"] == float64(1) { msg, _ := out["message"].(string) code, _ := out["code"].(string) return out, &Error{Message: msg, Code: code, Status: resp.StatusCode, Data: out["data"]} } return out, nil } func (c *Client) SendEmail(ctx context.Context, p map[string]any) (map[string]any, error) { return c.do(ctx, "POST", "/email", p, false) } func (c *Client) SendSms(ctx context.Context, p map[string]any) (map[string]any, error) { return c.do(ctx, "POST", "/sms", p, false) } func (c *Client) SendWhatsapp(ctx context.Context, p map[string]any) (map[string]any, error) { return c.do(ctx, "POST", "/whatsapp", p, false) } func (c *Client) SendPush(ctx context.Context, p map[string]any) (map[string]any, error) { return c.do(ctx, "POST", "/push", p, false) } func (c *Client) RegisterDevice(ctx context.Context, p map[string]any) (map[string]any, error) { return c.do(ctx, "POST", "/push/devices", p, true) } func (c *Client) RemoveDevice(ctx context.Context, p map[string]any) (map[string]any, error) { return c.do(ctx, "DELETE", "/push/devices", p, true) } func (c *Client) SendTransactional(ctx context.Context, p map[string]any) (map[string]any, error) { return c.do(ctx, "POST", "/transactional/email", p, false) } func (c *Client) Templates(ctx context.Context) (map[string]any, error) { return c.do(ctx, "GET", "/templates", nil, false) } func (c *Client) SendOtp(ctx context.Context, p map[string]any) (map[string]any, error) { return c.do(ctx, "POST", "/otp", p, false) } func (c *Client) VerifyOtp(ctx context.Context, p map[string]any) (map[string]any, error) { return c.do(ctx, "POST", "/otp/verify", p, false) } func (c *Client) Balance(ctx context.Context) (map[string]any, error) { return c.do(ctx, "GET", "/balance", nil, false) } func (c *Client) Status(ctx context.Context, channel string, id int) (map[string]any, error) { return c.do(ctx, "GET", fmt.Sprintf("/messages/%s/%d", channel, id), nil, false) }