grab the bot's bot id from the message response and use that to avoid self-reply loops

This commit is contained in:
skkiesel 2017-11-17 10:16:10 -05:00
parent 9f332909b5
commit 95d99fa94d
2 changed files with 7 additions and 11 deletions

View File

@ -109,7 +109,6 @@ type Config struct {
MinPush int MinPush int
MaxPush int MaxPush int
} }
BotList map[string]bool
} }
func init() { func init() {

View File

@ -37,6 +37,8 @@ type Slack struct {
users map[string]string users map[string]string
myBotID string
emoji map[string]string emoji map[string]string
eventReceived func(msg.Message) eventReceived func(msg.Message)
@ -228,6 +230,7 @@ func (s *Slack) SendMessageType(channel, message string, meMessage bool) (string
type MessageResponse struct { type MessageResponse struct {
OK bool `json:"ok"` OK bool `json:"ok"`
Timestamp string `json:"ts"` Timestamp string `json:"ts"`
BotID string `json:"message.bot_id"`
} }
var mr MessageResponse var mr MessageResponse
@ -240,6 +243,8 @@ func (s *Slack) SendMessageType(channel, message string, meMessage bool) (string
return "", errors.New("failure response received") return "", errors.New("failure response received")
} }
s.myBotID = mr.BotID
return mr.Timestamp, err return mr.Timestamp, err
} }
@ -392,16 +397,8 @@ func (s *Slack) Serve() error {
} }
switch msg.Type { switch msg.Type {
case "message": case "message":
botOK := true isItMe := s.myBotID != "" && msg.BotID != s.myBotID
if msg.BotID != "" { if !isItMe && !msg.Hidden && msg.ThreadTs == "" {
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 == "" {
m := s.buildMessage(msg) m := s.buildMessage(msg)
if m.Time.Before(s.lastRecieved) { if m.Time.Before(s.lastRecieved) {
log.Printf("Ignoring message: %+v\nlastRecieved: %v msg: %v", msg.ID, s.lastRecieved, m.Time) log.Printf("Ignoring message: %+v\nlastRecieved: %v msg: %v", msg.ID, s.lastRecieved, m.Time)