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 (
"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)
}
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment