vega/testbot/messages.go

78 lines
1.5 KiB
Go
Raw Normal View History

2020-04-11 20:26:59 +00:00
package main
import (
"log"
"strings"
2020-04-21 20:42:39 +00:00
"git.saintnet.tech/stryan/vega/botlib"
2020-04-11 20:26:59 +00:00
"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)
}
}
2020-04-11 20:26:59 +00:00
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)
2020-04-11 20:26:59 +00:00
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
}
2020-04-14 20:54:31 +00:00
if body_s[1] == "stop" {
2020-04-11 20:26:59 +00:00
err := t.Message(e.RoomID, "Shutting down...")
if err != nil {
log.Fatal(err)
}
t.Shutdown()
return
}
2020-04-14 20:54:31 +00:00
response, found := ActionList[body_s[1]]
if !found {
2020-04-21 20:42:39 +00:00
response = func(inputs ...string) botlib.Message {
return botlib.Message{
Type: botlib.MessageResponse,
Sender: t.Conf.Name,
Receiver: t.Conf.Name,
Body: "Command not found",
}
}
2020-04-14 20:54:31 +00:00
}
msg := response(body_s[1:]...)
2020-04-21 20:42:39 +00:00
err := t.Message(e.RoomID, msg.Body)
2020-04-14 20:54:31 +00:00
if err != nil {
log.Fatal(err)
}
return
2020-04-11 20:26:59 +00:00
}
func (t *TestBot) MessageHandler(cancelChan <-chan bool) {
for {
select {
case <-cancelChan:
return
case event := <-t.Jobs:
t.HandleMessage(event)
}
}
}