implement basic command handling and xkcd command

This commit is contained in:
lyx0 2023-05-06 21:37:21 +02:00
parent 635bdceb87
commit 580639433b
9 changed files with 178 additions and 23 deletions

View file

@ -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")
}
}
}

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

@ -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
}
}
}

14
cmd/nourybot/common.go Normal file
View file

@ -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")
}
}

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

@ -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)
}

View file

@ -78,14 +78,24 @@ func main() {
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))
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) { syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) {
if evt.GetStateKey() == app.Mc.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite { if evt.GetStateKey() == app.Mc.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {

View file

@ -0,0 +1 @@
package commands

View file

@ -0,0 +1,6 @@
package commands
func Ping() (string, error) {
resp := "Pong!"
return resp, nil
}

55
internal/commands/xkcd.go Normal file
View file

@ -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
}

35
internal/common/random.go Normal file
View file

@ -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)
}