42 lines
863 B
Go
42 lines
863 B
Go
package adapter
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type barkConfig struct {
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
type barkPayload struct {
|
|
Title string `json:"title"`
|
|
Body string `json:"body"`
|
|
}
|
|
|
|
type BarkSender struct{}
|
|
|
|
func (s *BarkSender) Type() string { return "bark" }
|
|
|
|
func (s *BarkSender) Send(title, content string, config json.RawMessage) error {
|
|
var cfg barkConfig
|
|
if err := json.Unmarshal(config, &cfg); err != nil {
|
|
return fmt.Errorf("parse bark config: %w", err)
|
|
}
|
|
|
|
payload := barkPayload{Title: title, Body: content}
|
|
body, _ := json.Marshal(payload)
|
|
resp, err := http.Post(cfg.URL, "application/json", bytes.NewReader(body))
|
|
if err != nil {
|
|
return fmt.Errorf("bark send: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode >= 400 {
|
|
return fmt.Errorf("bark returned status %d", resp.StatusCode)
|
|
}
|
|
return nil
|
|
}
|