line bot ที่สามารถเก็บ ข้อมูลการคุย รูป ข้อความ vdo และ ไฟล์ต่างๆ ด้วย php curl
บนความนี้จะไม่ได้กล่าวถึง การ ลงทะเบียนทำ line bot เพราะน่าจะหาอ่านได้ ทั่วไปเยอะอยู่
เราจะมาว่าถึงตอนที่ได้ AccessToken ที่ใช้ในการ authen แล้ว ซึ่งจริงๆ มันapi ให้ไป get จะใช้ ajax หรือ curl ของ php ก็ได้ แต่เนื่องจาก server เป็น ตัวรับเลยใช้ php
https://developers.line.biz/en/reference/messaging-api/#send-push-message
Webhook URL ต้องเป็น url เท่านั้น ip ไม่ได้และ ต้อง https ca จริงเท่านั้น
ดังนั้นจึงขอแบ่งเป็น ส่วนๆ ใน code ดังนี้
ส่วนรับ ข้อความ
$content = file_get_contents(‘php://input’);
$arrJson = json_decode($content, true);
จะเป็นรูปแบบ json อันนี้ ลองใส่ db แล้วดูเต็มๆ ดู
หากต้องการรูป displayName และ รูป profile ใน group ตามนี้เลย
$url_content=”https://api.line.me/v2/bot/group/”.$groupId.”/member/”.$user_id;
$headers = array(‘Authorization: Bearer ‘ . $strAccessToken);
$ch = curl_init($url_content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data =curl_exec($ch);
curl_close($ch);
$arrJson_profile = json_decode($data, true);
$displayName=$arrJson_profile[‘displayName’];
$pictureUrl=$arrJson_profile[‘pictureUrl’];
แต่หากต้องการดูส่วนตัวหรือ ใน room จะต้องเปลี่ย url โดย room จาก group เป็น room
หากต้องการรูปจากข้อความ
$url_content=”https://api.line.me/v2/bot/message/”.$msg_id.”/content”;
$headers = array(‘Authorization: Bearer ‘ . $strAccessToken);
$ch = curl_init($url_content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data =curl_exec($ch);
curl_close($ch);
$fp = ‘img_file/’.$msg_id.’.png’;
$url_img=”http://103.40.151.6/line_bot_gts_issue/”.$fp;
file_put_contents( $fp, $data );
โดยมันจะส่งมาเป็น binary แต่ code นี้แปลงให้แล้ว แต่ติดปัญหาว่า ไม่ได้ชื่อไฟล์จริงๆ และ นามสกุลจริงๆ ได้ตามที่ใส่ไว้คือ png
หากเป็น vdo
$url_content=”https://api.line.me/v2/bot/message/”.$msg_id.”/content”;
$headers = array(‘Authorization: Bearer ‘ . $strAccessToken);
$ch = curl_init($url_content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data =curl_exec($ch);
curl_close($ch);
$fp = ‘vdo_file/’.$msg_id.’.mp4′;
$url_img=”http://103.40.151.6/line_bot_gts_issue/”.$fp;
file_put_contents( $fp, $data );
โดยมันจะส่งมาเป็น binary แต่ code นี้แปลงให้แล้ว แต่ติดปัญหาว่า ไม่ได้ชื่อไฟล์จริงๆ และ นามสกุลจริงๆ ได้ตามที่ใส่ไว้คือ mp4
หากเป็นไฟล์
$url_content=”https://api.line.me/v2/bot/message/”.$msg_id.”/content”;
$headers = array(‘Authorization: Bearer ‘ . $strAccessToken);
$ch = curl_init($url_content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data =curl_exec($ch);
curl_close($ch);
$fp = ‘files/’.$msg_id.’_’.$fileName;
$url_img=”http://103.40.151.6/line_bot_gts_issue/”.$fp;
file_put_contents( $fp, $data );
โดยมันจะส่งมาเป็น binary แต่ code นี้แปลงให้แล้ว โดยชื่อไฟล์มันมีมาให้ตอนได้รับข้อความตอนแรก เลยนำมาทำชื่อไฟล์ได้ถูก
ส่วนการส่งข้อความกลับนั้น น่าจะแนวๆนี้ เพราะไม่ได้ดูต่อ เนื่องจากมันส่งกลับได้หลายรูปแบบ
$strUrl = “https://api.line.me/v2/bot/message/reply”;
$arrHeader = array();
$arrHeader[] = “Content-Type: application/json”;
$arrHeader[] = “Authorization: Bearer {$strAccessToken}”;
$arrPostData = array();
$arrPostData[‘replyToken’] = $arrJson[‘events’][0][‘replyToken’];
$arrPostData[‘messages’][0][‘type’] = “text”;
$arrPostData[‘messages’][0][‘text’] = “กรุณา ติดต่อผู้ดูแลระบบ NMC เพื่อขอใช้งาน”;
$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, $arrHeader);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arrPostData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close ($ch);
ถ้ามีจะมาเพิ่มเติมอีก