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

feat: improve customizability

parent 1553ab52
Branches main
Tags v0.1.4
No related merge requests found
...@@ -26,20 +26,8 @@ type PusherData struct { ...@@ -26,20 +26,8 @@ type PusherData struct {
Url string `json:"url"` Url string `json:"url"`
} }
func SetPusher(token MatrixToken, url string) error { func SetPusher(token MatrixToken, pusher PusherRequest) error {
body, err := json.Marshal(PusherRequest{ body, err := json.Marshal(pusher)
AppDisplayName: "webhook",
AppId: "de.justjanne.webhook",
Append: false,
Data: PusherData{
Url: url,
},
DeviceDisplayName: "webhook",
Kind: "http",
Lang: "en",
ProfileTag: "abcdef",
PushKey: "webhook",
})
if err != nil { if err != nil {
return err return err
} }
......
...@@ -11,17 +11,13 @@ import ( ...@@ -11,17 +11,13 @@ import (
) )
type MatrixBot struct { type MatrixBot struct {
Token *MatrixToken AppName string
PushUrl *url.URL AppId string
handlers map[string]func(bot *MatrixBot, notification Notification) error ProfileTag string
} PushKey string
PushUrl *url.URL
func NewMatrixBot(pushUrl *url.URL) *MatrixBot { Token *MatrixToken
return &MatrixBot{ handlers map[string]func(bot *MatrixBot, notification Notification) error
Token: nil,
PushUrl: pushUrl,
handlers: make(map[string]func(bot *MatrixBot, notification Notification) error),
}
} }
func (bot *MatrixBot) RefreshToken() error { func (bot *MatrixBot) RefreshToken() error {
...@@ -65,11 +61,30 @@ func (bot *MatrixBot) RefreshTask() { ...@@ -65,11 +61,30 @@ func (bot *MatrixBot) RefreshTask() {
} }
func (bot *MatrixBot) RegisterPusher() error { 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) { 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) { func (bot *MatrixBot) Serve(endpoint string) {
...@@ -93,7 +108,7 @@ func (bot *MatrixBot) Serve(endpoint string) { ...@@ -93,7 +108,7 @@ func (bot *MatrixBot) Serve(endpoint string) {
return return
} }
command := strings.SplitN(notification.Content.Body, " ", 2)[0] command := strings.SplitN(notification.Content.Body, " ", 2)[0]
handler, ok := bot.handlers[command] handler, ok := bot.ensureHandlers()[command]
if !ok { if !ok {
log.Printf("could not find handler for '%s'\n", command) log.Printf("could not find handler for '%s'\n", command)
return return
......
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