66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package notify
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
)
|
|
|
|
|
|
type NtfyNotifier struct {
|
|
client *http.Client
|
|
}
|
|
|
|
func NewNtfyNotifier() NtfyNotifier {
|
|
return NtfyNotifier{
|
|
client: &http.Client{
|
|
Transport: &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (n NtfyNotifier) SendNotification(camera string, online bool) {
|
|
type ntfyJson struct {
|
|
Topic string `json:"topic"`
|
|
Title string `json:"title"`
|
|
Message string `json:"message"`
|
|
Priority int `json:"priority"`
|
|
Click string `json:"click"`
|
|
Icon string `json:"icon"`
|
|
}
|
|
|
|
var reqJson, jsonErr = json.Marshal(ntfyJson{
|
|
Topic: "frigate_camera_uptime",
|
|
Title: "Frigate",
|
|
Message: createMessage(camera, online),
|
|
Priority: 3,
|
|
Click: fmt.Sprintf("https://frigate.homelab.net/cameras/%s", camera),
|
|
Icon: "https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/frigate.png",
|
|
})
|
|
if jsonErr != nil {
|
|
slog.Error("Failed to construct JSON request body", "error", jsonErr)
|
|
return
|
|
}
|
|
|
|
var req, reqErr = http.NewRequest("POST", "https://ntfy.homelab.net", bytes.NewBuffer([]byte(reqJson)))
|
|
if reqErr != nil {
|
|
slog.Error("Failed to create HTTP request", "error", reqErr)
|
|
return
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
var resp, respErr = n.client.Do(req)
|
|
if respErr != nil {
|
|
slog.Error("Failed to send HTTP request", "error", respErr)
|
|
return
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
slog.Error("Ntfy notification returned HTTP", "status", resp.Status)
|
|
}
|
|
}
|