first commit
This commit is contained in:
89
gotify.go
Normal file
89
gotify.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type GotifyMessage struct {
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message"`
|
||||
Priority int `json:"priority"`
|
||||
}
|
||||
|
||||
func Gotify(spot FlexSpot) {
|
||||
|
||||
ExceptionList := "4U1UN 5Z4B OH2B CS3B"
|
||||
|
||||
if Cfg.Gotify.Enable && !strings.Contains(ExceptionList, spot.DX) {
|
||||
|
||||
message := fmt.Sprintf("DX: %s\nFrom: %s\nFreq: %s\nMode: %s\nCountry: %s\nTime: %s\n", spot.DX, spot.SpotterCallsign, spot.FrequencyMhz, spot.Mode, spot.CountryName, spot.UTCTime)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if spot.NewMode && Cfg.Gotify.NewMode && !spot.NewBand {
|
||||
title := "FlexDXCluster New Mode"
|
||||
gotifyMsg.Title = title
|
||||
gotifyMsg.Message = message
|
||||
sendToGotify(gotifyMsg)
|
||||
}
|
||||
|
||||
if spot.NewBand && Cfg.Gotify.NewBand && !spot.NewMode {
|
||||
title := "FlexDXCluster New 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.Debugln("Push successfully sent to Gotify")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user