listening to thread responses to rpg threads only

This commit is contained in:
skkiesel 2017-10-31 14:14:45 -04:00
parent d9bb7ec3c0
commit 54b83cd444
26 changed files with 122 additions and 12 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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

View File

@ -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}}

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -322,3 +322,5 @@ func reminderer(p *ReminderPlugin) {
p.queueUpNextReminder()
}
}
func (p *ReminderPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }

View File

@ -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
}

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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()