68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"log"
|
||
|
|
"net/http"
|
||
|
|
"net/url"
|
||
|
|
"os"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"github.com/AnthonyHewins/gotfy"
|
||
|
|
)
|
||
|
|
|
||
|
|
var client *gotfy.Publisher
|
||
|
|
|
||
|
|
type authdTransport struct {
|
||
|
|
underlyingTransport http.RoundTripper
|
||
|
|
}
|
||
|
|
|
||
|
|
func (t *authdTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||
|
|
var ntfyToken string
|
||
|
|
env := os.Environ()
|
||
|
|
for _, keyVal := range env {
|
||
|
|
keyValArr := strings.Split(keyVal, "=")
|
||
|
|
if keyValArr[0] == "NTFY_TOKEN" {
|
||
|
|
ntfyToken = keyValArr[1]
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
req.Header.Add("Authorization", "Bearer "+ntfyToken)
|
||
|
|
return t.underlyingTransport.RoundTrip(req)
|
||
|
|
}
|
||
|
|
|
||
|
|
func SetupNtfyClient() {
|
||
|
|
server, err := url.Parse("https://home.collinduncan.com/ntfy")
|
||
|
|
|
||
|
|
if err != nil {
|
||
|
|
log.Fatal(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
authdHttpClient := &http.Client{Transport: &authdTransport{underlyingTransport: http.DefaultTransport}}
|
||
|
|
|
||
|
|
tp, err := gotfy.NewPublisher(server, authdHttpClient)
|
||
|
|
|
||
|
|
if err != nil {
|
||
|
|
log.Fatal(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
client = tp
|
||
|
|
resp, err := client.SendMessage(context.Background(), &gotfy.Message{
|
||
|
|
Topic: "collinenlucy_nl",
|
||
|
|
Message: "Collin RSVP'd",
|
||
|
|
Title: "New RSVP",
|
||
|
|
Tags: []string{},
|
||
|
|
Priority: gotfy.Default,
|
||
|
|
})
|
||
|
|
|
||
|
|
if err != nil {
|
||
|
|
log.Fatal(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
fmt.Printf("%s\n", resp.ID)
|
||
|
|
}
|
||
|
|
|
||
|
|
func SendRsvpNotification() {
|
||
|
|
}
|