slack: add image support

* Make untappd checkins embed images
* Added attachment types as an optional send arg
This commit is contained in:
Chris Sexton 2019-03-09 22:40:03 -05:00
parent 47516a82fa
commit f267ae07e3
4 changed files with 73 additions and 37 deletions

View File

@ -30,6 +30,11 @@ const (
SelfMessage
)
type ImageAttachment struct {
URL string
AltTxt string
}
type Kind int
type Callback func(Kind, msg.Message, ...interface{}) bool
type CallbackMap map[string]map[Kind][]Callback

View File

@ -60,9 +60,9 @@ func (i *Irc) Send(kind bot.Kind, args ...interface{}) (string, error) {
switch kind {
case bot.Reply:
case bot.Message:
return i.sendMessage(args[0].(string), args[1].(string))
return i.sendMessage(args[0].(string), args[1].(string), args...)
case bot.Action:
return i.sendAction(args[0].(string), args[1].(string))
return i.sendAction(args[0].(string), args[1].(string), args...)
default:
}
return "", nil
@ -73,7 +73,7 @@ func (i *Irc) JoinChannel(channel string) {
i.Client.Out <- irc.Msg{Cmd: irc.JOIN, Args: []string{channel}}
}
func (i *Irc) sendMessage(channel, message string) (string, error) {
func (i *Irc) sendMessage(channel, message string, args ...interface{}) (string, error) {
for len(message) > 0 {
m := irc.Msg{
Cmd: "PRIVMSG",
@ -96,15 +96,32 @@ func (i *Irc) sendMessage(channel, message string) (string, error) {
<-throttle
i.Client.Out <- m
if len(args) > 0 {
for _, a := range args {
switch a := a.(type) {
case bot.ImageAttachment:
m = irc.Msg{
Cmd: "PRIVMSG",
Args: []string{channel, fmt.Sprintf("%s: %s",
a.AltTxt, a.URL)},
}
<-throttle
i.Client.Out <- m
}
}
}
}
return "NO_IRC_IDENTIFIERS", nil
}
// Sends action to channel
func (i *Irc) sendAction(channel, message string) (string, error) {
func (i *Irc) sendAction(channel, message string, args ...interface{}) (string, error) {
message = actionPrefix + " " + message + "\x01"
return i.sendMessage(channel, message)
return i.sendMessage(channel, message, args...)
}
func (i *Irc) GetEmojiList() map[string]string {

View File

@ -171,9 +171,9 @@ func (s *SlackApp) msgReceivd(msg *slackevents.MessageEvent) {
func (s *SlackApp) Send(kind bot.Kind, args ...interface{}) (string, error) {
switch kind {
case bot.Message:
return s.sendMessage(args[0].(string), args[1].(string))
return s.sendMessage(args[0].(string), args[1].(string), false, args...)
case bot.Action:
return s.sendAction(args[0].(string), args[1].(string))
return s.sendMessage(args[0].(string), args[1].(string), true, args...)
case bot.Edit:
return s.edit(args[0].(string), args[1].(string), args[2].(string))
case bot.Reply:
@ -192,20 +192,45 @@ func (s *SlackApp) Send(kind bot.Kind, args ...interface{}) (string, error) {
return "", fmt.Errorf("No handler for message type %d", kind)
}
func (s *SlackApp) sendMessageType(channel, message string, meMessage bool) (string, error) {
func (s *SlackApp) sendMessage(channel, message string, meMessage bool, args ...interface{}) (string, error) {
ts, err := "", fmt.Errorf("")
nick := s.config.Get("Nick", "bot")
if meMessage {
_, ts, err = s.api.PostMessage(channel,
options := []slack.MsgOption{
slack.MsgOptionUsername(nick),
slack.MsgOptionText(message, false),
slack.MsgOptionMeMessage())
} else {
_, ts, err = s.api.PostMessage(channel,
slack.MsgOptionUsername(nick),
slack.MsgOptionText(message, false))
}
if meMessage {
options = append(options, slack.MsgOptionMeMessage())
}
// Check for message attachments
attachments := []slack.Attachment{}
if len(args) > 0 {
for _, a := range args {
switch a := a.(type) {
case bot.ImageAttachment:
attachments = append(attachments, slack.Attachment{
ImageURL: a.URL,
Text: a.AltTxt,
})
}
}
}
if len(attachments) > 0 {
options = append(options, slack.MsgOptionAttachments(attachments...))
}
log.Debug().
Str("channel", channel).
Str("message", message).
Int("attachment count", len(attachments)).
Int("option count", len(options)).
Int("arg count", len(args)).
Msg("Sending message")
_, ts, err = s.api.PostMessage(channel, options...)
if err != nil {
log.Error().Err(err).Msg("Error sending message")
@ -215,24 +240,6 @@ func (s *SlackApp) sendMessageType(channel, message string, meMessage bool) (str
return ts, nil
}
func (s *SlackApp) sendMessage(channel, message string) (string, error) {
log.Debug().
Str("channel", channel).
Str("message", message).
Msg("Sending message")
identifier, err := s.sendMessageType(channel, message, false)
return identifier, err
}
func (s *SlackApp) sendAction(channel, message string) (string, error) {
log.Debug().
Str("channel", channel).
Str("message", message).
Msg("Sending action")
identifier, err := s.sendMessageType(channel, "_"+message+"_", true)
return identifier, err
}
func (s *SlackApp) replyToMessageIdentifier(channel, message, identifier string) (string, error) {
nick := s.config.Get("Nick", "bot")
icon := s.config.Get("IconURL", "https://placekitten.com/128/128")

View File

@ -408,11 +408,18 @@ func (p *BeersPlugin) checkUntappd(channel string) {
msg, checkin.Checkin_comment)
}
args := []interface{}{
channel,
msg,
}
if checkin.Media.Count > 0 {
if strings.Contains(checkin.Media.Items[0].Photo.Photo_img_lg, "photos-processing") {
continue
}
msg += "\nHere's a photo: " + checkin.Media.Items[0].Photo.Photo_img_lg
args = append(args, bot.ImageAttachment{
URL: checkin.Media.Items[0].Photo.Photo_img_lg,
AltTxt: "Here's a photo",
})
}
user.lastCheckin = checkin.Checkin_id
@ -427,7 +434,7 @@ func (p *BeersPlugin) checkUntappd(channel string) {
Int("checkin_id", checkin.Checkin_id).
Str("msg", msg).
Msg("checkin")
p.Bot.Send(bot.Message, channel, msg)
p.Bot.Send(bot.Message, args...)
}
}