mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 08:40:01 +00:00
[customevent] Add API module to create custom events
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
cbf50ff928
commit
a73f27abd0
4 changed files with 54 additions and 1 deletions
2
go.mod
2
go.mod
|
@ -1,6 +1,6 @@
|
|||
module github.com/Luzifer/twitch-bot
|
||||
|
||||
go 1.17
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/Luzifer/go-openssl/v4 v4.1.0
|
||||
|
|
45
internal/apimodules/customevent/customevent.go
Normal file
45
internal/apimodules/customevent/customevent.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package customevent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/Luzifer/twitch-bot/plugins"
|
||||
)
|
||||
|
||||
var eventCreatorFunc plugins.EventHandlerFunc
|
||||
|
||||
func Register(args plugins.RegistrationArguments) error {
|
||||
eventCreatorFunc = args.CreateEvent
|
||||
|
||||
args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
|
||||
Description: "Creates an `custom` event containing the fields provided in the request body",
|
||||
HandlerFunc: handleCreateEvent,
|
||||
Method: http.MethodPost,
|
||||
Module: "customevent",
|
||||
Name: "Create custom event",
|
||||
Path: "/create",
|
||||
RequiresWriteAuth: true,
|
||||
ResponseType: plugins.HTTPRouteResponseTypeNo200,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleCreateEvent(w http.ResponseWriter, r *http.Request) {
|
||||
payload := make(map[string]any)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||
http.Error(w, errors.Wrap(err, "parsing event payload").Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := eventCreatorFunc("custom", plugins.FieldCollectionFromData(payload)); err != nil {
|
||||
http.Error(w, errors.Wrap(err, "creating event").Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
|
@ -47,6 +47,8 @@ type (
|
|||
RegisterFunc func(RegistrationArguments) error
|
||||
|
||||
RegistrationArguments struct {
|
||||
// CreateEvent allows to create an event handed out to all modules to handle
|
||||
CreateEvent EventHandlerFunc
|
||||
// FormatMessage is a method to convert templates into strings using internally known variables / configs
|
||||
FormatMessage MsgFormatter
|
||||
// GetLogger returns a sirupsen log.Entry pre-configured with the module name
|
||||
|
|
|
@ -22,6 +22,7 @@ import (
|
|||
"github.com/Luzifer/twitch-bot/internal/actors/respond"
|
||||
"github.com/Luzifer/twitch-bot/internal/actors/timeout"
|
||||
"github.com/Luzifer/twitch-bot/internal/actors/whisper"
|
||||
"github.com/Luzifer/twitch-bot/internal/apimodules/customevent"
|
||||
"github.com/Luzifer/twitch-bot/internal/apimodules/msgformat"
|
||||
"github.com/Luzifer/twitch-bot/internal/apimodules/overlays"
|
||||
"github.com/Luzifer/twitch-bot/internal/template/numeric"
|
||||
|
@ -53,6 +54,7 @@ var (
|
|||
random.Register,
|
||||
|
||||
// API-only modules
|
||||
customevent.Register,
|
||||
msgformat.Register,
|
||||
overlays.Register,
|
||||
}
|
||||
|
@ -104,6 +106,10 @@ func registerRoute(route plugins.HTTPRouteRegistrationArgs) error {
|
|||
|
||||
func getRegistrationArguments() plugins.RegistrationArguments {
|
||||
return plugins.RegistrationArguments{
|
||||
CreateEvent: func(evt string, eventData *plugins.FieldCollection) error {
|
||||
handleMessage(ircHdl.Client(), nil, &evt, eventData)
|
||||
return nil
|
||||
},
|
||||
FormatMessage: formatMessage,
|
||||
GetLogger: func(moduleName string) *log.Entry { return log.WithField("module", moduleName) },
|
||||
GetStorageManager: func() plugins.StorageManager { return store },
|
||||
|
|
Loading…
Reference in a new issue