slackApp: fix user info functionality

This commit is contained in:
Chris Sexton 2019-02-07 14:21:22 -05:00
parent c8abb4b423
commit 4925069ac9
2 changed files with 32 additions and 11 deletions

View File

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

View File

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