Last active
June 12, 2023 05:18
-
-
Save kasparsd/f8f36794ecf558915d6f to your computer and use it in GitHub Desktop.
Get user timezones using Slack API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html ng-app="timezoneApp"> | |
<head> | |
<title>Team Timezones</title> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> | |
<script> | |
var period = 172800; // 2 days in seconds | |
var now = new Date(); | |
var utc = new Date( now.getTime() + now.getTimezoneOffset() * 60000 ); | |
var seconds_today = utc.getSeconds() + ( 60 * ( utc.getMinutes() + ( 60 * utc.getHours() ) ) ); | |
var timezoneApp = angular.module( 'timezoneApp', [] ); | |
timezoneApp.controller( 'ctrl', function( $scope, $http ) { | |
$scope.users = []; | |
$scope.marks = []; | |
hours = [ 12 ]; | |
for ( h = 1; h <= 12; h++ ) | |
hours.push( h ); | |
for ( h = 1; h <= 12; h++ ) | |
hours.push( h ); | |
for ( h = 1; h <= 12; h++ ) | |
hours.push( h ); | |
for ( h = 1; h <= 11; h++ ) | |
hours.push( h ); | |
for ( hour in hours ) | |
$scope.marks.push({ mark: hours[ hour ] }); | |
$http.get( 'user-timezone.json' ).then( function( res ) { | |
var usertime; | |
// Convert user time to relative position | |
for ( user in res.data ) { | |
usertime = seconds_today + res.data[ user ].tz_offset; | |
if ( usertime > 86400 ) | |
res.data[ user ].alt = 'alt'; | |
else | |
res.data[ user ].alt = ''; | |
res.data[ user ].pos = 25 + usertime / period * 100; | |
} | |
$scope.users = res.data; | |
}); | |
}); | |
</script> | |
<style> | |
body { margin:0; padding:2em 0 0 0; font: 16px/1.5 sans-serif; min-width:800px; } | |
ul, li { display:block; margin:0; padding:0; list-style:none; float:left; width:100%; clear:both; } | |
li { height:1.5em; margin:0; background:#eee; position:relative; } | |
.time-bar { display:block; float:left; position:relative; height:1em; padding:0.25em 0; } | |
.yesterday { width:25%; } | |
.tomorrow { width:25%; } | |
.today { width:50%; background:#fff; } /* 8am = 33.33%, 9h = 37.5% */ | |
.users .today:before { position:absolute; left:33.33%; top:0; bottom:0; width:37.5%; height:1.5em; background:#389ece; content:''; display:block; } | |
.users .tomorrow:before { position:absolute; left:66.6%; top:0; bottom:0; width:33.4%; height:1.5em; background:#389ece; content:''; display:block; } | |
.users .yesterday:before { position:absolute; left:0; top:0; bottom:0; width:50%; height:1.5em; background:#389ece; content:''; display:block; } | |
.pos { position:absolute; bottom:0; padding-left:0.5em; } | |
.pos:before { float:left; position:absolute; bottom:0; left:0; display:block; content:''; height:1.5em; width:2px; background:#000; } | |
.labels li { height:auto; } | |
.labels span { font-size:0.9em; padding:1em 0; color:#999; } | |
.marks { float:left; width:100%; clear:both; position:fixed; z-index:10; top:0; width:100%; margin-right:-1em; } | |
.marks li { color:#999; font-size:0.8em; float:left; width:2.08333%; padding:0.5em 0; clear:none; background:#fff; margin-left:-1px; border-left:1px solid #ddd; } | |
</style> | |
</head> | |
<body ng-controller="ctrl"> | |
<ul class="marks"> | |
<li ng-repeat="mark in marks"> | |
<span> {{ mark.mark }}</span> | |
</li> | |
</ul> | |
<ul class="labels"> | |
<li> | |
<span class="time-bar yesterday"> Yesterday</span> | |
<span class="time-bar today"> Today</span> | |
<span class="time-bar tomorrow"> Tomorrow</span> | |
</li> | |
</ul> | |
<ul class="users"> | |
<li ng-repeat="user in users"> | |
<span class="time-bar yesterday"></span> | |
<span class="time-bar today"></span> | |
<span class="time-bar tomorrow"></span> | |
<div class="pos" style="left:{{ user.pos }}%" ng-class="user.alt"> | |
<span>{{user.name}}</span> | |
</div> | |
</li> | |
</ul> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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