43 lines
851 B
Go
43 lines
851 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Notifier struct {
|
|
httpClient *http.Client
|
|
channel string
|
|
}
|
|
|
|
func NewNotifier(channel string) *Notifier {
|
|
return &Notifier{
|
|
httpClient: &http.Client{Timeout: 10 * time.Second},
|
|
channel: channel,
|
|
}
|
|
}
|
|
|
|
func (n *Notifier) Send(msg string) error {
|
|
ntfyUrl := fmt.Sprintf("https://ntfy.sh/%s", n.channel)
|
|
req, err := http.NewRequest(http.MethodPost, ntfyUrl, strings.NewReader(msg))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "text/plain")
|
|
req.Header.Set("Title", "DoctoCheck")
|
|
req.Header.Set("Tags", "medical_symbol")
|
|
resp, err := n.httpClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("unexpected status %d", resp.StatusCode)
|
|
}
|
|
|
|
return nil
|
|
}
|