First commit
This commit is contained in:
37
matrixbot/bot.go
Normal file
37
matrixbot/bot.go
Normal file
@ -0,0 +1,37 @@
|
||||
package matrixbot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
mautrix "maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/event"
|
||||
)
|
||||
|
||||
// Bot encapsulates a Matrix client.
|
||||
type Bot struct {
|
||||
Client *mautrix.Client
|
||||
}
|
||||
|
||||
// NewBot creates a new Bot instance.
|
||||
func NewBot(client *mautrix.Client) *Bot {
|
||||
return &Bot{Client: client}
|
||||
}
|
||||
|
||||
// Start begins syncing with the Matrix server and sets up event handlers.
|
||||
// It waits 30 seconds after connecting before registering the message handler
|
||||
// to allow historical messages to be skipped.
|
||||
func (b *Bot) Start() error {
|
||||
syncer := b.Client.Syncer.(*mautrix.DefaultSyncer)
|
||||
|
||||
// Pause for 30 seconds to skip over historical messages
|
||||
time.Sleep(30 * time.Second)
|
||||
|
||||
// Register message handler for new events only
|
||||
syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
|
||||
go HandleMessage(evt, b.Client)
|
||||
})
|
||||
|
||||
// Start the sync loop (blocks until error).
|
||||
return b.Client.Sync()
|
||||
}
|
70
matrixbot/handler.go
Normal file
70
matrixbot/handler.go
Normal file
@ -0,0 +1,70 @@
|
||||
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())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user