package notifyha import ( "bytes" "encoding/json" "fmt" "net/http" "time" ) type Notification struct { Event string `json:"event"` Severity string `json:"severity"` Title string `json:"title"` Message string `json:"message"` Source string `json:"source"` Tags []string `json:"tags,omitempty"` Timestamp time.Time `json:"timestamp"` } type Client struct { WebhookURL string HTTPClient *http.Client } func New(webhookURL string) *Client { return &Client{ WebhookURL: webhookURL, HTTPClient: &http.Client{ Timeout: 10 * time.Second, }, } } func (c *Client) Send(n Notification) error { n.Timestamp = time.Now().UTC() body, err := json.Marshal(n) if err != nil { return err } resp, err := c.HTTPClient.Post( c.WebhookURL, "application/json", bytes.NewBuffer(body), ) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode >= 300 { return fmt.Errorf("webhook returned %s", resp.Status) } return nil } func (c *Client) Info(title, message, source string) error { return c.Send(Notification{ Event: "notification", Severity: Info, Title: title, Message: message, Source: source, }) } func (c *Client) Warning(title, message, source string) error { return c.Send(Notification{ Event: "notification", Severity: Warning, Title: title, Message: message, Source: source, }) } func (c *Client) Error(title, message, source string) error { return c.Send(Notification{ Event: "notification", Severity: Error, Title: title, Message: message, Source: source, }) } func (c *Client) Critical(title, message, source string) error { return c.Send(Notification{ Event: "notification", Severity: Critical, Title: title, Message: message, Source: source, }) }