Skip to content
Snippets Groups Projects
Verified Commit e921ee64 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

fix: push url setup

parent 6f9e6245
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"html/template" "html/template"
"math/rand/v2" "math/rand/v2"
"net/http" "net/http"
"net/url"
"os" "os"
"strings" "strings"
) )
...@@ -15,7 +16,11 @@ import ( ...@@ -15,7 +16,11 @@ import (
func main() { func main() {
var err error var err error
bot := NewMatrixBot() pushUrl, err := url.Parse(os.Getenv("BOT_PUSHURL"))
if err != nil {
panic(err)
}
bot := NewMatrixBot(pushUrl)
// !8ball handler // !8ball handler
bot.HandleFunc("!8ball", func(bot *MatrixBot, notification api.Notification) error { bot.HandleFunc("!8ball", func(bot *MatrixBot, notification api.Notification) error {
...@@ -112,7 +117,7 @@ func main() { ...@@ -112,7 +117,7 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = bot.RegisterPusher(os.Getenv("BOT_PUSHURL")) err = bot.RegisterPusher()
if err != nil { if err != nil {
panic(err) panic(err)
} }
......
...@@ -6,18 +6,21 @@ import ( ...@@ -6,18 +6,21 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"net/url"
"strings" "strings"
"time" "time"
) )
type MatrixBot struct { type MatrixBot struct {
token *api.Token token *api.Token
pushUrl *url.URL
handlers map[string]func(bot *MatrixBot, notification api.Notification) error handlers map[string]func(bot *MatrixBot, notification api.Notification) error
} }
func NewMatrixBot() *MatrixBot { func NewMatrixBot(pushUrl *url.URL) *MatrixBot {
return &MatrixBot{ return &MatrixBot{
token: nil, token: nil,
pushUrl: pushUrl,
handlers: make(map[string]func(bot *MatrixBot, notification api.Notification) error), handlers: make(map[string]func(bot *MatrixBot, notification api.Notification) error),
} }
} }
...@@ -62,8 +65,8 @@ func (bot *MatrixBot) RefreshTask() { ...@@ -62,8 +65,8 @@ func (bot *MatrixBot) RefreshTask() {
} }
} }
func (bot *MatrixBot) RegisterPusher(url string) error { func (bot *MatrixBot) RegisterPusher() error {
return api.SetPusher(*bot.token, url) return api.SetPusher(*bot.token, bot.pushUrl.String())
} }
func (bot *MatrixBot) HandleFunc(command string, handler func(bot *MatrixBot, notification api.Notification) error) { func (bot *MatrixBot) HandleFunc(command string, handler func(bot *MatrixBot, notification api.Notification) error) {
...@@ -75,7 +78,7 @@ func (bot *MatrixBot) Serve(endpoint string) { ...@@ -75,7 +78,7 @@ func (bot *MatrixBot) Serve(endpoint string) {
writer.WriteHeader(200) writer.WriteHeader(200)
_, _ = io.WriteString(writer, "OK\n") _, _ = 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") log.Println("Received push notification")
notification, err := api.ParseNotification(request.Body) notification, err := api.ParseNotification(request.Body)
if err != nil { if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment