mirror of https://github.com/velour/catbase.git
commit
026eda5b24
|
@ -30,6 +30,11 @@ const (
|
||||||
SelfMessage
|
SelfMessage
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ImageAttachment struct {
|
||||||
|
URL string
|
||||||
|
AltTxt string
|
||||||
|
}
|
||||||
|
|
||||||
type Kind int
|
type Kind int
|
||||||
type Callback func(Kind, msg.Message, ...interface{}) bool
|
type Callback func(Kind, msg.Message, ...interface{}) bool
|
||||||
type CallbackMap map[string]map[Kind][]Callback
|
type CallbackMap map[string]map[Kind][]Callback
|
||||||
|
|
|
@ -60,9 +60,9 @@ func (i *Irc) Send(kind bot.Kind, args ...interface{}) (string, error) {
|
||||||
switch kind {
|
switch kind {
|
||||||
case bot.Reply:
|
case bot.Reply:
|
||||||
case bot.Message:
|
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:
|
case bot.Action:
|
||||||
return i.sendAction(args[0].(string), args[1].(string))
|
return i.sendAction(args[0].(string), args[1].(string), args...)
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
|
@ -73,7 +73,7 @@ func (i *Irc) JoinChannel(channel string) {
|
||||||
i.Client.Out <- irc.Msg{Cmd: irc.JOIN, Args: []string{channel}}
|
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 {
|
for len(message) > 0 {
|
||||||
m := irc.Msg{
|
m := irc.Msg{
|
||||||
Cmd: "PRIVMSG",
|
Cmd: "PRIVMSG",
|
||||||
|
@ -96,15 +96,32 @@ func (i *Irc) sendMessage(channel, message string) (string, error) {
|
||||||
<-throttle
|
<-throttle
|
||||||
|
|
||||||
i.Client.Out <- m
|
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
|
return "NO_IRC_IDENTIFIERS", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sends action to channel
|
// 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"
|
message = actionPrefix + " " + message + "\x01"
|
||||||
|
|
||||||
return i.sendMessage(channel, message)
|
return i.sendMessage(channel, message, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Irc) GetEmojiList() map[string]string {
|
func (i *Irc) GetEmojiList() map[string]string {
|
||||||
|
|
|
@ -171,9 +171,9 @@ func (s *SlackApp) msgReceivd(msg *slackevents.MessageEvent) {
|
||||||
func (s *SlackApp) Send(kind bot.Kind, args ...interface{}) (string, error) {
|
func (s *SlackApp) Send(kind bot.Kind, args ...interface{}) (string, error) {
|
||||||
switch kind {
|
switch kind {
|
||||||
case bot.Message:
|
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:
|
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:
|
case bot.Edit:
|
||||||
return s.edit(args[0].(string), args[1].(string), args[2].(string))
|
return s.edit(args[0].(string), args[1].(string), args[2].(string))
|
||||||
case bot.Reply:
|
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)
|
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("")
|
ts, err := "", fmt.Errorf("")
|
||||||
nick := s.config.Get("Nick", "bot")
|
nick := s.config.Get("Nick", "bot")
|
||||||
|
|
||||||
if meMessage {
|
options := []slack.MsgOption{
|
||||||
_, ts, err = s.api.PostMessage(channel,
|
|
||||||
slack.MsgOptionUsername(nick),
|
slack.MsgOptionUsername(nick),
|
||||||
slack.MsgOptionText(message, false),
|
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 {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Error sending message")
|
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
|
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) {
|
func (s *SlackApp) replyToMessageIdentifier(channel, message, identifier string) (string, error) {
|
||||||
nick := s.config.Get("Nick", "bot")
|
nick := s.config.Get("Nick", "bot")
|
||||||
icon := s.config.Get("IconURL", "https://placekitten.com/128/128")
|
icon := s.config.Get("IconURL", "https://placekitten.com/128/128")
|
||||||
|
|
|
@ -408,11 +408,18 @@ func (p *BeersPlugin) checkUntappd(channel string) {
|
||||||
msg, checkin.Checkin_comment)
|
msg, checkin.Checkin_comment)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
args := []interface{}{
|
||||||
|
channel,
|
||||||
|
msg,
|
||||||
|
}
|
||||||
if checkin.Media.Count > 0 {
|
if checkin.Media.Count > 0 {
|
||||||
if strings.Contains(checkin.Media.Items[0].Photo.Photo_img_lg, "photos-processing") {
|
if strings.Contains(checkin.Media.Items[0].Photo.Photo_img_lg, "photos-processing") {
|
||||||
continue
|
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
|
user.lastCheckin = checkin.Checkin_id
|
||||||
|
@ -427,7 +434,7 @@ func (p *BeersPlugin) checkUntappd(channel string) {
|
||||||
Int("checkin_id", checkin.Checkin_id).
|
Int("checkin_id", checkin.Checkin_id).
|
||||||
Str("msg", msg).
|
Str("msg", msg).
|
||||||
Msg("checkin")
|
Msg("checkin")
|
||||||
p.Bot.Send(bot.Message, channel, msg)
|
p.Bot.Send(bot.Message, args...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue