February 26, 2018
Membuat Remote Server Linux menggunakan Line Chat – Setup Local Server
seperti yang telah kita bahas sebelumnya pada pembuatan Remote Server untuk Facebook, pada bagian ini hampir mirip dengan sebelumnya, tetapi Line lebih mudah untuk dibuat karena tidak mengharuskan untuk menghasilkan result tertentu (Facebook Chalenge Test), yang penting bisa meresponse sukses (kode 200) maka sudah dianggap benar oleh sistem Line.
tentu saja tidak banyak yang saya bahas disini karena memang yang kita kerjakan tak lebih hanya sekedat membuat sebuah source code untuk memastikan Request Line dapat ditangkap oleh Local Server dengan baik. saya akan bagi menjadi 3 file untuk hal ini agar nantinya bisa dikembangkan sendiri-sendiri secara lebih baik. yang pertama adalah file LineBot.php dan index.php. pada LineBot.php ini adalah sebuah class yang mana dibuat untuk memudahkan user dalam melakukan access perintah-perintah bot dalam line sehingga lebih mudah untuk di akses dan dikembangkan ulang. berikut ini adalah isi dari file LineBot.php.
<?php class LineBot { private $token; private $secret; private $response; private $e_object; private $url_reply; private $url_push; public function __construct(){ $this->token = "ACCES TOKEN DISINI"; $this->secret = "SECRET TOKEN DISINI"; $this->url_reply = "https://api.line.me/v2/bot/message/reply"; $this->url_push = "https://api.line.me/v2/bot/message/push"; $this->response = file_get_contents('php://input'); $this->e_object = json_decode($this->response); } private function httpPost($api,$body){ $ch = curl_init($api); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charser=UTF-8', 'Authorization: Bearer '.$this->token)); $result = curl_exec($ch); curl_close($ch); return $result; } public function response($text){ $object = $this->e_object; $token_reply = $object->{"events"}[0]->{"replyToken"}; $body["replyToken"] = $token_reply; $body["messages"][0] = array(); $body["messages"][0]['type'] = "text"; $body["messages"][0]['text'] = $text; $result = $this->httpPost($this->url_reply,$body); return $result; } public function getText(){ $object = $this->e_object; $txt = $object->{"events"}[0]->{"message"}->{"text"}; return $txt; } public function getEvent(){ $object = $this->e_object; $postback = $object->{"events"}[0]->{"postback"}->{"data"}; return $postback; } public function getIDUser(){ $object = $this->e_object; $id = $object->{"events"}[0]->{"source"}->{"userId"}; return $id; } }
pada file LineBot.php terdapat beberapa hal yang harus disesuaikan yakni ACCESS TOKEN dan SECRET TOKEN sesuaikan dengan milik anda seperti pada tulisan sebelumnya ( Membuat Remote Server Linux menggunakan Line Chat – Membuat Channel ) adalah file index.php yang mana file ini adalah file yang menjalankan seluruh perintah dalam LineBot.php, file index.php akan memanfaatkan file LineBot.php untuk mengakses perintah, dan LineBot. berikut ini adalah file index.php.
<?php require_once __DIR__ . '/LineBot.php'; try{ $body = file_get_contents('php://input'); if($body!=null){ $bot = new LineBot(); $text = $bot->getText(); $bot->response("CopyCat -> ".$text); } }catch(Exception $e){ echo $e->getMessage(); } ?>
masukan ketiga file tersebut dalam satu folder, sebagai contoh saya menggunakan folder linebot sehingga kalau saya akses dari localhost milik saya ada di https://localhost/linebot/.
setelah seluruhnya selesai maka tahap untuk setup selanjutnya adalah memasang ngrok, karena Line sama seperti Facebook Messengger dia membutuhkan akses HTTPS untuk mau berkomunikasi. untuk setting dan setup ngrok akan kita bahas pada tulisan berikutnya. untuk source code dapat di download di Dropbox atau Google Drive.