respond to ping message

This commit is contained in:
lyx0 2024-01-10 18:10:28 +01:00
parent fcef2c479f
commit 9d25b1569c
3 changed files with 83 additions and 13 deletions

31
cmd/nourybot/commands.go Normal file
View file

@ -0,0 +1,31 @@
package main
import (
"strings"
"maunium.net/go/mautrix/event"
)
func (app *Application) ParseCommand(evt *event.Event) {
// commandName is the actual name of the command without the prefix.
// e.g. `!ping` would be `ping`.
//commandName := strings.ToLower(strings.SplitN(evt.Content.AsMessage().Body, " ", 2)[0][1:])
// cmdParams are additional command parameters.
// e.g. `!weather san antonio`
// cmdParam[0] is `san` and cmdParam[1] = `antonio`.
cmdParams := strings.SplitN(evt.Content.AsMessage().Body, " ", 500)
app.Log.Info().Msgf("cmdParams: %s", cmdParams)
// msgLen is the amount of words in a message without the prefix.
// Useful to check if enough cmdParams are provided.
//msgLen := len(strings.SplitN(evt.Content.AsMessage().Body, " ", -2))
app.Log.Info().Msgf("Command: %s", evt.Content.AsMessage().Body)
switch evt.Content.AsMessage().Body {
case "!xd":
app.SendText(evt, "xd !")
return
}
}

9
cmd/nourybot/events.go Normal file
View file

@ -0,0 +1,9 @@
package main
import "maunium.net/go/mautrix/event"
func (app *Application) ParseEvent(evt *event.Event) {
// TODO:
// Log the events or whatever, I don't even know what events there all are rn.
app.Log.Info().Msgf("Event: %s", evt.Content.AsMessage().Body)
}

View file

@ -32,6 +32,11 @@ var debug = flag.Bool("debug", false, "Enable debug logs")
//var database = flag.String("database", "test.db", "SQLite database path")
type Application struct {
MatrixClient *mautrix.Client
Log zerolog.Logger
}
func main() {
flag.Parse()
err := godotenv.Load()
@ -48,6 +53,7 @@ func main() {
if err != nil {
panic(err)
}
rl, err := readline.New("[no room]> ")
if err != nil {
panic(err)
@ -62,22 +68,34 @@ func main() {
}
client.Log = log
app := &Application{
MatrixClient: client,
Log: log,
}
var lastRoomID id.RoomID
syncer := client.Syncer.(*mautrix.DefaultSyncer)
syncer := app.MatrixClient.Syncer.(*mautrix.DefaultSyncer)
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
lastRoomID = evt.RoomID
rl.SetPrompt(fmt.Sprintf("%s> ", lastRoomID))
log.Info().
if evt.Content.AsMessage().Body[:1] == "!" {
app.Log.Info().
Str("sender", evt.Sender.String()).
Str("type", evt.Type.String()).
Str("id", evt.ID.String()).
Str("body", evt.Content.AsMessage().Body).
Msg("Received message")
Msg("Received xdddddddddddddddddddddddd message")
app.ParseCommand(evt)
return
} else {
app.ParseEvent(evt)
return
}
})
syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) {
if evt.GetStateKey() == client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
_, err := client.JoinRoomByID(context.TODO(), evt.RoomID)
if evt.GetStateKey() == app.MatrixClient.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
_, err := app.MatrixClient.JoinRoomByID(context.TODO(), evt.RoomID)
if err == nil {
lastRoomID = evt.RoomID
rl.SetPrompt(fmt.Sprintf("%s> ", lastRoomID))
@ -92,9 +110,10 @@ func main() {
Msg("Failed to join room after invite")
}
}
})
cryptoHelper, err := cryptohelper.NewCryptoHelper(client, []byte("meow"), database)
cryptoHelper, err := cryptohelper.NewCryptoHelper(app.MatrixClient, []byte("meow"), database)
if err != nil {
panic(err)
}
@ -116,7 +135,7 @@ func main() {
panic(err)
}
// Set the client crypto helper in order to automatically encrypt outgoing messages
client.Crypto = cryptoHelper
app.MatrixClient.Crypto = cryptoHelper
log.Info().Msg("Now running")
syncCtx, cancelSync := context.WithCancel(context.Background())
@ -124,7 +143,7 @@ func main() {
syncStopWait.Add(1)
go func() {
err = client.SyncWithContext(syncCtx)
err = app.MatrixClient.SyncWithContext(syncCtx)
defer syncStopWait.Done()
if err != nil && !errors.Is(err, context.Canceled) {
panic(err)
@ -140,7 +159,7 @@ func main() {
log.Error().Msg("Wait for an incoming message before sending messages")
continue
}
resp, err := client.SendText(context.TODO(), lastRoomID, line)
resp, err := app.MatrixClient.SendText(context.TODO(), lastRoomID, line)
if err != nil {
log.Error().Err(err).Msg("Failed to send event")
} else {
@ -154,3 +173,14 @@ func main() {
log.Error().Err(err).Msg("Error closing database")
}
}
func (app Application) SendText(evt *event.Event, message string) {
room := evt.RoomID
resp, err := app.MatrixClient.SendText(context.TODO(), room, message)
if err != nil {
app.Log.Error().Err(err).Msg("Failed to send event")
} else {
app.Log.Info().Str("event_id", resp.EventID.String()).Msg("Event sent")
}
}