|
| 1 | +package ntfy |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + |
| 10 | + "github.com/crazy-max/diun/v4/internal/model" |
| 11 | + "github.com/crazy-max/diun/v4/internal/msg" |
| 12 | + "github.com/crazy-max/diun/v4/internal/notif/notifier" |
| 13 | +) |
| 14 | + |
| 15 | +// Client represents an active ntfy notification object |
| 16 | +type Client struct { |
| 17 | + *notifier.Notifier |
| 18 | + cfg *model.NotifNtfy |
| 19 | + meta model.Meta |
| 20 | +} |
| 21 | + |
| 22 | +// New creates a new ntfy notification instance |
| 23 | +func New(config *model.NotifNtfy, meta model.Meta) notifier.Notifier { |
| 24 | + return notifier.Notifier{ |
| 25 | + Handler: &Client{ |
| 26 | + cfg: config, |
| 27 | + meta: meta, |
| 28 | + }, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Name returns notifier's name |
| 33 | +func (c *Client) Name() string { |
| 34 | + return "ntfy" |
| 35 | +} |
| 36 | + |
| 37 | +// Send creates and sends a ntfy notification with an entry |
| 38 | +func (c *Client) Send(entry model.NotifEntry) error { |
| 39 | + hc := http.Client{ |
| 40 | + Timeout: *c.cfg.Timeout, |
| 41 | + } |
| 42 | + |
| 43 | + message, err := msg.New(msg.Options{ |
| 44 | + Meta: c.meta, |
| 45 | + Entry: entry, |
| 46 | + TemplateTitle: c.cfg.TemplateTitle, |
| 47 | + TemplateBody: c.cfg.TemplateBody, |
| 48 | + }) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + title, body, err := message.RenderMarkdown() |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + dataBuf := new(bytes.Buffer) |
| 59 | + if err := json.NewEncoder(dataBuf).Encode(struct { |
| 60 | + Topic string `json:"topic"` |
| 61 | + Message string `json:"message"` |
| 62 | + Title string `json:"title"` |
| 63 | + Priority int `json:"priority"` |
| 64 | + Tags []string `json:"tags"` |
| 65 | + }{ |
| 66 | + Topic: c.cfg.Topic, |
| 67 | + Message: string(body), |
| 68 | + Title: string(title), |
| 69 | + Priority: c.cfg.Priority, |
| 70 | + Tags: c.cfg.Tags, |
| 71 | + }); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + u, err := url.Parse(c.cfg.Endpoint) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + q := u.Query() |
| 81 | + u.RawQuery = q.Encode() |
| 82 | + |
| 83 | + req, err := http.NewRequest("POST", u.String(), dataBuf) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + req.Header.Set("Content-Type", "application/json") |
| 89 | + req.Header.Set("User-Agent", c.meta.UserAgent) |
| 90 | + |
| 91 | + resp, err := hc.Do(req) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + if resp.StatusCode != http.StatusOK { |
| 97 | + var errBody struct { |
| 98 | + Error string `json:"error"` |
| 99 | + ErrorCode int `json:"errorCode"` |
| 100 | + ErrorDescription string `json:"errorDescription"` |
| 101 | + } |
| 102 | + err := json.NewDecoder(resp.Body).Decode(&errBody) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + return fmt.Errorf("%d %s: %s", errBody.ErrorCode, errBody.Error, errBody.ErrorDescription) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
0 commit comments