diff --git a/bot/interfaces.go b/bot/interfaces.go index 2f430e4..bc71c6c 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -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 diff --git a/connectors/irc/irc.go b/connectors/irc/irc.go index b9f1b24..b57bdd7 100644 --- a/connectors/irc/irc.go +++ b/connectors/irc/irc.go @@ -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 { diff --git a/connectors/slackapp/slackApp.go b/connectors/slackapp/slackApp.go index ad5e24f..fca94d0 100644 --- a/connectors/slackapp/slackApp.go +++ b/connectors/slackapp/slackApp.go @@ -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, - slack.MsgOptionUsername(nick), - slack.MsgOptionText(message, false), - slack.MsgOptionMeMessage()) - } else { - _, ts, err = s.api.PostMessage(channel, - slack.MsgOptionUsername(nick), - slack.MsgOptionText(message, false)) + options := []slack.MsgOption{ + 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") diff --git a/plugins/beers/beers.go b/plugins/beers/beers.go index 138d455..a654c6a 100644 --- a/plugins/beers/beers.go +++ b/plugins/beers/beers.go @@ -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...) } }