Skip to content

Instantly share code, notes, and snippets.

@ktakayama
Created April 2, 2015 02:28
Show Gist options
  • Save ktakayama/8ac790b15f319fdd8668 to your computer and use it in GitHub Desktop.
Save ktakayama/8ac790b15f319fdd8668 to your computer and use it in GitHub Desktop.
King of Time to slack
<?
$SLACK_NAMES = array(
'田中 太郎' => '@tanaka',
'高山 二郎' => '@takayama',
'木村 三郎' => '@kimura'
);
define('ACCESS_TOKEN', 'YOUR ACCESS TOKEN');
define('API_POST_MESSAGE_URL', 'https://slack.com/api/chat.postMessage');
define('BOT_NAME', 'slackun');
define('POST_CHANNEL', 'general');
define('KOT_BASE_URL', 'YOUR TIME RECORDER URL');
?>
<?php
require_once 'simple_html_dom.php'; // (http://sourceforge.net/projects/simplehtmldom/)
require_once 'config.inc';
ini_set("date.timezone", "Asia/Tokyo");
check_yesterday();
check_today();
function check_yesterday() {
$date = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
$results = check_record($date);
$in_names = $results[0];
$out_names = $results[1];
$message = "";
if(count($in_names) > 0) {
$message .= join($in_names, " ") . " ";
$message .= date("d", $date);
$message .= "日の出勤を押していないようです";
$message .= "\n";
}
if(count($out_names) > 0) {
$message .= join($out_names, " ") . " ";
$message .= date("d", $date);
$message .= "日の退勤を押していないようです";
$message .= "\n";
}
if(strlen($message) > 0) {
post_message( $message );
}
}
function check_today() {
$date = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$results = check_record($date);
$in_names = $results[0];
$message = "";
if(count($in_names) > 0) {
$message .= join($in_names, " ") . " ";
$message .= date("d", $date);
$message .= "日の出勤を押していないようです(退勤が押してあります)";
$message .= "\n";
}
if(strlen($message) > 0) {
post_message( $message );
}
}
function check_record($date) {
global $SLACK_NAMES;
$records = get_record($date);
$in_names = array();
$out_names = array();
foreach ($records as $record) {
if(isHoliday($record)) {
continue;
}
if(!$record["inTime"] && !$record["outTime"]) {
continue;
}
if(!$record["inTime"] && !(isAMHome($record))) {
# 出勤時間が抜けている場合(AM在宅の場合はOK)
array_push($in_names, $SLACK_NAMES[$record["name"]]);
}
if(!$record["outTime"] && !(isPMHome($record))) {
# 退勤時間が抜けている場合(PM在宅の場合はOK)
array_push($out_names, $SLACK_NAMES[$record["name"]]);
}
}
return array( $in_names, $out_names );
}
function post_message($message) {
$url = API_POST_MESSAGE_URL;
$data = array(
'channel' => POST_CHANNEL,
'token' => ACCESS_TOKEN,
'username' => BOT_NAME,
'text' => $message,
'link_names' => '1',
);
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data),
));
file_get_contents($url, false, stream_context_create($options));
}
# King of Timeのページからスクレイピングしてデータを取得
function get_record($date) {
$day = date("Ymd", $date);
$url = KOT_BASE_URL . $day;
#$url = "./data.sample";
$html = file_get_html( $url );
$table = $html->find( 'table', 1 );
$accounts = $table->find( 'tr' );
$results = array();
foreach($accounts as $account) {
$td = $account->find( 'td', 1 );
$name = get_name($td->plaintext);
if($name == "invalid") continue;
$td = $account->find( 'td', 3 );
$schedule = $td->plaintext;
$inTime = get_date($account->find( 'td > div', 0 )->plaintext);
$outTime = get_date($account->find( 'td > div', 1 )->plaintext);
array_push($results, array(
"name" => $name,
"schedule" => $schedule,
"inTime" => $inTime,
"outTime" => $outTime
));
}
return $results;
}
function get_name($str) {
preg_match('/\d{6} (.*)/', $str, $matches, PREG_OFFSET_CAPTURE);
if(count($matches) > 0) {
return trim($matches[1][0]);
} else {
return "invalid";
}
}
function get_date($str) {
preg_match('/\d{2}\/\d{2} \d{2}:\d{2}/', $str, $matches, PREG_OFFSET_CAPTURE);
if(count($matches) > 0) {
return $matches[0][0];
} else {
return "";
}
}
function isHoliday($record) {
return (strstr($record["schedule"], '休暇'));
}
function isAMHome($record) {
return (
strstr($record["schedule"], '直行') or
strstr($record["schedule"], 'AM在宅勤務') or
strstr($record["schedule"], 'AM欠勤')
);
}
function isPMHome($record) {
return (
strstr($record["schedule"], '直帰') or
strstr($record["schedule"], 'PM在宅勤務') or
strstr($record["schedule"], 'PM欠勤')
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment