vega/testbot/messages.go

70 lines
1.3 KiB
Go

package main
import (
"log"
"strings"
"maunium.net/go/mautrix"
)
func (t *TestBot) QueueMessage(e *mautrix.Event) {
t.Jobs <- e
}
func (t *TestBot) Message(roomid, message string) error {
_, err := t.Bot.Client.SendText(roomid, message)
return err
}
func (t *TestBot) TestMessage() {
_, err := t.Bot.Client.SendText(t.Bot.ManagementRoomID, "TestBot Online!")
if err != nil {
log.Fatal(err)
}
}
func (t *TestBot) HandleMessage(e *mautrix.Event) {
if e.Sender == t.Conf.Userid {
return //we don't care about our own messages
}
log.Printf("Handling room %v message:'%v'\n", e.RoomID, e.Content.Body)
body := e.Content.Body
body_s := strings.Split(body, " ")
if body_s[0] != t.Conf.Prefix {
return //disregard non commands
}
if len(body_s) < 2 {
return //nothing to parse
}
if body_s[1] == "stop" {
err := t.Message(e.RoomID, "Shutting down...")
if err != nil {
log.Fatal(err)
}
t.Shutdown()
return
}
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) {
for {
select {
case <-cancelChan:
return
case event := <-t.Jobs:
t.HandleMessage(event)
}
}
}