75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/gotify/go-api-client/v2/auth"
|
|
"github.com/gotify/go-api-client/v2/client"
|
|
"github.com/gotify/go-api-client/v2/client/message"
|
|
"github.com/gotify/go-api-client/v2/gotify"
|
|
"github.com/gotify/go-api-client/v2/models"
|
|
)
|
|
|
|
var gotifyClient *client.GotifyREST
|
|
|
|
func getRoot(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "POST" {
|
|
messageContent, _ := io.ReadAll(r.Body)
|
|
if !r.URL.Query().Has("token") {
|
|
io.WriteString(w, "Missing token\n")
|
|
return
|
|
}
|
|
|
|
params := message.NewCreateMessageParams()
|
|
if string(messageContent) == "" {
|
|
io.WriteString(w, "Missing message\n")
|
|
return
|
|
}
|
|
|
|
params.Body = &models.MessageExternal{
|
|
Message: string(messageContent),
|
|
}
|
|
|
|
if r.URL.Query().Has("priority") {
|
|
params.Body.Priority, _ = strconv.Atoi(r.URL.Query().Get("priority"))
|
|
}
|
|
|
|
_, err := gotifyClient.Message.CreateMessage(params, auth.TokenAuth(r.URL.Query().Get("token")))
|
|
|
|
if err != nil {
|
|
log.Fatalf("Could not send message %v", err)
|
|
return
|
|
}
|
|
|
|
io.WriteString(w, "OK\n")
|
|
|
|
} else {
|
|
io.WriteString(w, "Webhook to Gotify forwarder\n")
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
gotifyUrl, present := os.LookupEnv("GOTIFY_URL")
|
|
if !present {
|
|
log.Fatal("Missing GOTIFY_URL")
|
|
}
|
|
gotifyUrlParsed, _ := url.Parse(gotifyUrl)
|
|
gotifyClient = gotify.NewClient(gotifyUrlParsed, &http.Client{})
|
|
versionResponse, err := gotifyClient.Version.GetVersion(nil)
|
|
if err != nil {
|
|
log.Fatal("Could not request version from Gotify ", err)
|
|
}
|
|
version := versionResponse.Payload
|
|
log.Println("Found Gotify version", *version)
|
|
|
|
http.HandleFunc("/", getRoot)
|
|
err = http.ListenAndServe(":3333", nil)
|
|
if err != nil {
|
|
log.Fatal("Failed to start HTTP Server ", err)
|
|
}
|
|
}
|