gotify
This commit is contained in:
72
gotify.go
Normal file
72
gotify.go
Normal file
@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type GotifyMessage struct {
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message"`
|
||||
Priority int `json:"priority"`
|
||||
}
|
||||
|
||||
func Gotify(spot FlexSpot) {
|
||||
|
||||
if Cfg.Gotify.Enable {
|
||||
|
||||
message := fmt.Sprintf("DX: %s\nFrom: %s\nFreq: %s\nMode: %s\n", spot.DX, spot.Source, spot.FrequencyMhz, spot.Mode)
|
||||
|
||||
gotifyMsg := GotifyMessage{
|
||||
Title: "",
|
||||
Message: message,
|
||||
Priority: 10,
|
||||
}
|
||||
|
||||
if spot.NewDXCC && Cfg.Gotify.NewDXCC {
|
||||
title := "FlexDXCluster New DXCC"
|
||||
gotifyMsg.Title = title
|
||||
gotifyMsg.Message = message
|
||||
sendToGotify(gotifyMsg)
|
||||
}
|
||||
|
||||
if spot.NewBand && spot.NewMode && Cfg.Gotify.NewBandAndMode {
|
||||
title := "FlexDXCluster New Mode & Band"
|
||||
gotifyMsg.Title = title
|
||||
gotifyMsg.Message = message
|
||||
sendToGotify(gotifyMsg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func sendToGotify(mess GotifyMessage) {
|
||||
jsonData, err := json.Marshal(mess)
|
||||
if err != nil {
|
||||
Log.Errorln("Error marshaling JSON:", err)
|
||||
return
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", Cfg.Gotify.URL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
Log.Errorln("Error creating request:", err)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+Cfg.Gotify.Token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
Log.Errorln("Error sending request:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
Log.Errorln("Gotify server returned non-OK status:", resp.Status)
|
||||
} else {
|
||||
Log.Println("Push successfully sent to Gotify")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user