mirror of
https://github.com/lyx0/nourybot-matrix.git
synced 2024-11-13 19:49:54 +01:00
respond to ping message
This commit is contained in:
parent
fcef2c479f
commit
9d25b1569c
3 changed files with 83 additions and 13 deletions
31
cmd/nourybot/commands.go
Normal file
31
cmd/nourybot/commands.go
Normal 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
9
cmd/nourybot/events.go
Normal 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)
|
||||||
|
}
|
|
@ -32,6 +32,11 @@ var debug = flag.Bool("debug", false, "Enable debug logs")
|
||||||
|
|
||||||
//var database = flag.String("database", "test.db", "SQLite database path")
|
//var database = flag.String("database", "test.db", "SQLite database path")
|
||||||
|
|
||||||
|
type Application struct {
|
||||||
|
MatrixClient *mautrix.Client
|
||||||
|
Log zerolog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
|
@ -48,6 +53,7 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rl, err := readline.New("[no room]> ")
|
rl, err := readline.New("[no room]> ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -62,22 +68,34 @@ func main() {
|
||||||
}
|
}
|
||||||
client.Log = log
|
client.Log = log
|
||||||
|
|
||||||
|
app := &Application{
|
||||||
|
MatrixClient: client,
|
||||||
|
Log: log,
|
||||||
|
}
|
||||||
var lastRoomID id.RoomID
|
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) {
|
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
||||||
lastRoomID = evt.RoomID
|
lastRoomID = evt.RoomID
|
||||||
rl.SetPrompt(fmt.Sprintf("%s> ", lastRoomID))
|
rl.SetPrompt(fmt.Sprintf("%s> ", lastRoomID))
|
||||||
log.Info().
|
if evt.Content.AsMessage().Body[:1] == "!" {
|
||||||
|
app.Log.Info().
|
||||||
Str("sender", evt.Sender.String()).
|
Str("sender", evt.Sender.String()).
|
||||||
Str("type", evt.Type.String()).
|
Str("type", evt.Type.String()).
|
||||||
Str("id", evt.ID.String()).
|
Str("id", evt.ID.String()).
|
||||||
Str("body", evt.Content.AsMessage().Body).
|
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) {
|
syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) {
|
||||||
if evt.GetStateKey() == client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
|
if evt.GetStateKey() == app.MatrixClient.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
|
||||||
_, err := client.JoinRoomByID(context.TODO(), evt.RoomID)
|
_, err := app.MatrixClient.JoinRoomByID(context.TODO(), evt.RoomID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
lastRoomID = evt.RoomID
|
lastRoomID = evt.RoomID
|
||||||
rl.SetPrompt(fmt.Sprintf("%s> ", lastRoomID))
|
rl.SetPrompt(fmt.Sprintf("%s> ", lastRoomID))
|
||||||
|
@ -92,9 +110,10 @@ func main() {
|
||||||
Msg("Failed to join room after invite")
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -116,7 +135,7 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
// Set the client crypto helper in order to automatically encrypt outgoing messages
|
// Set the client crypto helper in order to automatically encrypt outgoing messages
|
||||||
client.Crypto = cryptoHelper
|
app.MatrixClient.Crypto = cryptoHelper
|
||||||
|
|
||||||
log.Info().Msg("Now running")
|
log.Info().Msg("Now running")
|
||||||
syncCtx, cancelSync := context.WithCancel(context.Background())
|
syncCtx, cancelSync := context.WithCancel(context.Background())
|
||||||
|
@ -124,7 +143,7 @@ func main() {
|
||||||
syncStopWait.Add(1)
|
syncStopWait.Add(1)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err = client.SyncWithContext(syncCtx)
|
err = app.MatrixClient.SyncWithContext(syncCtx)
|
||||||
defer syncStopWait.Done()
|
defer syncStopWait.Done()
|
||||||
if err != nil && !errors.Is(err, context.Canceled) {
|
if err != nil && !errors.Is(err, context.Canceled) {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -140,7 +159,7 @@ func main() {
|
||||||
log.Error().Msg("Wait for an incoming message before sending messages")
|
log.Error().Msg("Wait for an incoming message before sending messages")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
resp, err := client.SendText(context.TODO(), lastRoomID, line)
|
resp, err := app.MatrixClient.SendText(context.TODO(), lastRoomID, line)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Failed to send event")
|
log.Error().Err(err).Msg("Failed to send event")
|
||||||
} else {
|
} else {
|
||||||
|
@ -154,3 +173,14 @@ func main() {
|
||||||
log.Error().Err(err).Msg("Error closing database")
|
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")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue