List Public Slack Channels via API


Image from page 249 of “The development of the chick; an introduction to embryology” (1919) flickr photo by Internet Archive Book Images shared with no copyright restriction (Flickr Commons)

I ended up doing this while pursuing some of the API integration stuff for our projects page. It doesn’t list the private pages and might be useful to someone.

  
    $token = 'YOURTOKENHERE';
	$channel_url = 'https://slack.com/api/channels.list?token='.$token.'&exclude_archived=true&pretty=1';
	$channels = file_get_contents($channel_url);
	$data = json_decode($channels);

	$num_chan = sizeof($data->channels);
	echo '<h3>'.$num_chan.'</h3><ol>';
	for($i=1; $i < $num_chan; $i++){
		echo '<li>'.$data->channels[$i]->name . '</li>';
	}
	echo '</ol>'

This was the byproduct of looking for a way to look up the ID for a particular channel which ended up looking like this.

function getSlackChannelId ($the_channel){
$token ='YOURTOKENHERE';
		$channel_url = 'https://slack.com/api/channels.list?token='.$token.'&exclude_archived=true&pretty=1';
		$channels = file_get_contents($channel_url);
		$data = json_decode($channels);		
		$num_chan = sizeof($data->channels);
		for($i=1; $i < $num_chan; $i++){
			$data->channels[$i]->name;
			if ($data->channels[$i]->name == $the_channel){
				return $data->channels[$i]->id;
			} else {
				return '';
			}
	}
}

Comments on this post

No comments.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL