2021-08-19 13:33:56 +00:00
|
|
|
package respond
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-28 23:56:06 +00:00
|
|
|
"encoding/json"
|
2021-09-03 10:35:15 +00:00
|
|
|
"fmt"
|
2023-05-28 23:56:06 +00:00
|
|
|
"net/http"
|
2021-09-03 10:35:15 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
"github.com/go-irc/irc"
|
2023-05-28 23:56:06 +00:00
|
|
|
"github.com/gorilla/mux"
|
2020-12-21 00:32:39 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-10-21 22:02:18 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2020-12-21 00:32:39 +00:00
|
|
|
)
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
const actorName = "respond"
|
|
|
|
|
|
|
|
var (
|
|
|
|
formatMessage plugins.MsgFormatter
|
2022-10-25 16:47:30 +00:00
|
|
|
send plugins.SendMessageFunc
|
2021-09-22 13:36:45 +00:00
|
|
|
|
2022-10-31 16:26:53 +00:00
|
|
|
ptrBoolFalse = func(v bool) *bool { return &v }(false)
|
|
|
|
ptrStringEmpty = func(s string) *string { return &s }("")
|
2021-09-22 13:36:45 +00:00
|
|
|
)
|
2021-08-19 13:33:56 +00:00
|
|
|
|
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
formatMessage = args.FormatMessage
|
2022-10-25 16:47:30 +00:00
|
|
|
send = args.SendMessage
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Respond to message with a new message",
|
|
|
|
Name: "Respond to Message",
|
|
|
|
Type: "respond",
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Message text to send",
|
|
|
|
Key: "message",
|
|
|
|
Long: true,
|
|
|
|
Name: "Message",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Fallback message text to send if message cannot be generated",
|
|
|
|
Key: "fallback",
|
|
|
|
Name: "Fallback",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "false",
|
|
|
|
Description: "Send message as a native Twitch-reply to the original message",
|
|
|
|
Key: "as_reply",
|
|
|
|
Name: "As Reply",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeBool,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Send message to a different channel than the original message",
|
|
|
|
Key: "to_channel",
|
|
|
|
Name: "To Channel",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2023-05-28 23:56:06 +00:00
|
|
|
args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
|
|
|
|
Description: "Send a message on behalf of the bot (send JSON object with `message` key)",
|
|
|
|
HandlerFunc: handleAPISend,
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Module: actorName,
|
|
|
|
Name: "Send message",
|
|
|
|
Path: "/{channel}",
|
|
|
|
RequiresWriteAuth: true,
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
|
|
|
|
RouteParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Channel to send the message to",
|
|
|
|
Name: "channel",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
return nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
type actor struct{}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2023-03-24 21:32:00 +00:00
|
|
|
func (a actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.FieldCollection) (preventCooldown bool, err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
msg, err := formatMessage(attrs.MustString("message", nil), m, r, eventData)
|
2021-06-11 11:52:42 +00:00
|
|
|
if err != nil {
|
2021-10-21 21:57:06 +00:00
|
|
|
if !attrs.CanString("fallback") || attrs.MustString("fallback", nil) == "" {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing response")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-10-21 22:02:18 +00:00
|
|
|
log.WithError(err).Error("Response message processing caused error, trying fallback")
|
2021-09-22 13:36:45 +00:00
|
|
|
if msg, err = formatMessage(attrs.MustString("fallback", nil), m, r, eventData); err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing response fallback")
|
2021-06-02 13:31:16 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-06-02 13:31:16 +00:00
|
|
|
|
2021-09-03 10:35:15 +00:00
|
|
|
toChannel := plugins.DeriveChannel(m, eventData)
|
2021-09-22 13:36:45 +00:00
|
|
|
if attrs.CanString("to_channel") && attrs.MustString("to_channel", nil) != "" {
|
|
|
|
toChannel = fmt.Sprintf("#%s", strings.TrimLeft(attrs.MustString("to_channel", nil), "#"))
|
2021-09-03 10:35:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
ircMessage := &irc.Message{
|
|
|
|
Command: "PRIVMSG",
|
|
|
|
Params: []string{
|
2021-09-03 10:35:15 +00:00
|
|
|
toChannel,
|
2021-06-11 11:52:42 +00:00
|
|
|
msg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
if attrs.MustBool("as_reply", ptrBoolFalse) {
|
2021-06-11 11:52:42 +00:00
|
|
|
id, ok := m.GetTag("id")
|
|
|
|
if ok {
|
|
|
|
if ircMessage.Tags == nil {
|
|
|
|
ircMessage.Tags = make(irc.Tags)
|
2021-06-02 13:31:16 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
ircMessage.Tags["reply-parent-msg-id"] = irc.TagValue(id)
|
2021-06-02 13:31:16 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-06-02 13:31:16 +00:00
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2022-10-25 16:47:30 +00:00
|
|
|
send(ircMessage),
|
2021-06-11 11:52:42 +00:00
|
|
|
"sending response",
|
|
|
|
)
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (a actor) IsAsync() bool { return false }
|
2021-09-22 13:36:45 +00:00
|
|
|
func (a actor) Name() string { return actorName }
|
|
|
|
|
2022-10-31 16:26:53 +00:00
|
|
|
func (a actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *plugins.FieldCollection) (err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
if v, err := attrs.String("message"); err != nil || v == "" {
|
|
|
|
return errors.New("message must be non-empty string")
|
|
|
|
}
|
|
|
|
|
2022-10-31 16:26:53 +00:00
|
|
|
for _, field := range []string{"message", "fallback"} {
|
|
|
|
if err = tplValidator(attrs.MustString(field, ptrStringEmpty)); err != nil {
|
|
|
|
return errors.Wrapf(err, "validating %s template", field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-05-28 23:56:06 +00:00
|
|
|
|
|
|
|
func handleAPISend(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var payload struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "parsing payload").Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(payload.Message) == "" {
|
|
|
|
http.Error(w, errors.New("no message found").Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := send(&irc.Message{
|
|
|
|
Command: "PRIVMSG",
|
|
|
|
Params: []string{
|
|
|
|
"#" + strings.TrimLeft(mux.Vars(r)["channel"], "#"),
|
|
|
|
strings.TrimSpace(payload.Message),
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "sending message").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
}
|