add function dispatch
This commit is contained in:
parent
da39ba7b18
commit
f23162d49a
5
testbot/action.go
Normal file
5
testbot/action.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type Action func(inputs ...string) string
|
||||||
|
|
||||||
|
var ActionList map[string]Action
|
@ -26,6 +26,7 @@ func main() {
|
|||||||
Conf: c,
|
Conf: c,
|
||||||
Jobs: make(chan *mautrix.Event, 100),
|
Jobs: make(chan *mautrix.Event, 100),
|
||||||
}
|
}
|
||||||
|
tb.InitActions()
|
||||||
syncer := botlib.NewCustomSyncer("@testbot:saintnet.tech", b.Client.Store)
|
syncer := botlib.NewCustomSyncer("@testbot:saintnet.tech", b.Client.Store)
|
||||||
syncer.OnEventType(mautrix.EventMessage, tb.QueueMessage)
|
syncer.OnEventType(mautrix.EventMessage, tb.QueueMessage)
|
||||||
syncer.OnEventType(mautrix.StateMember, b.DefaultHandleMember)
|
syncer.OnEventType(mautrix.StateMember, b.DefaultHandleMember)
|
||||||
|
@ -30,25 +30,24 @@ func (t *TestBot) HandleMessage(e *mautrix.Event) {
|
|||||||
if len(body_s) < 2 {
|
if len(body_s) < 2 {
|
||||||
return //nothing to parse
|
return //nothing to parse
|
||||||
}
|
}
|
||||||
switch body_s[1] {
|
if body_s[1] == "stop" {
|
||||||
case "stop":
|
|
||||||
err := t.Message(e.RoomID, "Shutting down...")
|
err := t.Message(e.RoomID, "Shutting down...")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Shutdown()
|
t.Shutdown()
|
||||||
return
|
return
|
||||||
case "echo":
|
|
||||||
err := t.Message(e.RoomID, body)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
case "version":
|
|
||||||
err := t.Message(e.RoomID, "0.0.1")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
response, found := ActionList[body_s[1]]
|
||||||
|
if !found {
|
||||||
|
response = func(inputs ...string) string { return "Command not found" }
|
||||||
|
}
|
||||||
|
msg := response(body_s[1:]...)
|
||||||
|
err := t.Message(e.RoomID, msg)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TestBot) MessageHandler(cancelChan <-chan bool) {
|
func (t *TestBot) MessageHandler(cancelChan <-chan bool) {
|
||||||
|
@ -39,3 +39,11 @@ func (t *TestBot) LoadState() {
|
|||||||
}
|
}
|
||||||
t.Bot.ManagementRoomID = strings.Trim(string(dat), "")
|
t.Bot.ManagementRoomID = strings.Trim(string(dat), "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TestBot) InitActions() {
|
||||||
|
ActionList = make(map[string]Action)
|
||||||
|
ActionList["version"] = func(inputs ...string) string { return "0.0.1" }
|
||||||
|
ActionList["echo"] = func(inputs ...string) string {
|
||||||
|
return strings.Join(inputs[:], "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user