mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 08:40:01 +00:00
[msgformat] Add module to retrieve filled template through API
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
900e478e00
commit
fe76b23b09
2 changed files with 55 additions and 0 deletions
51
internal/apimodules/msgformat/msgformat.go
Normal file
51
internal/apimodules/msgformat/msgformat.go
Normal 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)
|
||||
}
|
|
@ -21,6 +21,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/msgformat"
|
||||
"github.com/Luzifer/twitch-bot/internal/template/numeric"
|
||||
"github.com/Luzifer/twitch-bot/internal/template/random"
|
||||
"github.com/Luzifer/twitch-bot/plugins"
|
||||
|
@ -47,6 +48,9 @@ var (
|
|||
// Template functions
|
||||
numeric.Register,
|
||||
random.Register,
|
||||
|
||||
// API-only modules
|
||||
msgformat.Register,
|
||||
}
|
||||
knownModules []string
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue