Skip to content

Instantly share code, notes, and snippets.

@shandanjay
Forked from kasparsd/index.html
Created May 17, 2016 04:19
Show Gist options
  • Save shandanjay/677b6fe34e343405f6061a586aaa17b4 to your computer and use it in GitHub Desktop.
Save shandanjay/677b6fe34e343405f6061a586aaa17b4 to your computer and use it in GitHub Desktop.
Get user timezones using Slack API
<?php
$api_token = 'slack-api-token';
if ( file_exists( 'users.json' ) ) {
$api_users = file_get_contents( 'users.json' );
} else {
$api_users = file_get_contents( 'https://slack.com/api/users.list?token=' . $api_token );
if ( false === $api_users ) {
die( "Failed to retrieve the list of users:\n" . print_r( $http_response_header, true ) );
}
file_put_contents( 'users.json', $api_users );
}
$users = json_decode( $api_users );
$user_tz = array();
$users_missing_tx = array();
foreach ( $users->members as $member ) {
if ( $member->deleted || $member->is_restricted || $member->is_ultra_restricted || $member->is_bot )
continue;
if ( isset( $member->tz_label ) ) {
$user_tz[] = array(
'username' => $member->name,
'name' => $member->real_name,
'email' => $member->profile->email,
'tz_label' => $member->tz_label,
'tz_offset' => $member->tz_offset,
);
} else {
$users_missing_tx[] = $member;
}
}
usort( $user_tz, function( $a, $b ) {
return $a['tz_offset'] - $b['tz_offset'];
});
file_put_contents( 'user-timezone.json', json_encode( $user_tz ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment