Skip to content

Instantly share code, notes, and snippets.

@takuya-andou
Created October 6, 2016 02:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takuya-andou/ffcb0b2e28cde2e6fb882eafa7e74883 to your computer and use it in GitHub Desktop.
Save takuya-andou/ffcb0b2e28cde2e6fb882eafa7e74883 to your computer and use it in GitHub Desktop.
LineビーコンとLineMessagingAPIによる勤怠管理
<?php
$json_string = file_get_contents('php://input');
$json_object = json_decode($json_string);
$AndoUserID = '<<LineUserID>>';
error_log("callback start.");
foreach ($json_object->events as $event) {
if('message' == $event->type){
if ($event->source->userId == $AndoUserID) {
if ($event->message->text =='おは'){
MessageToSlack('おはようございます。出勤を記録しました。');
timecard();
api_post_request($event->replyToken, '出勤を記録しました。');
}
if ($event->message->text =='おつ'){
MessageToSlack('お疲れ様でした。退勤を記録しました。');
timecard();
api_post_request($event->replyToken, '退勤を記録しました。');
}
else{
api_post_request($event->replyToken, $event->message->text);
}
}
else{
api_post_request($event->replyToken, $event->message->text);
}
}else if('beacon' == $event->type){
if ($event->source->userId == $AndoUserID) {
api_post_request($event->replyToken, 'あんどうさんこんにちは');
MessageToSlack('おはようございます。');
timecard();
}
else{
api_post_request($event->replyToken, 'BEACONイベント!!');
}
}
}
function api_post_request($token, $message) {
$url = 'https://api.line.me/v2/bot/message/reply';
$channel_access_token = '<<channel_access_token>>';
$headers = array(
'Content-Type: application/json',
"Authorization: Bearer {$channel_access_token}"
);
$post = array(
'replyToken' => $token,
'messages' => array(
array(
'type' => 'text',
'text' => $message
)
)
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_exec($curl);
}
//GroupSessionへの打刻
function timecard(){
$URL = "https://<<ドメイン>>/api/timecard/dakoku.do?";
$username = "<<GroupSessionID>>";
$password = "<<GroupSessionPASS>>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
"Authorization: Basic " . base64_encode($username . ":" . $password)
));
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$buf = curl_exec($ch);
var_dump($buf);
curl_close($ch);
}
//Slackへのメッセージ
function MessageToSlack($text){
$slackApiKey = '<<slackAPIkey>>'; //APIキー
$text = urlencode($text);
$url = "https://slack.com/api/chat.postMessage?token=${slackApiKey}&channel=%23general&text=${text}&as_user=true";
file_get_contents($url);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment