From 95d99fa94d9ada09e07d93ba4787a7cd621b8c6a Mon Sep 17 00:00:00 2001 From: skkiesel Date: Fri, 17 Nov 2017 10:16:10 -0500 Subject: [PATCH 1/5] grab the bot's bot id from the message response and use that to avoid self-reply loops --- config/config.go | 1 - slack/slack.go | 17 +++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index 15241a2..d00cb5c 100644 --- a/config/config.go +++ b/config/config.go @@ -109,7 +109,6 @@ type Config struct { MinPush int MaxPush int } - BotList map[string]bool } func init() { diff --git a/slack/slack.go b/slack/slack.go index 5dddef3..94a3dc2 100644 --- a/slack/slack.go +++ b/slack/slack.go @@ -37,6 +37,8 @@ type Slack struct { users map[string]string + myBotID string + emoji map[string]string eventReceived func(msg.Message) @@ -228,6 +230,7 @@ func (s *Slack) SendMessageType(channel, message string, meMessage bool) (string type MessageResponse struct { OK bool `json:"ok"` Timestamp string `json:"ts"` + BotID string `json:"message.bot_id"` } var mr MessageResponse @@ -240,6 +243,8 @@ func (s *Slack) SendMessageType(channel, message string, meMessage bool) (string return "", errors.New("failure response received") } + s.myBotID = mr.BotID + return mr.Timestamp, err } @@ -392,16 +397,8 @@ func (s *Slack) Serve() error { } switch msg.Type { case "message": - botOK := true - if msg.BotID != "" { - u, _ := s.getUser(msg.User) - if u == "" && msg.Username != "" { - u = msg.Username - } - log.Printf("User: %s, BotList: %+v", u, s.config.BotList) - botOK = s.config.BotList[strings.Title(u)] - } - if botOK && !msg.Hidden && msg.ThreadTs == "" { + isItMe := s.myBotID != "" && msg.BotID != s.myBotID + if !isItMe && !msg.Hidden && msg.ThreadTs == "" { m := s.buildMessage(msg) if m.Time.Before(s.lastRecieved) { log.Printf("Ignoring message: %+v\nlastRecieved: %v msg: %v", msg.ID, s.lastRecieved, m.Time) From 7841da0f48b0a40131b21fcb6aa1397524753583 Mon Sep 17 00:00:00 2001 From: skkiesel Date: Fri, 17 Nov 2017 10:30:15 -0500 Subject: [PATCH 2/5] one more try --- slack/slack.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/slack/slack.go b/slack/slack.go index 94a3dc2..3c81389 100644 --- a/slack/slack.go +++ b/slack/slack.go @@ -230,7 +230,9 @@ func (s *Slack) SendMessageType(channel, message string, meMessage bool) (string type MessageResponse struct { OK bool `json:"ok"` Timestamp string `json:"ts"` - BotID string `json:"message.bot_id"` + Message struct { + BotID string `json:"bot_id"` + } `json:"message"` } var mr MessageResponse @@ -243,7 +245,7 @@ func (s *Slack) SendMessageType(channel, message string, meMessage bool) (string return "", errors.New("failure response received") } - s.myBotID = mr.BotID + s.myBotID = mr.Message.BotID return mr.Timestamp, err } @@ -397,7 +399,7 @@ func (s *Slack) Serve() error { } switch msg.Type { case "message": - isItMe := s.myBotID != "" && msg.BotID != s.myBotID + isItMe := msg.BotID != "" && msg.BotID == s.myBotID if !isItMe && !msg.Hidden && msg.ThreadTs == "" { m := s.buildMessage(msg) if m.Time.Before(s.lastRecieved) { From 83289d65f0394c7f8f8c34b2709dab1de31e984b Mon Sep 17 00:00:00 2001 From: Steve McCoy Date: Mon, 27 Nov 2017 22:07:59 -0500 Subject: [PATCH 3/5] Don't count "a" and "it" toward the emojification score Fixes #95. --- plugins/emojifyme/emojifyme.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/plugins/emojifyme/emojifyme.go b/plugins/emojifyme/emojifyme.go index e8a6119..e71aede 100644 --- a/plugins/emojifyme/emojifyme.go +++ b/plugins/emojifyme/emojifyme.go @@ -71,19 +71,25 @@ func (p *EmojifyMePlugin) Message(message msg.Message) bool { tokens := strings.Fields(strings.ToLower(message.Body)) for i, token := range tokens { if _, ok := p.Emoji[token]; ok { - emojied++ + if token != "a" && token != "it" { + emojied++ + } tokens[i] = ":" + token + ":" } else if strings.HasSuffix(token, "s") { //Check to see if we can strip the trailing "es" off and get an emoji temp := strings.TrimSuffix(token, "s") if _, ok := p.Emoji[temp]; ok { - emojied++ + if token != "a" && token != "it" { + emojied++ + } tokens[i] = ":" + temp + ":s" } else if strings.HasSuffix(token, "es") { //Check to see if we can strip the trailing "es" off and get an emoji temp := strings.TrimSuffix(token, "es") if _, ok := p.Emoji[temp]; ok { - emojied++ + if token != "a" && token != "it" { + emojied++ + } tokens[i] = ":" + temp + ":es" } } From 85b649bc98ebac7be503318f6c1bd009dff793cb Mon Sep 17 00:00:00 2001 From: Steve McCoy Date: Mon, 27 Nov 2017 22:09:55 -0500 Subject: [PATCH 4/5] =?UTF-8?q?Oh,=20and=20handle=20the=20plurals=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/emojifyme/emojifyme.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/emojifyme/emojifyme.go b/plugins/emojifyme/emojifyme.go index e71aede..8898b54 100644 --- a/plugins/emojifyme/emojifyme.go +++ b/plugins/emojifyme/emojifyme.go @@ -79,7 +79,7 @@ func (p *EmojifyMePlugin) Message(message msg.Message) bool { //Check to see if we can strip the trailing "es" off and get an emoji temp := strings.TrimSuffix(token, "s") if _, ok := p.Emoji[temp]; ok { - if token != "a" && token != "it" { + if temp != "a" && temp != "it" { emojied++ } tokens[i] = ":" + temp + ":s" @@ -87,7 +87,7 @@ func (p *EmojifyMePlugin) Message(message msg.Message) bool { //Check to see if we can strip the trailing "es" off and get an emoji temp := strings.TrimSuffix(token, "es") if _, ok := p.Emoji[temp]; ok { - if token != "a" && token != "it" { + if temp != "a" && temp != "it" { emojied++ } tokens[i] = ":" + temp + ":es" From 5794c73efdcb27132b4d858b8c516cebfe3a1cc7 Mon Sep 17 00:00:00 2001 From: Steve McCoy Date: Sun, 3 Dec 2017 13:04:55 -0500 Subject: [PATCH 5/5] =?UTF-8?q?Change=20to=20use=20the=20lua=20config?= =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 1 + example_config.lua | 6 +++++- plugins/emojifyme/emojifyme.go | 16 +++++++++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index d00cb5c..0d345c7 100644 --- a/config/config.go +++ b/config/config.go @@ -91,6 +91,7 @@ type Config struct { } Emojify struct { Chance float64 + Scoreless []string } Reaction struct { GeneralChance float64 diff --git a/example_config.lua b/example_config.lua index 58e3d74..fce3646 100644 --- a/example_config.lua +++ b/example_config.lua @@ -29,7 +29,11 @@ config = { YourChance = 0.4 }, Emojify = { - Chance = 0.02 + Chance = 0.02, + Scoreless = { + "a", + "it" + } }, DB = { File = "catbase.db", diff --git a/plugins/emojifyme/emojifyme.go b/plugins/emojifyme/emojifyme.go index 8898b54..4c1383d 100644 --- a/plugins/emojifyme/emojifyme.go +++ b/plugins/emojifyme/emojifyme.go @@ -67,11 +67,12 @@ func (p *EmojifyMePlugin) Message(message msg.Message) bool { } } + inertTokens := p.Bot.Config().Emojify.Scoreless emojied := 0.0 tokens := strings.Fields(strings.ToLower(message.Body)) for i, token := range tokens { if _, ok := p.Emoji[token]; ok { - if token != "a" && token != "it" { + if !stringsContain(inertTokens, token) { emojied++ } tokens[i] = ":" + token + ":" @@ -79,7 +80,7 @@ func (p *EmojifyMePlugin) Message(message msg.Message) bool { //Check to see if we can strip the trailing "es" off and get an emoji temp := strings.TrimSuffix(token, "s") if _, ok := p.Emoji[temp]; ok { - if temp != "a" && temp != "it" { + if !stringsContain(inertTokens, temp) { emojied++ } tokens[i] = ":" + temp + ":s" @@ -87,7 +88,7 @@ func (p *EmojifyMePlugin) Message(message msg.Message) bool { //Check to see if we can strip the trailing "es" off and get an emoji temp := strings.TrimSuffix(token, "es") if _, ok := p.Emoji[temp]; ok { - if temp != "a" && temp != "it" { + if !stringsContain(inertTokens, temp) { emojied++ } tokens[i] = ":" + temp + ":es" @@ -120,3 +121,12 @@ func (p *EmojifyMePlugin) RegisterWeb() *string { } func (p *EmojifyMePlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } + +func stringsContain(haystack []string, needle string) bool { + for _, s := range haystack { + if s == needle { + return true + } + } + return false +}