Skip to content

Instantly share code, notes, and snippets.

@lewayotte
Created February 22, 2019 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewayotte/2633e63b7e02320a2d88c311313dd03b to your computer and use it in GitHub Desktop.
Save lewayotte/2633e63b7e02320a2d88c311313dd03b to your computer and use it in GitHub Desktop.
Mute specific Slack channels after hours, Unmute them during business hours...
<?php
// cronjob:
// 0 8 * * * /usr/bin/php /path/to/file.php unmute > /dev/null 2>&1
// 0 18 * * * /usr/bin/php /path/to/file.php mute > /dev/null 2>&1
if ( 1 < $argc ) {
$channels = array(
'CHANNEL_ID',
);
$after_hours_mute = array (
'CHANNEL_ID',
);
if ( 'mute' == $argv['1'] ) {
$channels = array_merge( $channels, $after_hours_mute );
}
mute( implode( ',', $channels ) );
}
function mute( $channels ) {
$ch = curl_init( 'https://slack.com/api/users.prefs.set' );
$data = http_build_query([
'token' => 'YOUR_PERSONAL_TOKEN', // from: https://api.slack.com/custom-integrations/legacy-tokens
'prefs' => json_encode( array( 'muted_channels' => $channels ) ),
]);
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
$result = curl_exec( $ch );
curl_close( $ch);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment