From 4925069ac906273b1e17aaeb94613d6bb1388575 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Thu, 7 Feb 2019 14:21:22 -0500 Subject: [PATCH] slackApp: fix user info functionality --- connectors/slackapp/fix_text.go | 6 +++--- connectors/slackapp/slackApp.go | 37 ++++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/connectors/slackapp/fix_text.go b/connectors/slackapp/fix_text.go index 24735ad..8a23a53 100644 --- a/connectors/slackapp/fix_text.go +++ b/connectors/slackapp/fix_text.go @@ -13,7 +13,7 @@ import ( // • Strips < and > surrounding links. // // This was directly bogarted from velour/chat with emoji conversion removed. -func fixText(findUser func(id string) (string, bool), text string) string { +func fixText(findUser func(id string) (string, error), text string) string { var output []rune for len(text) > 0 { r, i := utf8.DecodeRuneInString(text) @@ -48,14 +48,14 @@ func fixText(findUser func(id string) (string, bool), text string) string { return string(output) } -func fixTag(findUser func(string) (string, bool), tag []rune) ([]rune, bool) { +func fixTag(findUser func(string) (string, error), tag []rune) ([]rune, bool) { switch { case hasPrefix(tag, "@U"): if i := indexRune(tag, '|'); i >= 0 { return tag[i+1:], true } if findUser != nil { - if u, ok := findUser(string(tag[1:])); ok { + if u, err := findUser(string(tag[1:])); err == nil { return []rune(u), true } } diff --git a/connectors/slackapp/slackApp.go b/connectors/slackapp/slackApp.go index c8348de..2d309f7 100644 --- a/connectors/slackapp/slackApp.go +++ b/connectors/slackapp/slackApp.go @@ -124,7 +124,6 @@ func (s *SlackApp) msgReceivd(msg *slackevents.MessageEvent) { } func (s *SlackApp) Send(kind bot.Kind, args ...interface{}) (string, error) { - // TODO: All of these local calls to slack should get routed through the library switch kind { case bot.Message: return s.sendMessage(args[0].(string), args[1].(string)) @@ -285,28 +284,50 @@ func (s *SlackApp) buildMessage(m *slackevents.MessageEvent) msg.Message { } // Get username for Slack user ID -func (s *SlackApp) getUser(id string) (string, bool) { +func (s *SlackApp) getUser(id string) (string, error) { if name, ok := s.users[id]; ok { - return name, true + return name, nil } log.Printf("User %s not already found, requesting info", id) u, err := s.api.GetUserInfo(id) if err != nil { - return "UNKNOWN", false + return "UNKNOWN", err } s.users[id] = u.Name - return s.users[id], true + return s.users[id], nil } // Who gets usernames out of a channel func (s *SlackApp) Who(id string) []string { + if s.userToken == "NONE" { + log.Println("Cannot get emoji list without slack.usertoken") + return []string{s.config.Get("nick", "bot")} + } + dbg := slack.OptionDebug(true) + api := slack.New(s.userToken) + dbg(api) + log.Println("Who is queried for ", id) // Not super sure this is the correct call - members, err := s.api.GetUserGroupMembers(id) + params := &slack.GetUsersInConversationParameters{ + ChannelID: id, + Limit: 50, + } + members, _, err := api.GetUsersInConversation(params) if err != nil { log.Println(err) - return []string{} + return []string{s.config.Get("nick", "bot")} } - return members + + ret := []string{} + for _, m := range members { + u, err := s.getUser(m) + if err != nil { + log.Printf("Couldn't get user %s: %s", m, err) + continue + } + ret = append(ret, u) + } + return ret }