71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package matrixbot
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"matrix-scores-bot/cbb"
|
|
"matrix-scores-bot/cfb"
|
|
|
|
mautrix "maunium.net/go/mautrix"
|
|
"maunium.net/go/mautrix/event"
|
|
)
|
|
|
|
// HandleMessage processes incoming Matrix messages and dispatches commands with HTML formatting.
|
|
func HandleMessage(evt *event.Event, client *mautrix.Client) {
|
|
msg := evt.Content.AsMessage()
|
|
if msg.MsgType != event.MsgText {
|
|
return
|
|
}
|
|
fields := strings.Fields(msg.Body)
|
|
if len(fields) == 0 {
|
|
return
|
|
}
|
|
|
|
cmd := fields[0]
|
|
var plain, html string
|
|
|
|
switch cmd {
|
|
case "!cfb":
|
|
if len(fields) < 2 {
|
|
plain = "Usage: !cfb <teamName>"
|
|
html = plain
|
|
} else {
|
|
team := strings.Join(fields[1:], " ")
|
|
html = cfb.GetGameInfo(team)
|
|
plain = strings.ReplaceAll(html, "<strong>", "")
|
|
plain = strings.ReplaceAll(plain, "</strong>", "")
|
|
}
|
|
|
|
case "!cbb":
|
|
if len(fields) < 2 {
|
|
plain = "Usage: !cbb <teamName> [MM/DD]"
|
|
html = plain
|
|
} else {
|
|
team := fields[1]
|
|
date := ""
|
|
if len(fields) >= 3 {
|
|
date = fields[2]
|
|
}
|
|
html = cbb.GetGameInfo(team, date)
|
|
plain = strings.ReplaceAll(html, "<strong>", "")
|
|
plain = strings.ReplaceAll(plain, "</strong>", "")
|
|
}
|
|
|
|
default:
|
|
return
|
|
}
|
|
|
|
content := event.MessageEventContent{
|
|
MsgType: event.MsgText,
|
|
Body: plain,
|
|
Format: "org.matrix.custom.html",
|
|
FormattedBody: html,
|
|
}
|
|
|
|
if _, err := client.SendMessageEvent(context.Background(), evt.RoomID, event.EventMessage, content); err != nil {
|
|
// Log failure but do not panic
|
|
println("Failed to send formatted message:", err.Error())
|
|
}
|
|
}
|