frigate/uptime-go/uptime/notify/ntfy.go

59 lines
1.3 KiB
Go

package notify
import (
"fmt"
"bytes"
"encoding/json"
"log/slog"
"net/http"
)
type NtfyNotifier struct {
client *http.Client
}
func NewNtfyNotifier() NtfyNotifier {
return NtfyNotifier{
client: &http.Client{},
}
}
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", 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", reqErr)
}
req.Header.Set("Content-Type", "application/json")
var resp, respErr = n.client.Do(req)
if respErr != nil {
slog.Error("Failed to send HTTP request", respErr)
}
if resp.StatusCode != 200 {
slog.Error("Ntfy notification returned HTTP", resp.Status)
}
}