2022-01-20 01:03:52 +00:00
|
|
|
package msgformat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2022-10-23 13:06:45 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v2/plugins"
|
2022-01-20 01:03:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var formatMessage plugins.MsgFormatter
|
|
|
|
|
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
|
|
|
|
args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
|
2022-03-30 22:27:52 +00:00
|
|
|
Description: "Takes the given template and renders it using the same renderer as messages in the channel are",
|
2022-01-20 01:03:52 +00:00
|
|
|
HandlerFunc: handleFormattedMessage,
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Module: "msgformat",
|
2022-03-30 22:27:52 +00:00
|
|
|
Name: "Format Message",
|
2022-01-20 01:03:52 +00:00
|
|
|
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)
|
|
|
|
}
|