[msgformat] Add module to retrieve filled template through API

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-01-20 02:03:52 +01:00
parent 900e478e00
commit fe76b23b09
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D
2 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,51 @@
package msgformat
import (
"fmt"
"net/http"
"github.com/pkg/errors"
"github.com/Luzifer/twitch-bot/plugins"
)
var formatMessage plugins.MsgFormatter
func Register(args plugins.RegistrationArguments) error {
formatMessage = args.FormatMessage
args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
HandlerFunc: handleFormattedMessage,
Method: http.MethodGet,
Module: "msgformat",
Path: "/format",
QueryParams: []plugins.HTTPRouteParamDocumentation{
{
Description: "The template to execute",
Name: "template",
Required: true,
Type: "string",
},
},
RequiresWriteAuth: true, // This module can potentially be used to harvest data / read internal variables so it is handled as a write-module
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
})
return nil
}
func handleFormattedMessage(w http.ResponseWriter, r *http.Request) {
tpl := r.FormValue("template")
if tpl == "" {
w.WriteHeader(http.StatusNoContent)
return
}
msg, err := formatMessage(tpl, nil, nil, nil)
if err != nil {
http.Error(w, errors.Wrap(err, "executing template").Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, msg)
}

View File

@ -21,6 +21,7 @@ import (
"github.com/Luzifer/twitch-bot/internal/actors/respond" "github.com/Luzifer/twitch-bot/internal/actors/respond"
"github.com/Luzifer/twitch-bot/internal/actors/timeout" "github.com/Luzifer/twitch-bot/internal/actors/timeout"
"github.com/Luzifer/twitch-bot/internal/actors/whisper" "github.com/Luzifer/twitch-bot/internal/actors/whisper"
"github.com/Luzifer/twitch-bot/internal/apimodules/msgformat"
"github.com/Luzifer/twitch-bot/internal/template/numeric" "github.com/Luzifer/twitch-bot/internal/template/numeric"
"github.com/Luzifer/twitch-bot/internal/template/random" "github.com/Luzifer/twitch-bot/internal/template/random"
"github.com/Luzifer/twitch-bot/plugins" "github.com/Luzifer/twitch-bot/plugins"
@ -47,6 +48,9 @@ var (
// Template functions // Template functions
numeric.Register, numeric.Register,
random.Register, random.Register,
// API-only modules
msgformat.Register,
} }
knownModules []string knownModules []string
) )