slack: add whitelist for bots

This commit is contained in:
Chris Sexton 2017-11-11 11:51:50 -05:00
parent 11bb338e6b
commit 20bf8748a2
2 changed files with 9 additions and 3 deletions

View File

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

View File

@ -134,7 +134,7 @@ type slackMessage struct {
Text string `json:"text"` Text string `json:"text"`
User string `json:"user"` User string `json:"user"`
Username string `json:"username"` Username string `json:"username"`
BotId string `json:"bot_id"` BotID string `json:"bot_id"`
Ts string `json:"ts"` Ts string `json:"ts"`
ThreadTs string `json:"thread_ts"` ThreadTs string `json:"thread_ts"`
Error struct { Error struct {
@ -363,7 +363,6 @@ func (s *Slack) populateEmojiList() {
func (s *Slack) receiveMessage() (slackMessage, error) { func (s *Slack) receiveMessage() (slackMessage, error) {
var msg []byte var msg []byte
m := slackMessage{} m := slackMessage{}
//err := websocket.JSON.Receive(s.ws, &m)
err := websocket.Message.Receive(s.ws, &msg) err := websocket.Message.Receive(s.ws, &msg)
if err != nil { if err != nil {
log.Println("Error decoding WS message") log.Println("Error decoding WS message")
@ -393,7 +392,13 @@ func (s *Slack) Serve() error {
} }
switch msg.Type { switch msg.Type {
case "message": case "message":
if msg.BotId == "" && !msg.Hidden && msg.ThreadTs == "" { botOK := true
if msg.BotID != "" {
u, _ := s.getUser(msg.User)
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)