Archiving Slack Channels

Since we’re making channels in Slack via our project creation, it made sense to archive them when the project was completed.

In projects (this particular post type) we have a custom field for the start date of the project and one for the end date of the project.

Step one is to check on updates whether the post has the end-date field filled out. In my case, this is one of the legacy ACF fields that survived my great metadata purge. So checking it is done like so . . .

function projectClosed($id){
	if(get_field('end_date', $id)){
		return true;
	} else {
		return false;
	}
}

The Slack archive API piece looks like this.


function slackArchive($id, $post){
        $token ='YOUR_TOKEN_GOES_HERE';
	$id = $post->ID;
	if (projectClosed($id)){
		$channelName = substr($post->post_name,0,21);
		$channelId = getSlackChannelId('p-'.$channelName);
		$url = 'https://slack.com/api/channels.archive?token='.$token.'&channel='.$channelId.'&pretty=1';
		@file_get_contents($url);	
	}
}

And finally we run this function when projects are updated like so.

add_action( 'save_post_project', 'slackArchive', 10, 2);

We’re still experimenting with this workflow and archiving is a decent start. You can easily reactivate it and results still turn up in searches.

It’s likely we’ll also rename it from p-whatever to z-whatever to get it out of the way.