From 580639433b4424f2c909d80af9262bf02a94a305 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sat, 6 May 2023 21:37:21 +0200 Subject: [PATCH] implement basic command handling and xkcd command --- cmd/nourybot/command.go | 16 ---------- cmd/nourybot/commands.go | 39 +++++++++++++++++++++++++ cmd/nourybot/common.go | 14 +++++++++ cmd/nourybot/events.go | 11 +++++++ cmd/nourybot/main.go | 24 ++++++++++----- internal/commands/commands.go | 1 + internal/commands/ping.go | 6 ++++ internal/commands/xkcd.go | 55 +++++++++++++++++++++++++++++++++++ internal/common/random.go | 35 ++++++++++++++++++++++ 9 files changed, 178 insertions(+), 23 deletions(-) delete mode 100644 cmd/nourybot/command.go create mode 100644 cmd/nourybot/commands.go create mode 100644 cmd/nourybot/common.go create mode 100644 cmd/nourybot/events.go create mode 100644 internal/commands/commands.go create mode 100644 internal/commands/ping.go create mode 100644 internal/commands/xkcd.go create mode 100644 internal/common/random.go diff --git a/cmd/nourybot/command.go b/cmd/nourybot/command.go deleted file mode 100644 index 24103e8..0000000 --- a/cmd/nourybot/command.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import ( - "maunium.net/go/mautrix/event" -) - -func (app *Application) ParseEvent(evt *event.Event) { - if evt.Content.AsMessage().Body == "xD" { - resp, err := app.Mc.SendText(evt.RoomID, "hehe") - 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") - } - } -} diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go new file mode 100644 index 0000000..336f0cd --- /dev/null +++ b/cmd/nourybot/commands.go @@ -0,0 +1,39 @@ +package main + +import ( + "strings" + + "github.com/lyx0/nourybot-matrix/internal/commands" + "maunium.net/go/mautrix/event" +) + +func (app *Application) ParseCommand(evt *event.Event) { + commandName := strings.ToLower(strings.SplitN(evt.Content.AsMessage().Body, " ", 2)[0][1:]) + app.Log.Info().Msgf("Command: %s", commandName) + + switch commandName { + case "xd": + app.SendText(evt, "XD !") + return + + case "ping": + if resp, err := commands.Ping(); err != nil { + app.Log.Error().Err(err).Msg("Failed to send Ping") + app.SendText(evt, "Something went wrong.") + return + } else { + app.SendText(evt, resp) + return + } + + case "xkcd": + if resp, err := commands.Xkcd(); err != nil { + app.Log.Error().Err(err).Msg("Failed to send Ping") + app.SendText(evt, "Something went wrong.") + return + } else { + app.SendText(evt, resp) + return + } + } +} diff --git a/cmd/nourybot/common.go b/cmd/nourybot/common.go new file mode 100644 index 0000000..74189f4 --- /dev/null +++ b/cmd/nourybot/common.go @@ -0,0 +1,14 @@ +package main + +import "maunium.net/go/mautrix/event" + +func (app *Application) SendText(evt *event.Event, message string) { + room := evt.RoomID + + resp, err := app.Mc.SendText(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") + } +} diff --git a/cmd/nourybot/events.go b/cmd/nourybot/events.go new file mode 100644 index 0000000..aa4916c --- /dev/null +++ b/cmd/nourybot/events.go @@ -0,0 +1,11 @@ +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) +} diff --git a/cmd/nourybot/main.go b/cmd/nourybot/main.go index 281e04d..1d1b8a5 100644 --- a/cmd/nourybot/main.go +++ b/cmd/nourybot/main.go @@ -78,14 +78,24 @@ func main() { syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) { lastRoomID = evt.RoomID rl.SetPrompt(fmt.Sprintf("%s> ", lastRoomID)) - 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") - app.ParseEvent(evt) + // Filter out messages from the bot user account. + if evt.Sender.String() != fmt.Sprintf("@%s:%s", cfg.matrixUser, cfg.matrixHomeserver) { + 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") + + if evt.Content.AsMessage().Body[:1] == "!" { + app.ParseCommand(evt) + return + } else { + app.ParseEvent(evt) + return + } + } }) syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) { if evt.GetStateKey() == app.Mc.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite { diff --git a/internal/commands/commands.go b/internal/commands/commands.go new file mode 100644 index 0000000..cdff10d --- /dev/null +++ b/internal/commands/commands.go @@ -0,0 +1 @@ +package commands diff --git a/internal/commands/ping.go b/internal/commands/ping.go new file mode 100644 index 0000000..519a901 --- /dev/null +++ b/internal/commands/ping.go @@ -0,0 +1,6 @@ +package commands + +func Ping() (string, error) { + resp := "Pong!" + return resp, nil +} diff --git a/internal/commands/xkcd.go b/internal/commands/xkcd.go new file mode 100644 index 0000000..6fefcb7 --- /dev/null +++ b/internal/commands/xkcd.go @@ -0,0 +1,55 @@ +package commands + +import ( + "encoding/json" + "fmt" + "github.com/lyx0/nourybot-matrix/internal/common" + "io" + "net/http" +) + +type xkcdResponse struct { + Num int `json:"num"` + SafeTitle string `json:"safe_title"` + Img string `json:"img"` +} + +func Xkcd() (string, error) { + response, err := http.Get("https://xkcd.com/info.0.json") + if err != nil { + return "", err + } + responseData, err := io.ReadAll(response.Body) + if err != nil { + return "", err + } + var responseObject xkcdResponse + if err = json.Unmarshal(responseData, &responseObject); err != nil { + return "", err + } + + reply := fmt.Sprint("Current Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img) + + return reply, nil +} + +func RandomXkcd() (string, error) { + comicNum := fmt.Sprint(common.GenerateRandomNumber(2772)) + + response, err := http.Get(fmt.Sprint("http://xkcd.com/" + comicNum + "/info.0.json")) + if err != nil { + return "", err + } + responseData, err := io.ReadAll(response.Body) + if err != nil { + return "", err + } + var responseObject xkcdResponse + if err = json.Unmarshal(responseData, &responseObject); err != nil { + return "", err + } + + reply := fmt.Sprint("Random Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img) + + return reply, nil +} diff --git a/internal/common/random.go b/internal/common/random.go new file mode 100644 index 0000000..c56b1ca --- /dev/null +++ b/internal/common/random.go @@ -0,0 +1,35 @@ +package common + +import ( + "fmt" + "math/rand" + "strconv" +) + +// StrGenerateRandomNumber generates a random number from +// a given max value as a string +func StrGenerateRandomNumber(max string) int { + num, err := strconv.Atoi(max) + if num < 1 { + return 0 + } + + if err != nil { + fmt.Printf("Supplied value %v is not a number", num) + return 0 + } else { + return rand.Intn(num) + } +} + +// GenerateRandomNumber returns a random number from +// a given max value as a int +func GenerateRandomNumber(max int) int { + return rand.Intn(max) +} + +// GenerateRandomNumberRange returns a random number +// over a given minimum and maximum range. +func GenerateRandomNumberRange(min int, max int) int { + return (rand.Intn(max-min) + min) +}