Last active
June 18, 2022 06:25
-
-
Save aeknarinamn/8ef80222cdf4f17daad0f7be5e73fbc7 to your computer and use it in GitHub Desktop.
LINE BOT EXAMPLE PHP EP1
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 | |
/*Get Data From POST Http Request*/ | |
$datas = file_get_contents('php://input'); | |
/*Decode Json From LINE Data Body*/ | |
$deCode = json_decode($datas,true); | |
file_put_contents('log.txt', file_get_contents('php://input') . PHP_EOL, FILE_APPEND); | |
$replyToken = $deCode['events'][0]['replyToken']; | |
$userId = $deCode['events'][0]['source']['userId']; | |
$text = $deCode['events'][0]['message']['text']; | |
$messages = []; | |
$messages['replyToken'] = $replyToken; | |
$messages['messages'][0] = getFormatTextMessage("เอ้ย ถามอะไรก็ตอบได้"); | |
$encodeJson = json_encode($messages); | |
$LINEDatas['url'] = "https://api.line.me/v2/bot/message/reply"; | |
$LINEDatas['token'] = "<YOUR-CHANNEL-ACCESS-TOKEN>"; | |
$results = sentMessage($encodeJson,$LINEDatas); | |
/*Return HTTP Request 200*/ | |
http_response_code(200); | |
function getFormatTextMessage($text) | |
{ | |
$datas = []; | |
$datas['type'] = 'text'; | |
$datas['text'] = $text; | |
return $datas; | |
} | |
function sentMessage($encodeJson,$datas) | |
{ | |
$datasReturn = []; | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => $datas['url'], | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => $encodeJson, | |
CURLOPT_HTTPHEADER => array( | |
"authorization: Bearer ".$datas['token'], | |
"cache-control: no-cache", | |
"content-type: application/json; charset=UTF-8", | |
), | |
)); | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
if ($err) { | |
$datasReturn['result'] = 'E'; | |
$datasReturn['message'] = $err; | |
} else { | |
if($response == "{}"){ | |
$datasReturn['result'] = 'S'; | |
$datasReturn['message'] = 'Success'; | |
}else{ | |
$datasReturn['result'] = 'E'; | |
$datasReturn['message'] = $response; | |
} | |
} | |
return $datasReturn; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment