diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index 0c60250..f473b0e 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -9,6 +9,7 @@ import ( "html/template" "math/rand" "net/http" + "net/url" "regexp" "strings" "time" @@ -16,6 +17,7 @@ import ( "github.com/rs/zerolog/log" "github.com/jmoiron/sqlx" + "github.com/velour/catbase/bot" "github.com/velour/catbase/bot/msg" ) @@ -419,6 +421,8 @@ func (p *FactoidPlugin) sayFact(c bot.Connector, message msg.Message, fact Facto p.Bot.Send(c, bot.Reaction, message.Channel, msg, message) } else if fact.Verb == "reply" { p.Bot.Send(c, bot.Message, message.Channel, msg) + } else if fact.Verb == "image" { + p.sendImage(c, message, msg) } else { p.Bot.Send(c, bot.Message, message.Channel, full) } @@ -437,6 +441,34 @@ func (p *FactoidPlugin) sayFact(c bot.Connector, message msg.Message, fact Facto p.LastFact = &fact } +func (p *FactoidPlugin) sendImage(c bot.Connector, message msg.Message, msg string) { + imgSrc := "" + txt := "" + for _, w := range strings.Split(msg, " ") { + if _, err := url.Parse(w); err == nil && strings.HasPrefix(w, "http") { + log.Debug().Msgf("Valid image found: %s", w) + imgSrc = w + } else { + txt = txt + " " + w + log.Debug().Msgf("Adding %s to txt %s", w, txt) + } + } + log.Debug(). + Str("imgSrc", imgSrc). + Str("txt", txt). + Str("msg", msg). + Msg("Sending image attachment") + if imgSrc != "" { + img := bot.ImageAttachment{ + URL: imgSrc, + AltTxt: txt, + } + p.Bot.Send(c, bot.Message, message.Channel, "", img) + return + } + p.Bot.Send(c, bot.Message, message.Channel, txt) +} + // trigger checks the message for its fitness to be a factoid and then hauls // the message off to sayFact for processing if it is in fact a trigger func (p *FactoidPlugin) trigger(c bot.Connector, message msg.Message) bool {