Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(slack): use conversations api #131

Merged
merged 9 commits into from
Nov 11, 2020
Next Next commit
fix(slack): use conversations api for channel info
  • Loading branch information
wass3r committed Mar 6, 2020
commit a2ef11a53cf99f483604f689e01edca51a4ec5af
69 changes: 49 additions & 20 deletions remote/slack/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,21 @@ func handleCallBack(api *slack.Client, event slackevents.EventsAPIInnerEvent, bo
inputMsgs <- populateMessage(models.NewMessage(), msgType, channel, text, timestamp, threadTimestamp, link, mentioned, user, bot)
}
// This is an Event shared between RTM and the Events API
case *slack.MemberJoinedChannelEvent:
// get bot rooms
bot.Rooms = getRooms(api)
bot.Log.Debugf("%s has joined the channel %s", bot.Name, bot.Rooms[ev.Channel])
case *slack.MemberLeftChannelEvent:
// remove room
delete(bot.Rooms, ev.Channel)
bot.Log.Debugf("%s has left the channel %s", bot.Name, bot.Rooms[ev.Channel])
case *slackevents.MemberJoinedChannelEvent:
// limit to our bot
if ev.User == bot.ID {
// look up channel info, since 'ev' only gives us ID
channel, err := api.GetChannelInfo(ev.Channel)
if err != nil {
bot.Log.Debugf("unable to fetch channel info for channel joined event: %v", err)
} else {
// add the room to the lookup
if bot.Rooms[channel.Name] == "" {
bot.Rooms[channel.Name] = channel.ID
bot.Log.Debugf("Joined new channel. %s(%s) added to lookup", channel.Name, channel.ID)
}
}
}
default:
bot.Log.Errorf("getEventsAPIEventHandler: Unrecognized event type: %v", ev)
}
Expand Down Expand Up @@ -265,16 +272,39 @@ func getInteractiveComponentRuleHandler(verificationToken string, inputMsgs chan
// getRooms - return a map of rooms
func getRooms(api *slack.Client) map[string]string {
rooms := make(map[string]string)
// get public channels
channels, _ := api.GetChannels(true)
for _, channel := range channels {
rooms[channel.Name] = channel.ID

// we're getting all channel types by default
// this can be controlled with permission scopes in Slack:
// channels:read, groups:read, im:read, mpim:read
cp := slack.GetConversationsParameters{
Cursor: "",
ExcludeArchived: "true",
Limit: 1000, // this is the maximum value allowed
Types: []string{"public_channel", "private_channel", "mpim", "im"},
}
// get private channels
groups, _ := api.GetGroups(true)
for _, group := range groups {
rooms[group.Name] = group.ID

// there's a possibility we need to page through results
// the results, so we're looping until there are no more pages
for {
channels, nc, err := api.GetConversations(&cp)
if err != nil {
break
}

// populate our channel map
for _, channel := range channels {
rooms[channel.Name] = channel.ID
}

// no more pages to process? quit the loop
if len(nc) == 0 {
break
}

// override the cursor
cp.Cursor = nc
}

return rooms
}

Expand Down Expand Up @@ -554,11 +584,8 @@ func readFromRTM(rtm *slack.RTM, inputMsgs chan<- models.Message, bot *models.Bo
// populate user groups
populateUserGroups(bot)
bot.Log.Debugf("RTM connection established!")
case *slack.GroupJoinedEvent:
case *slack.ChannelJoinedEvent:
// when the bot joins a channel add it to the internal lookup
// NOTE: looks like there is another unsupported event we could use
// Received unmapped event \"member_joined_channel\"
// Maybe watch for an update to slack package for future support
if bot.Rooms[ev.Channel.Name] == "" {
bot.Rooms[ev.Channel.Name] = ev.Channel.ID
bot.Log.Debugf("Joined new channel. %s(%s) added to lookup", ev.Channel.Name, ev.Channel.ID)
Expand All @@ -573,6 +600,8 @@ func readFromRTM(rtm *slack.RTM, inputMsgs chan<- models.Message, bot *models.Bo
if !bot.CLI {
bot.Log.Debug("Invalid Authorization. Please double check your Slack token.")
}
default:
bot.Log.Debugf("%+v", ev)
}
}
}
Expand Down