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 {
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
}
......
......@@ -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
......
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