| 24 | } |
| 25 | |
| 26 | func (p *pushoverProvider) Send(ctx context.Context, msg *sender.Message) error { |
| 27 | payload := map[string]string{ |
| 28 | "token": p.opt.AppToken, |
| 29 | "user": p.opt.UserKey, |
| 30 | "message": msg.Subject + "\n\n" + msg.Body, |
| 31 | } |
| 32 | |
| 33 | if p.Format() == "html" { |
| 34 | payload["html"] = "1" |
| 35 | } |
| 36 | |
| 37 | targetURL := defaultPushoverURL |
| 38 | if p.opt.Endpoint != "" { |
| 39 | targetURL = p.opt.Endpoint |
| 40 | } |
| 41 | |
| 42 | body, err := json.Marshal(payload) |
| 43 | if err != nil { |
| 44 | return errors.Wrap(err, "error preparing pushover notification") |
| 45 | } |
| 46 | |
| 47 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, targetURL, bytes.NewReader(body)) |
| 48 | if err != nil { |
| 49 | return errors.Wrap(err, "error preparing pushover notification") |
| 50 | } |
| 51 | |
| 52 | req.Header.Set("Content-Type", "application/json") |
| 53 | |
| 54 | resp, err := http.DefaultClient.Do(req) |
| 55 | if err != nil { |
| 56 | return errors.Wrap(err, "error sending pushover notification") |
| 57 | } |
| 58 | |
| 59 | defer resp.Body.Close() //nolint:errcheck |
| 60 | |
| 61 | if resp.StatusCode != http.StatusOK { |
| 62 | return errors.Errorf("error sending pushover notification: %v", resp.Status) |
| 63 | } |
| 64 | |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | func (p *pushoverProvider) Summary() string { |
| 69 | return fmt.Sprintf("Pushover user %q app %q format %q", p.opt.UserKey, p.opt.AppToken, p.Format()) |