Search code examples
slack-api

Slack API - conversations.list performance decrease as number of archived channels increase


I am trying to improve performance for our slack integration as it is taking a long time to fetch all of the channels. We are on the free tier of slack so we don't have an option to delete channels using the API. When our automation runs against slack, we create a channel and archive it once done. This has led to a large number of archived channels in our workspace.

When we hit the conversations.list endpoint, we apply a filter to exclude archived channels but the filtering is applied after paging, so we have to page through a ton of empty pages. Initially I thought I could just say that if a page was empty, break out of the loop and ignore the rest of the pages. However, I found that if I unarchive a channel on let's say page 3, when I use conversations.list, page 1 and 2 are empty (all channels on these pages are archived) and the newly unarchived channel will still be on page 3. With this behavior, I need to loop through all the pages to ensure I grab any channels that are not archived.

We use the conversations.list endpoint to see if we have a channel that exists for a given record in our software. When creating a new record or editing an existing record, we need to search for the conversation by name. To do this, we fetch all the channels and periodically populate a local channel cache so we can look up the channel id for a given channel name. When we do have to populate the channel cache, depending on the connection type it can take several minutes to fetch all the channels

If there was a way to search for channels by name in the API, this would solve the issue as we could just search for the channel name we need without having to fetch every channel. However, I can't find a method like this in the API reference and the extra args for conversations.list don't seem to help.

Just wondering if anyone has run into an issue like this and if so what they did to work around it? Thanks for reading!


Solution

  • I've just discovered a wonderfully hacky way to do this. You are right that conversations.list is the official way, there is no other method of searching for channels by name, and your paging is what is supposed to happen.

    However, if you can gain access to chat.scheduleMessage then you can schedule a message by the channel name. This will allow you to work out if a channel exists by its name or not by seeing if you get a valid response or an error (just schedule the message way off in the future).

    To prevent a deluge of scheduled messages in the future, you can immediately then delete the scheduled message via chat.deleteScheduledMessage