fact: add image verb

This will attach the last URL it sees as an image attachment. It will
fail if that is not a valid image (IDK, slack will get mad) and if no
image is found, it will send as text. The text in the message that is
not a URL will be sent as the image title.
This commit is contained in:
Chris Sexton 2019-12-30 10:26:17 -05:00 committed by Chris Sexton
parent 32d1ebaa95
commit 595492a16b
1 changed files with 32 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"html/template" "html/template"
"math/rand" "math/rand"
"net/http" "net/http"
"net/url"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -16,6 +17,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg" "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) p.Bot.Send(c, bot.Reaction, message.Channel, msg, message)
} else if fact.Verb == "reply" { } else if fact.Verb == "reply" {
p.Bot.Send(c, bot.Message, message.Channel, msg) p.Bot.Send(c, bot.Message, message.Channel, msg)
} else if fact.Verb == "image" {
p.sendImage(c, message, msg)
} else { } else {
p.Bot.Send(c, bot.Message, message.Channel, full) 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 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 // 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 // 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 { func (p *FactoidPlugin) trigger(c bot.Connector, message msg.Message) bool {