vega/testbot/messages.go

64 lines
1.1 KiB
Go
Raw Normal View History

2020-04-11 16:26:59 -04:00
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) HandleMessage(e *mautrix.Event) {
if e.Sender == t.Conf.Userid {
return //we don't care about our own messages
}
log.Printf("Handling message:'%v'\n", e.Content.Body)
2020-04-11 16:26:59 -04: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
}
switch body_s[1] {
case "stop":
err := t.Message(e.RoomID, "Shutting down...")
if err != nil {
log.Fatal(err)
}
t.Shutdown()
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)
}
}
}
func (t *TestBot) MessageHandler(cancelChan <-chan bool) {
for {
select {
case <-cancelChan:
return
case event := <-t.Jobs:
t.HandleMessage(event)
}
}
}