diff --git a/bot/bot.go b/bot/bot.go index eac6f28..777229c 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -93,6 +93,7 @@ func New(config *config.Config, connector Connector) Bot { connector.RegisterMessageReceived(bot.MsgReceived) connector.RegisterEventReceived(bot.EventReceived) + connector.RegisterReplyMessageReceived(bot.ReplyMsgReceived) return bot } diff --git a/bot/handlers.go b/bot/handlers.go index 234e85e..651e0ae 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -22,9 +22,6 @@ func (b *bot) MsgReceived(msg msg.Message) { // msg := b.buildMessage(client, inMsg) // do need to look up user and fix it - - log.Println(msg.User.Name) - if strings.HasPrefix(msg.Body, "help ") && msg.Command { parts := strings.Fields(strings.ToLower(msg.Body)) b.checkHelp(msg.Channel, parts) @@ -55,6 +52,18 @@ func (b *bot) EventReceived(msg msg.Message) { } } +// Handle incoming replys +func (b *bot) ReplyMsgReceived(msg msg.Message, identifier string) { + log.Println("Received message: ", msg) + + for _, name := range b.pluginOrdering { + p := b.plugins[name] + if p.ReplyMessage(msg, identifier) { + break + } + } +} + func (b *bot) SendMessage(channel, message string) string { return b.conn.SendMessage(channel, message) } diff --git a/bot/interfaces.go b/bot/interfaces.go index 2e76e93..2780d29 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -22,6 +22,7 @@ type Bot interface { React(string, string, msg.Message) bool Edit(string, string, string) bool MsgReceived(msg.Message) + ReplyMsgReceived(msg.Message, string) EventReceived(msg.Message) Filter(msg.Message, string) string LastMessage(string) (msg.Message, error) @@ -33,6 +34,7 @@ type Bot interface { type Connector interface { RegisterEventReceived(func(message msg.Message)) RegisterMessageReceived(func(message msg.Message)) + RegisterReplyMessageReceived(func(msg.Message, string)) SendMessage(channel, message string) string SendAction(channel, message string) string @@ -50,6 +52,7 @@ type Connector interface { type Handler interface { Message(message msg.Message) bool Event(kind string, message msg.Message) bool + ReplyMessage(msg.Message, string) bool BotMessage(message msg.Message) bool Help(channel string, parts []string) RegisterWeb() *string diff --git a/irc/irc.go b/irc/irc.go index d348610..4c34529 100644 --- a/irc/irc.go +++ b/irc/irc.go @@ -44,6 +44,7 @@ type Irc struct { eventReceived func(msg.Message) messageReceived func(msg.Message) + replyMessageReceived func(msg.Message, string) } func New(c *config.Config) *Irc { @@ -61,6 +62,10 @@ func (i *Irc) RegisterMessageReceived(f func(msg.Message)) { i.messageReceived = f } +func (i *Irc) RegisterReplyMessageReceived(f func(msg.Message, string)) { + i.replyMessageReceived = f +} + func (i *Irc) JoinChannel(channel string) { log.Printf("Joining channel: %s", channel) i.Client.Out <- irc.Msg{Cmd: irc.JOIN, Args: []string{channel}} diff --git a/plugins/admin/admin.go b/plugins/admin/admin.go index 85988dc..1113bdd 100644 --- a/plugins/admin/admin.go +++ b/plugins/admin/admin.go @@ -117,3 +117,5 @@ func (p *AdminPlugin) BotMessage(message msg.Message) bool { func (p *AdminPlugin) RegisterWeb() *string { return nil } + +func (p *AdminPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/babbler/babbler.go b/plugins/babbler/babbler.go index e5961c9..0ae0b58 100644 --- a/plugins/babbler/babbler.go +++ b/plugins/babbler/babbler.go @@ -935,3 +935,5 @@ func (p *BabblerPlugin) babbleSeedBookends(babblerName string, start, end []stri return strings.Join(words, " "), nil } + +func (p *BabblerPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/beers/beers.go b/plugins/beers/beers.go index 1bc6521..6d1b83e 100644 --- a/plugins/beers/beers.go +++ b/plugins/beers/beers.go @@ -461,3 +461,5 @@ func (p *BeersPlugin) BotMessage(message msg.Message) bool { func (p *BeersPlugin) RegisterWeb() *string { return nil } + +func (p *BeersPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index ebe51b0..ed70349 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -364,3 +364,5 @@ func (p *CounterPlugin) BotMessage(message msg.Message) bool { func (p *CounterPlugin) RegisterWeb() *string { return nil } + +func (p *CounterPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/dice/dice.go b/plugins/dice/dice.go index 07484ce..3ee6dc3 100644 --- a/plugins/dice/dice.go +++ b/plugins/dice/dice.go @@ -100,3 +100,5 @@ func (p *DicePlugin) BotMessage(message msg.Message) bool { func (p *DicePlugin) RegisterWeb() *string { return nil } + +func (p *DicePlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/downtime/downtime.go b/plugins/downtime/downtime.go index e553ed2..ef9df9e 100644 --- a/plugins/downtime/downtime.go +++ b/plugins/downtime/downtime.go @@ -231,3 +231,5 @@ func (p *DowntimePlugin) BotMessage(message msg.Message) bool { func (p *DowntimePlugin) RegisterWeb() *string { return nil } + +func (p *DowntimePlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/emojifyme/emojifyme.go b/plugins/emojifyme/emojifyme.go index 65b892f..e8a6119 100644 --- a/plugins/emojifyme/emojifyme.go +++ b/plugins/emojifyme/emojifyme.go @@ -112,3 +112,5 @@ func (p *EmojifyMePlugin) BotMessage(message msg.Message) bool { func (p *EmojifyMePlugin) RegisterWeb() *string { return nil } + +func (p *EmojifyMePlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index be364cf..17a4e4a 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -764,3 +764,5 @@ func (p *Factoid) serveQuery(w http.ResponseWriter, r *http.Request) { log.Println(err) } } + +func (p *Factoid) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/fact/remember.go b/plugins/fact/remember.go index e4fc6bd..1038eb7 100644 --- a/plugins/fact/remember.go +++ b/plugins/fact/remember.go @@ -170,3 +170,5 @@ func (p *RememberPlugin) recordMsg(message msg.Message) { log.Printf("Logging message: %s: %s", message.User.Name, message.Body) p.Log[message.Channel] = append(p.Log[message.Channel], message) } + +func (p *RememberPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/first/first.go b/plugins/first/first.go index 9d63c6f..3614576 100644 --- a/plugins/first/first.go +++ b/plugins/first/first.go @@ -228,3 +228,5 @@ func (p *FirstPlugin) BotMessage(message msg.Message) bool { func (p *FirstPlugin) RegisterWeb() *string { return nil } + +func (p *FirstPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/inventory/inventory.go b/plugins/inventory/inventory.go index abbaa35..7efb807 100644 --- a/plugins/inventory/inventory.go +++ b/plugins/inventory/inventory.go @@ -236,3 +236,5 @@ func (p *InventoryPlugin) RegisterWeb() *string { // nothing to register return nil } + +func (p *InventoryPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/leftpad/leftpad.go b/plugins/leftpad/leftpad.go index 6beb410..11098d9 100644 --- a/plugins/leftpad/leftpad.go +++ b/plugins/leftpad/leftpad.go @@ -76,3 +76,5 @@ func (p *LeftpadPlugin) RegisterWeb() *string { // nothing to register return nil } + +func (p *LeftpadPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/reaction/reaction.go b/plugins/reaction/reaction.go index a52c17f..7b204b4 100644 --- a/plugins/reaction/reaction.go +++ b/plugins/reaction/reaction.go @@ -80,3 +80,5 @@ func (p *ReactionPlugin) BotMessage(message msg.Message) bool { func (p *ReactionPlugin) RegisterWeb() *string { return nil } + +func (p *ReactionPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/reminder/reminder.go b/plugins/reminder/reminder.go index 1699da2..f55cb3d 100644 --- a/plugins/reminder/reminder.go +++ b/plugins/reminder/reminder.go @@ -322,3 +322,5 @@ func reminderer(p *ReminderPlugin) { p.queueUpNextReminder() } } + +func (p *ReminderPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/rpgORdie/rpgORdie.go b/plugins/rpgORdie/rpgORdie.go index 2733d7b..d922f55 100644 --- a/plugins/rpgORdie/rpgORdie.go +++ b/plugins/rpgORdie/rpgORdie.go @@ -2,7 +2,6 @@ package rpgORdie import ( "strings" - // "log" "time" "github.com/velour/catbase/bot" @@ -11,11 +10,13 @@ import ( type RPGPlugin struct { Bot bot.Bot + listenFor map[string]bool } func New(b bot.Bot) *RPGPlugin { return &RPGPlugin{ Bot: b, + listenFor: map[string]bool{}, } } @@ -23,17 +24,20 @@ func (p *RPGPlugin) Message(message msg.Message) bool { if strings.ToLower(message.Body) == "start rpg" { ts := p.Bot.SendMessage(message.Channel, "I'll edit this.") + p.listenFor[ts] = true + time.Sleep(2 * time.Second) edited := "" for i := 0; i <= 5; i++ { p.Bot.Edit(message.Channel, edited, ts) edited += ":fire:" - time.Sleep(2 * time.Second) + time.Sleep(500 * time.Millisecond) } p.Bot.Edit(message.Channel, "HECK YES", ts) p.Bot.ReplyToMessageIdentifier(message.Channel, "How's this reply?", ts) + return true } return false } @@ -57,3 +61,13 @@ func (p *RPGPlugin) BotMessage(message msg.Message) bool { func (p *RPGPlugin) RegisterWeb() *string { return nil } + +func (p *RPGPlugin) ReplyMessage(message msg.Message, identifier string) bool { + if strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Nick) { + if _, ok := p.listenFor[identifier]; ok { + p.Bot.ReplyToMessageIdentifier(message.Channel, "Pong", identifier) + return true + } + } + return false +} diff --git a/plugins/rss/rss.go b/plugins/rss/rss.go index bc7bee5..099cdf9 100644 --- a/plugins/rss/rss.go +++ b/plugins/rss/rss.go @@ -117,3 +117,5 @@ func (p *RSSPlugin) BotMessage(message msg.Message) bool { func (p *RSSPlugin) RegisterWeb() *string { return nil } + +func (p *RSSPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/stats/stats.go b/plugins/stats/stats.go index 7b7039b..81450b2 100644 --- a/plugins/stats/stats.go +++ b/plugins/stats/stats.go @@ -275,3 +275,5 @@ func (p *StatsPlugin) mkSightingStat(message msg.Message) stats { func (p *StatsPlugin) mkChannelStat(message msg.Message) stats { return stats{stat{mkDay(), "channel", message.Channel, 1}} } + +func (p *StatsPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/talker/talker.go b/plugins/talker/talker.go index 1e01b90..17e9d1b 100644 --- a/plugins/talker/talker.go +++ b/plugins/talker/talker.go @@ -119,3 +119,5 @@ func (p *TalkerPlugin) BotMessage(message msg.Message) bool { func (p *TalkerPlugin) RegisterWeb() *string { return nil } + +func (p *TalkerPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/twitch/twitch.go b/plugins/twitch/twitch.go index 4a5c1a0..941b55a 100644 --- a/plugins/twitch/twitch.go +++ b/plugins/twitch/twitch.go @@ -238,3 +238,5 @@ func (p *TwitchPlugin) checkTwitch(channel string, twitcher *Twitcher, alwaysPri twitcher.game = game } } + +func (p *TwitchPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/your/your.go b/plugins/your/your.go index 9100d37..9e24082 100644 --- a/plugins/your/your.go +++ b/plugins/your/your.go @@ -66,3 +66,5 @@ func (p *YourPlugin) BotMessage(message msg.Message) bool { func (p *YourPlugin) RegisterWeb() *string { return nil } + +func (p *YourPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/zork/zork.go b/plugins/zork/zork.go index 7bd97bd..cabc6d2 100644 --- a/plugins/zork/zork.go +++ b/plugins/zork/zork.go @@ -122,3 +122,5 @@ func (p *ZorkPlugin) Help(ch string, _ []string) { } func (p *ZorkPlugin) RegisterWeb() *string { return nil } + +func (p *ZorkPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/slack/slack.go b/slack/slack.go index b00ed3e..b22f622 100644 --- a/slack/slack.go +++ b/slack/slack.go @@ -41,6 +41,7 @@ type Slack struct { eventReceived func(msg.Message) messageReceived func(msg.Message) + replyMessageReceived func(msg.Message, string) } var idCounter uint64 @@ -134,6 +135,7 @@ type slackMessage struct { User string `json:"user"` Username string `json:"username"` Ts string `json:"ts"` + ThreadTs string `json:"thread_ts"` Error struct { Code uint64 `json:"code"` Msg string `json:"msg"` @@ -193,6 +195,10 @@ func (s *Slack) RegisterMessageReceived(f func(msg.Message)) { s.messageReceived = f } +func (s *Slack) RegisterReplyMessageReceived(f func(msg.Message, string)) { + s.replyMessageReceived = f +} + func (s *Slack) SendMessageType(channel, message string, meMessage bool) (string, error) { postUrl := "https://slack.com/api/chat.postMessage" if meMessage { @@ -384,19 +390,17 @@ func (s *Slack) Serve() error { } switch msg.Type { case "message": - if !msg.Hidden { + if !msg.Hidden && msg.ThreadTs == "" { m := s.buildMessage(msg) - - log.Println() - log.Println(m) - log.Println() - if m.Time.Before(s.lastRecieved) { log.Printf("Ignoring message: %+v\nlastRecieved: %v msg: %v", msg.ID, s.lastRecieved, m.Time) } else { s.lastRecieved = m.Time - s.messageReceived(s.buildMessage(msg)) + s.messageReceived(m) } + } else if msg.ThreadTs != "" { + //we're throwing away some information here by not parsing the correct reply object type, but that's okay + s.replyMessageReceived(s.buildLightReplyMessage(msg), msg.ThreadTs) } else { log.Printf("THAT MESSAGE WAS HIDDEN: %+v", msg.ID) } @@ -453,6 +457,40 @@ func (s *Slack) buildMessage(m slackMessage) msg.Message { } } +func (s *Slack) buildLightReplyMessage(m slackMessage) msg.Message { + text := html.UnescapeString(m.Text) + + text = fixText(s.getUser, text) + + isCmd, text := bot.IsCmd(s.config, text) + + isAction := m.SubType == "me_message" + + u, _ := s.getUser(m.User) + if m.Username != "" { + u = m.Username + } + + tstamp := slackTStoTime(m.Ts) + + return msg.Message{ + User: &user.User{ + ID: m.User, + Name: u, + }, + Body: text, + Raw: m.Text, + Channel: m.Channel, + Command: isCmd, + Action: isAction, + Host: string(m.ID), + Time: tstamp, + AdditionalData: map[string]string{ + "RAW_SLACK_TIMESTAMP": m.Ts, + }, + } +} + // markAllChannelsRead gets a list of all channels and marks each as read func (s *Slack) markAllChannelsRead() { chs := s.getAllChannels()