mirror of
https://github.com/lyx0/nourybot-matrix.git
synced 2024-11-13 19:49:54 +01:00
implement basic command handling and xkcd command
This commit is contained in:
parent
635bdceb87
commit
580639433b
9 changed files with 178 additions and 23 deletions
|
@ -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
39
cmd/nourybot/commands.go
Normal 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
14
cmd/nourybot/common.go
Normal 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
11
cmd/nourybot/events.go
Normal 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)
|
||||||
|
}
|
|
@ -78,6 +78,9 @@ 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))
|
||||||
|
|
||||||
|
// Filter out messages from the bot user account.
|
||||||
|
if evt.Sender.String() != fmt.Sprintf("@%s:%s", cfg.matrixUser, cfg.matrixHomeserver) {
|
||||||
app.Log.Info().
|
app.Log.Info().
|
||||||
Str("sender", evt.Sender.String()).
|
Str("sender", evt.Sender.String()).
|
||||||
Str("type", evt.Type.String()).
|
Str("type", evt.Type.String()).
|
||||||
|
@ -85,7 +88,14 @@ func main() {
|
||||||
Str("body", evt.Content.AsMessage().Body).
|
Str("body", evt.Content.AsMessage().Body).
|
||||||
Msg("Received message")
|
Msg("Received message")
|
||||||
|
|
||||||
|
if evt.Content.AsMessage().Body[:1] == "!" {
|
||||||
|
app.ParseCommand(evt)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
app.ParseEvent(evt)
|
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 {
|
||||||
|
|
1
internal/commands/commands.go
Normal file
1
internal/commands/commands.go
Normal file
|
@ -0,0 +1 @@
|
||||||
|
package commands
|
6
internal/commands/ping.go
Normal file
6
internal/commands/ping.go
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package commands
|
||||||
|
|
||||||
|
func Ping() (string, error) {
|
||||||
|
resp := "Pong!"
|
||||||
|
return resp, nil
|
||||||
|
}
|
55
internal/commands/xkcd.go
Normal file
55
internal/commands/xkcd.go
Normal 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
35
internal/common/random.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in a new issue