From e921ee64711396fa29300fc5bea9c2b60004567d Mon Sep 17 00:00:00 2001 From: Janne Mareike Koschinski <janne@kuschku.de> Date: Fri, 28 Feb 2025 13:07:06 +0100 Subject: [PATCH] fix: push url setup --- main.go | 9 +++++++-- matrixbot.go | 11 +++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 80556d9..629feeb 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "html/template" "math/rand/v2" "net/http" + "net/url" "os" "strings" ) @@ -15,7 +16,11 @@ import ( func main() { var err error - bot := NewMatrixBot() + pushUrl, err := url.Parse(os.Getenv("BOT_PUSHURL")) + if err != nil { + panic(err) + } + bot := NewMatrixBot(pushUrl) // !8ball handler bot.HandleFunc("!8ball", func(bot *MatrixBot, notification api.Notification) error { @@ -112,7 +117,7 @@ func main() { if err != nil { panic(err) } - err = bot.RegisterPusher(os.Getenv("BOT_PUSHURL")) + err = bot.RegisterPusher() if err != nil { panic(err) } diff --git a/matrixbot.go b/matrixbot.go index ede760a..6254142 100644 --- a/matrixbot.go +++ b/matrixbot.go @@ -6,18 +6,21 @@ import ( "io" "log" "net/http" + "net/url" "strings" "time" ) type MatrixBot struct { token *api.Token + pushUrl *url.URL handlers map[string]func(bot *MatrixBot, notification api.Notification) error } -func NewMatrixBot() *MatrixBot { +func NewMatrixBot(pushUrl *url.URL) *MatrixBot { return &MatrixBot{ token: nil, + pushUrl: pushUrl, handlers: make(map[string]func(bot *MatrixBot, notification api.Notification) error), } } @@ -62,8 +65,8 @@ func (bot *MatrixBot) RefreshTask() { } } -func (bot *MatrixBot) RegisterPusher(url string) error { - return api.SetPusher(*bot.token, url) +func (bot *MatrixBot) RegisterPusher() error { + return api.SetPusher(*bot.token, bot.pushUrl.String()) } func (bot *MatrixBot) HandleFunc(command string, handler func(bot *MatrixBot, notification api.Notification) error) { @@ -75,7 +78,7 @@ func (bot *MatrixBot) Serve(endpoint string) { writer.WriteHeader(200) _, _ = io.WriteString(writer, "OK\n") }) - http.HandleFunc("/_matrix/push/v1/notify", func(writer http.ResponseWriter, request *http.Request) { + http.HandleFunc(bot.pushUrl.RequestURI(), func(writer http.ResponseWriter, request *http.Request) { log.Println("Received push notification") notification, err := api.ParseNotification(request.Body) if err != nil { -- GitLab