diff --git a/api_setpusher.go b/api_setpusher.go index e1ed92e0f2716eed2ac4b95ea00a47f093e30dd4..c77a8aeaabe11b80528b33cf5a1fb88ae2b2953d 100644 --- a/api_setpusher.go +++ b/api_setpusher.go @@ -26,20 +26,8 @@ type PusherData struct { Url string `json:"url"` } -func SetPusher(token MatrixToken, url string) error { - body, err := json.Marshal(PusherRequest{ - AppDisplayName: "webhook", - AppId: "de.justjanne.webhook", - Append: false, - Data: PusherData{ - Url: url, - }, - DeviceDisplayName: "webhook", - Kind: "http", - Lang: "en", - ProfileTag: "abcdef", - PushKey: "webhook", - }) +func SetPusher(token MatrixToken, pusher PusherRequest) error { + body, err := json.Marshal(pusher) if err != nil { return err } diff --git a/matrixbot.go b/matrixbot.go index 9c7afb29abd689665b00ca5da8735187f15030ff..ae169822c17ffdddc3b0e00ab65884b7e97e6e49 100644 --- a/matrixbot.go +++ b/matrixbot.go @@ -11,17 +11,13 @@ import ( ) type MatrixBot struct { - Token *MatrixToken - PushUrl *url.URL - handlers map[string]func(bot *MatrixBot, notification Notification) error -} - -func NewMatrixBot(pushUrl *url.URL) *MatrixBot { - return &MatrixBot{ - Token: nil, - PushUrl: pushUrl, - handlers: make(map[string]func(bot *MatrixBot, notification Notification) error), - } + AppName string + AppId string + ProfileTag string + PushKey string + PushUrl *url.URL + Token *MatrixToken + handlers map[string]func(bot *MatrixBot, notification Notification) error } func (bot *MatrixBot) RefreshToken() error { @@ -65,11 +61,30 @@ func (bot *MatrixBot) RefreshTask() { } func (bot *MatrixBot) RegisterPusher() error { - return SetPusher(*bot.Token, bot.PushUrl.String()) + return SetPusher(*bot.Token, PusherRequest{ + AppDisplayName: bot.AppName, + AppId: bot.AppId, + Append: false, + Data: PusherData{ + Url: bot.PushUrl.String(), + }, + DeviceDisplayName: bot.AppName, + Kind: "http", + Lang: "en", + ProfileTag: bot.ProfileTag, + PushKey: bot.AppId, + }) +} + +func (bot *MatrixBot) ensureHandlers() map[string]func(bot *MatrixBot, notification Notification) error { + if bot.handlers == nil { + bot.handlers = make(map[string]func(bot *MatrixBot, notification Notification) error) + } + return bot.handlers } func (bot *MatrixBot) HandleFunc(command string, handler func(bot *MatrixBot, notification Notification) error) { - bot.handlers[command] = handler + bot.ensureHandlers()[command] = handler } func (bot *MatrixBot) Serve(endpoint string) { @@ -93,7 +108,7 @@ func (bot *MatrixBot) Serve(endpoint string) { return } command := strings.SplitN(notification.Content.Body, " ", 2)[0] - handler, ok := bot.handlers[command] + handler, ok := bot.ensureHandlers()[command] if !ok { log.Printf("could not find handler for '%s'\n", command) return