<?php
//exit();
$accessToken = 'uaxVj5dbj19gRkYGPdJ0ZNraQh5y2bdndHY3hLXM5qwd3d625n+mBlZzQ+x73sp+uHv0ROby9Wr2ySS76NuGxBPT4yH7kLVIid4Sex3zTVSPOPZbimNuKXxnmE3lCZBsA+iP1X7UaOOewOUG6+u0dAdB04t89/1O/w1cDnyilFU=';
//$accessToken = 'AptU7r0r1pPOB9RMtH7z5JoxsZxsGcM6OsratBQLcmQ3sSefY8z3xg6xsWj7ce1I0mUKXXUs/kvSvSPPNEnlbWGr3UDFo5GXwjGkVbF3SG4vBQCuSdeVR4BuR2W4acHnGsMZ65PPVq5XxzSwoYrdDAdB04t89/1O/w1cDnyilFU=' ;
$content = file_get_contents('php://input');
$arrJson = json_decode($content, true);
$arrayHeader = array();
$arrayHeader[] = "Content-Type: application/json";
$arrayHeader[] = "Authorization: Bearer {$accessToken}";
$userId = $arrJson['events'][0]['source']['userId'];
//$content_type = $arrJson['events'][0]['message']['type']; //['text','sticker','location','image','audio','video']
$user_profile = json_decode(LineGetUserProfile($userId,$arrayHeader)) ;
$arrayPostData = array();
$arrayPostData['replyToken'] = $arrJson['events'][0]['replyToken'];
if($arrJson['events'][0]['message']['text'] == "ผู้บริหาร") {
$arrayPostData['messages'][0]['type'] = "text";
$arrayPostData['messages'][0]['text'] = "http://smart2.ska2.go.th/drirector";
} else if ($arrJson['events'][0]['message']['text'] == "ติดต่อเรา") {
$arrayPostData['messages'][0]['type'] = "text";
$arrayPostData['messages'][0]['text'] = "http://smart2.ska2.go.th/customer";
} else if ($arrJson['events'][0]['message']['text'] == "e-Service") {
$arrayPostData['messages'][0]['type'] = "text";
$arrayPostData['messages'][0]['text'] = "http://smart2.ska2.go.th/e-service";
} else if ($arrJson['events'][0]['message']['text'] == "FB สพป.นค.2") {
$arrayPostData['messages'][0]['type'] = "text";
$arrayPostData['messages'][0]['text'] = "https://www.facebook.com/yst1/";
} else {
$arrayPostData['messages'][0] = replyNonUser($user_profile->displayName);
}
LineReplyMsg($arrayHeader,$arrayPostData);
//function
function LineReplyMsg($arrayHeader,$arrayPostData){
$strUrl = "https://line.myschool.in.th/notiapi/reply";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$strUrl);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrayHeader);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arrayPostData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close ($ch);
}
function LineGetUserProfile($userId,$arrayHeader){
$url = 'https://line.myschool.in.th/notiapi/profile/'.$userId;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrayHeader);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
#$displayName = $someArray->displayName;
#userId: ID ของผู้ใช้(สำหรับระบบหลังบ้าน)
#displayName: ชื่อของผู้ใช้
#statusMessage: ข้อความของผู้ใช้
#pictureUrl: รูปของผู้ใช้
#email: อีเมลของผู้ใช้(เฉพาะ LIFF v2 และ LINE Login)
}
function replyNonUser($displayName)
{
$json = '
{
"type": "flex",
"altText": "ยินดีต้อนรับเข้าสู่ สพป.ยส.1 Service",
"contents": {
"type": "bubble",
"direction": "ltr",
"header": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "สวัสดี คุณ '.$displayName.' ",
"align": "center",
"size": "lg",
"weight": "bold",
"color": "#009813"
},
{
"type": "text",
"text": "ยินดีต้อนรับสู่การใช้งาน",
"weight": "bold",
"align": "center"
},
{
"type": "text",
"text": "Line Bot YST1",
"weight": "bold",
"align": "center"
}
]
},
"hero": {
"type": "image",
"url": "https://web.nongkhai2.go.th/Linebot/naga-bg.png",
"size": "full",
"aspectRatio": "1.51:1",
"aspectMode": "fit"
},
"footer": {
"type": "box",
"layout": "horizontal",
"contents": [
{
"type": "button",
"style": "primary",
"color": "#805500",
"action": {
"type": "uri",
"label": "เข้าใช้งานระบบ",
"uri": "https://liff.line.me/1660827893-Yvv0Da6d"
}
}
]
}
},
"quickReply": {
"items": [
{
"type": "action",
"action": {
"type": "message",
"label": "ผู้บริหาร",
"text": "ผู้บริหาร"
}
},
{
"type": "action",
"action": {
"type": "message",
"label": "ติดต่อ สพป.นค.2",
"text": "ติดต่อเรา"
}
},
{
"type": "action",
"action": {
"type": "message",
"label": "e-Service",
"text": "e-Service"
}
},
{
"type": "action",
"action": {
"type": "message",
"label": "FB สพป.ยส.1",
"text": "FB สพป.ยส.1"
}
}
]
}
}
' ;
$jsonFlex = json_decode($json, true);
return $jsonFlex;
}
?>