vega/testbot/messages.go

78 lines
1.5 KiB
Go

package main
import (
"log"
"strings"
"git.saintnet.tech/stryan/vega/botlib"
"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) botlib.Message {
return botlib.Message{
Type: botlib.MessageResponse,
Sender: t.Conf.Name,
Receiver: t.Conf.Name,
Body: "Command not found",
}
}
}
msg := response(body_s[1:]...)
err := t.Message(e.RoomID, msg.Body)
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)
}
}
}