Last active
May 18, 2019 16:12
-
-
Save dictcp/130cf799980fa08ad5bcc349f5983aed to your computer and use it in GitHub Desktop.
Get the list of the Slack channel member who are the guests
This file contains 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 | |
// | |
// Usage: | |
// php slack-get-channels-guest.php | jq '.[] | [ .name, (.member[] | .name) ] | flatten | @csv' -r | pbcopy | |
// | |
$TOKEN = getenv('SLACK_TOKEN'); | |
$user_json = json_decode(file_get_contents("https://slack.com/api/users.list?token=$TOKEN&pretty=1"), true); | |
$guest_list=array_filter(array_map(function($x){ | |
if ((isset($x['is_restricted']) && $x['is_restricted'] == true) || | |
(isset($x['is_ultra_restricted']) && $x['is_ultra_restricted'] == true) | |
) { | |
return [ | |
'id'=> $x['id'], 'value' => ['id'=> $x['id'], 'title'=> $x['profile']['title'], 'name'=> $x['profile']['real_name']] | |
]; | |
} else { | |
return null; | |
} | |
}, $user_json['members'])); | |
$guest_index = []; | |
foreach ($guest_list as $guest_item) { | |
$guest_index[$guest_item['id']] = $guest_item['value']; | |
} | |
$channel_json = json_decode(file_get_contents("https://slack.com/api/conversations.list?token=$TOKEN&limit=1000&pretty=1"), true); | |
$channel_list = array_filter(array_map(function($x){ | |
if ($x['is_archived'] != true) { | |
return [ | |
'id'=> $x['id'], | |
'name' => $x['name'], | |
'topic' => $x['topic']['value'], | |
'purpose' => $x['purpose']['value'], | |
]; | |
} else { | |
return null; | |
} | |
}, $channel_json['channels'])); | |
$channels_member_list = []; | |
foreach($channel_list as $channel_item) { | |
$channel_id = $channel_item['id']; | |
$channel_name = $channel_item['name']; | |
$channel_member_json = json_decode(file_get_contents("https://slack.com/api/conversations.members?token=$TOKEN&channel=$channel_id&limit=1000&pretty=1"), true); | |
$channel_member_list = array_filter(array_map(function($x) use($guest_index){ | |
if (array_key_exists($x, $guest_index)) { | |
return $guest_index[$x]; | |
} else { | |
return null; | |
} | |
}, $channel_member_json['members'])); | |
$channels_member_list[] = [ | |
'id' => $channel_id, | |
'name' => $channel_name, | |
'member' => $channel_member_list, | |
]; | |
} | |
echo json_encode($channels_member_list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment