Last active
August 29, 2015 14:11
-
-
Save s-kiriki/2df9ca0e787f91cb8ce3 to your computer and use it in GitHub Desktop.
Slack上のBotの貢献度を図るスクリプト
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 | |
if (!getenv(SLACK_API_TOKEN)) { | |
exit('You must set env SLACK_API_TOKEN'); | |
} | |
ini_set('date.timezone', 'Asia/Tokyo'); | |
{// You can change belows | |
$slackbot_name = 'wanko'; | |
$bot_cmd_arr = array('ote', 'deploy', 'merge', 'update ref', 'assign'); | |
$start_date = '2014-01-01'; | |
$end_date = '2014-12-26'; | |
} | |
$data = getMessage($slackbot_name); | |
$pageCnt = $data['pageCnt']; | |
$callCnt = 0; | |
$callCntPerCmd = array(); | |
$callCntPerUser = array(); | |
for ($page = 1; $page <= $pageCnt; $page++) { | |
print $page. '/'. $pageCnt. "\r"; | |
$data = getMessage($slackbot_name, $page); | |
$messages = $data['messages']; | |
if (!$messages) { | |
continue; | |
} | |
foreach ($messages as $message){ | |
//期間内かチェック | |
if ( $message['ts'] < strtotime($start_date) || $message['ts'] > strtotime($end_date) ) { | |
continue; | |
} | |
$isValid = false; | |
//対象コマンドかチェック | |
foreach ($bot_cmd_arr as $bot_cmd) { | |
if(preg_match("/^{$slackbot_name} {$bot_cmd}/", $message['text'])){ | |
$callCnt++; | |
$callCntPerCmd[$bot_cmd]++; | |
$callCntPerUser[$message['username']]++; | |
break; | |
} | |
} | |
} | |
} | |
arsort($callCntPerCmd); | |
arsort($callCntPerUser); | |
echo $callCnt. PHP_EOL; | |
echo "{$slackbot_name} contributed {$callCnt} times!!!". PHP_EOL; | |
echo PHP_EOL; | |
echo '[ Popular command ranking ]'. PHP_EOL; | |
foreach ($callCntPerCmd as $cmd => $cnt) { | |
echo "{$cmd} : {$cnt} times". PHP_EOL; | |
} | |
echo PHP_EOL; | |
echo PHP_EOL; | |
foreach ($callCntPerUser as $user => $cnt) { | |
echo "{$user} : {$cnt} times". PHP_EOL; | |
} | |
function getMessage($slackbot_name, $page = null){ | |
$url = 'https://slack.com/api/search.messages'; | |
$params = array( | |
'token' => getenv(SLACK_API_TOKEN), | |
'query' => $slackbot_name, | |
'sort' => 'timestamp' | |
); | |
if ($page) { | |
$params['page'] = $page; | |
} | |
$data = http_build_query($params); | |
$url = ($data != '')?$url.'?'.$data:$url; | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 2); | |
$res = curl_exec($ch); | |
$resArr = json_decode($res, true); | |
return array('pageCnt'=>$resArr['messages']['pagination']['page_count'], 'messages'=>$resArr['messages']['matches']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment