Last active
August 29, 2015 14:16
-
-
Save mikestreety/99dccfa1955144eab0ac to your computer and use it in GitHub Desktop.
Get a list of your favourits from slack
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 | |
function makeClickableLinks($s) { | |
$s = str_replace('<', '', $s); | |
$s = str_replace('>', '', $s); | |
return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s); | |
} | |
include 'Slack.php'; | |
// Head to http://api.slack.com and (if you haven't already) scroll down and click the Create Token link | |
$Slack = new Slack('Create Token'); | |
$params = array( | |
'query' => 'has:star', | |
'count' => 100000, | |
'sort_dir' => 'asc' | |
); | |
$data = $Slack->call('search.messages', $params); | |
$links = array(); | |
foreach(range(1, $data['messages']['pagination']['page_count']) as $i) { | |
$data = $Slack->call('search.messages', array_merge($params, array('page' => $i))); | |
$links = array_merge($links, $data['messages']['matches']); | |
} | |
?> | |
<html> | |
<head> | |
<style> | |
body { | |
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; | |
font-weight: 300; | |
font-size: 16px; | |
margin: 0; | |
padding: 0; | |
} | |
table { | |
border-collapse: collapse; | |
} | |
td { | |
border: 1px solid #ccc; | |
border-width: 1px 0; | |
padding: 15px; | |
line-height: 1.4; | |
} | |
a { | |
color: #2392CB; | |
-webkit-transition: color ease 0.2s; | |
-o-transition: color ease 0.2s; | |
transition: color ease 0.2s; | |
} | |
h1 { | |
margin: 20px; | |
padding: 0; | |
} | |
.date { | |
white-space: nowrap; | |
border-left: 1px solid #ddd; | |
padding-left: 15px; | |
} | |
.date a { | |
color: rgba(35, 146, 203, 0.5); | |
} | |
.date a:hover { | |
color: rgba(35, 146, 203, 1); | |
} | |
.channel { | |
color: #999; | |
} | |
.who { | |
font-weight: bold; | |
border-right: 1px solid #ddd; | |
} | |
</style> | |
</head> | |
<body> | |
<table> | |
<?php foreach($links as $link) : | |
$date = explode('.', $link['ts']); ?> | |
<tr> | |
<td class="who"><?=$link['username']?><?=($link['user'])? '' : ' (bot)'?></td><td><?=makeClickableLinks($link['text'])?></td> | |
<td class="channel">#<?=$link['channel']['name']?></td> | |
<td class="date"><a href="<?=$link['permalink']?>"><?=date('j M', $date[0])?></a></td> | |
</tr> | |
<?php endforeach; ?> | |
</table> | |
</body> | |
</html> |
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 | |
/** | |
* Simple abstraction of Slack API | |
* | |
* Uses curl, if not falls back to file_get_contents and HTTP stream. | |
* | |
* For all api methods, refer to https://api.slack.com/ | |
* | |
* @author Yong Zhen <[email protected]> | |
* @version 1.0.0 | |
*/ | |
class Slack { | |
private $api_token; | |
private $api_endpoint = 'https://slack.com/api/<method>'; | |
/** | |
* Create a new instance | |
* @param string $api_token Your Slack api bearer token | |
*/ | |
function __construct($api_token){ | |
$this->api_token = $api_token; | |
} | |
/** | |
* Calls an API method. You don't have to pass in the token, it will automatically be included. | |
* @param string $method The API method to call. | |
* @param array $args An associative array of arguments to pass to the API. | |
* @param integer $timeout Set maximum time the request is allowed to take, in seconds. | |
* @return array The response as an associative array, JSON-decoded. | |
*/ | |
public function call($method, $args = array(), $timeout = 10){ | |
return $this->request($method, $args, $timeout); | |
} | |
/** | |
* Performs the underlying HTTP request. | |
* @param string $method The API method to call. | |
* @param array $args An associative array of arguments to pass to the API. | |
* @param integer $timeout Set maximum time the request is allowed to take, in seconds. | |
* @return array The response as an associative array, JSON-decoded. | |
*/ | |
private function request($method, $args = array(), $timeout = 10){ | |
$url = str_replace('<method>', $method, $this->api_endpoint); | |
$args['token'] = $this->api_token; | |
if (function_exists('curl_version')){ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $args); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
} else { | |
$post_data = http_build_query($args); | |
$result = file_get_contents($url, false, stream_context_create(array( | |
'http' => array( | |
'protocol_version' => 1.1, | |
'method' => 'POST', | |
'header' => "Content-type: application/x-www-form-urlencoded\r\n" . | |
"Content-length: " . strlen($post_data) . "\r\n" . | |
"Connection: close\r\n", | |
'content' => $post_data | |
), | |
))); | |
} | |
return $result ? json_decode($result, true) : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment