7

I am playing with slack apis to create an integration. I am able to sucessfully create a slack channel using

 this.http.post(slackApiHost + '/channels.create', payload, {headers: headers})
            .subscribe(
                res => {
                    console.log(res);
                    resolve(res)
                },
                err => {
                    console.log('error:' + err)
                }
                )
        })

the payload looks like

var payload = {
        "name" : channelName
    };

So, it will fail with name_taken if the channel already exists. which is great. However, I need to find the channel id of the existing channel so that i can then use it for my purpose. how do i go about it?

Vik
  • 8,721
  • 27
  • 83
  • 168
  • 1
    Is this information useful for you? https://api.slack.com/methods/channels.list This was not what you want, I'm sorry. – Tanaike Apr 30 '18 at 22:46
  • no this list all. i m looking ot search a specfic by name – Vik May 01 '18 at 02:22
  • 1
    I'm sorry for the inconvenience. I had thought that the existence of channel and the channel name and ID can be retrieved using the channel list retrieved from this API. From your comment, I could understand that this way is not what you want. I'm really sorry for my incomplete proposal. – Tanaike May 01 '18 at 03:00

4 Answers4

8

To get a list of all existing channel you can use conversations.list. This is the most flexible approach and allows you to retrieve any type of channel if you so choose.

If you are looking for a channel by a specific name you will need to first retrieve the list of all channels and then run your own name matching against the full list. An API method which allows you to directly search for a channel by name does not exist.

Note that if you are looking for private channels this method will only retrieve channels that you (the installer of your Slack app / your bot user) has been invited to.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • 1
    well. As I said, that is the only available approach for your problem. There is no other / "more performant" approach. – Erik Kalkoken May 01 '18 at 17:55
  • 36
    Well that's a real shame – Eli Dec 24 '18 at 22:59
  • 3
    Interestingly, when using the Slack web client, I see that there is a channel search API via https://edgeapi.slack.com/cache/TMQ5151S9/channels/search (when checking the network in the dev toolbar), but it doesn't seem to be "publicly available" (no way to properly retrieve/renew the necessary access token) nor documented. – Mathias Conradt Aug 26 '19 at 17:30
  • 1
    It's 2022 and still can't filter this endpoint. We have to sync the channels to our own database and search through them since the pagination + string matching strategy suggested as a different answer to this question is risky due to rate-limiting. – tester Oct 11 '22 at 19:56
  • 1
    There is a method available for search(https://api.slack.com/methods/admin.conversations.search). API requires token of an admin user. But it seems like this can be used only by Enterprise Grid organization. Guessing it is like a premium paid workspace. – Inba Jan 03 '23 at 12:07
4

There's currently no direct Slack API to find a slack channel by its name. But you can use conversations.list to handle this and here's the right code to use:

const slack = new SlackWebClient(token);
const channelNameToFind = "test";

for await (const page of slack.paginate("conversations.list", {
  limit: 50,
})) {
  for (const channel of page.channels) {
    if (channel.name === channelNameToFind) {
      console.log(`Found ${channelNameToFind} with slack id ${channel.id}`);
      break;
    }
  }
}

This is the right way™️ to do it since it will ensure you stop listing channels as soon as you found the right one.

Good luck!

vvo
  • 2,653
  • 23
  • 30
4

You can use built-in admin.conversations.search and then iterate found results.

One more kind of fast tricky solution: try to send chat.scheduleMessage and then if it was sent chat.deleteScheduledMessage or catch error. Ruby example:

def channel_exists?(channel)
  begin
    response = @slack_client.chat_scheduleMessage(channel: channel, text: '', post_at: Time.now.to_i + 100)
    @slack_client.chat_deleteScheduledMessage(channel: channel, scheduled_message_id: response.scheduled_message_id)
    return true
  rescue Slack::Web::Api::Errors::ChannelNotFound
    return false
  end
end
0

If you don't mind posting in that channel, you can use the chat.postMessage api that returns the channel id. This works with a bot token as well.

  • It appears the `chat.PostMessage` endpoint only exists under `slack.MsgOptionPost()` - How do I use that function without knowing the channel ID already? – SethYes Aug 11 '23 at 15:21