diff --git a/go.mod b/go.mod index 4ab0109..ee73cb7 100644 --- a/go.mod +++ b/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 diff --git a/internal/apimodules/customevent/customevent.go b/internal/apimodules/customevent/customevent.go new file mode 100644 index 0000000..e0b67b7 --- /dev/null +++ b/internal/apimodules/customevent/customevent.go @@ -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) +} diff --git a/plugins/interface.go b/plugins/interface.go index 7583066..40e2917 100644 --- a/plugins/interface.go +++ b/plugins/interface.go @@ -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 diff --git a/plugins_core.go b/plugins_core.go index d4103f4..7e6c87d 100644 --- a/plugins_core.go +++ b/plugins_core.go @@ -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 },