https://developers.line.biz/en/
https://developers.line.biz/console/profile
https://manager.line.biz/
http://www.unzeen.com/article/3506/
https://developers.line.biz/en/reference/messaging-api/
http://www.thaiall.com/line/sticker_list.pdf
https://medium.com/@nattaponsirikamonnet/%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87-line-bot-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-group-room-48eac871ad90
https://medium.com/@nattaponsirikamonnet/%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87-bot-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-line-messaging-api-d7de644ac892
https://medium.com/linedevth/%E0%B8%A1%E0%B8%B2%E0%B9%80%E0%B8%82%E0%B8%B5%E0%B8%A2%E0%B8%99-line-bot-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-php-%E0%B8%81%E0%B8%B1%E0%B8%99-ep-1-796d70bd40f5
https://medium.com/@nattaponsirikamonnet/%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87-line-bot-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-push-message-a4566f65a3d2
https://developers.line.biz/en/
Messaging API
Login with Line Account
พบ Welcome to the LINE Developers console.
Enter your information and select Create my account.
You can still change your developer information later.
> Create my account (thaiall)
> Create a new Provider (thaialldotcom)
> Create a Messanging API Channel
- Channel type = Messanging API
- Provider = thaialldotcom
- Channel icon = rspthaiall.png
- Channel name = thaiallbot
- Category = การศึกษา
- Email address = burin_ruj@mysite.com
ใน Messaging API settings ของ Channel
พบ QR Code และ Bot basic ID = @xxxxxx
Available APIs
- REPLY_MESSAGE
- PUSH_MESSAGE
Webhook settings
Webhook URL = https://goo.gl/72BPC (web2)
Webhook URL = http://myapp630306.herokuapp.com/linebot.php
https://page.line.me/362lmjyg
https://page.line.biz/
http://myapp630306.herokuapp.com/linebot.php (your app)
heroku login
git clone https://git.heroku.com/xxx.git
cd xxx
git add .
git commit -m "hello"
git push origin master
http://myapp630306.herokuapp.com/linebot.php
<?php
// copy [Channel access token] from Messaging API
$myid = ""; // thaiallbot (ต้องเปลี่ยน)
$groupid =""; // groupchat (ต้องเปลี่ยน)
$accessToken = "xxxxxxxxxxxx";
$content = file_get_contents('php://input');
$arrayJson = json_decode($content, true);
$arrayHeader = array();
$arrayHeader[] = "Content-Type: application/json";
$arrayHeader[] = "Authorization: Bearer {$accessToken}";
$message = $arrayJson['events'][0]['message']['text'];
$grouptype = ""; $roomtype = ""; $usertype = ""; $room =""; $group ="";
if(isset($arrayJson['events'][0]['source']['groupId'])){
$id = $arrayJson['events'][0]['source']['groupId'];
$group = $arrayJson['events'][0]['source']['groupId'];
$grouptype = "group";
}
if(isset($arrayJson['events'][0]['source']['room'])){
$id = $arrayJson['events'][0]['source']['room'];
$room = $arrayJson['events'][0]['source']['room'];
$roomtype = "room";
}
if(isset($arrayJson['events'][0]['source']['userId'])){
$id = $arrayJson['events'][0]['source']['userId'];
$usertype = "user";
}
// == ถ้าเรียกผ่าน url จะสั่งส่ง push ถึง $myid และแสดงลิงค์ add thaiallbot ==
if(isset($_GET["time"]) && $_GET["time"] == "now") $id = $myid;
// === ถ้า add thaiallbot เข้ากลุ่มไหน ก็จะ reply ตาม message ==
if($message == "สวัสดี" || strtolower($message) == "hello" || $message == "hi"){
$arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
$arrayPostData['messages'][0]['type'] = "text";
$arrayPostData['messages'][0]['text'] = "สวัสดีครับ สบายดีรึเปล่า";
replyMsg($arrayHeader,$arrayPostData);
} else if($message == "สวัสดีครับ"){
$arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
$arrayPostData['messages'][0]['type'] = "text";
$arrayPostData['messages'][0]['text'] = "ยินดีต้อนรับ มีอะไรคุยกันได้นะ";
replyMsg($arrayHeader,$arrayPostData);
} else if(strtolower($message) == "id" || strtolower($message) == "userid"){
$arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
$arrayPostData['messages'][0]['type'] = "text";
$arrayPostData['messages'][0]['text'] = "userId = $id";
replyMsg($arrayHeader,$arrayPostData);
}
// ===
// เห็น Cony ถือป้าย "okay" ใน line แสดงว่ามาจากส่วนนี้
// https://developers.line.biz/media/messaging-api/sticker_list.pdf
if(strlen($id) > 1 && false) { // เติม false หยุดการตอบ sticker ทันที
$arrayPostData['to'] = $id;
$arrayPostData['messages'][0]['type'] = "sticker";
$arrayPostData['messages'][0]['packageId'] = 11537;
$arrayPostData['messages'][0]['stickerId'] = 52002740; // ชูป้าย Okay
pushMsg($arrayHeader,$arrayPostData);
}
// === ทำทุกครั้ง ถ้ากำหนด $myid หรือ $groupid ===
// ส่วนนี้ทำทุกครั้ง ส่งข้อความ Text เข้าบัญชี $myid ถ้าไม่กำหนด $myid ก็จะไม่ส่ง
if(strlen($myid) > 0) {
$arrayPostData['to'] = $myid; // ส่งเข้า thaiallbot ทุกครั้ง
$arrayPostData['messages'][0]['type'] = "text";
$data = "$usertype : $id ";
if(strlen($room) > 0) $data .= "$roomtype : $room ";
if(strlen($group) > 0) $data .= "$grouptype : $group ";
$arrayPostData['messages'][0]['text'] = $data;
pushMsg($arrayHeader,$arrayPostData);
}
// ส่วนนี้ทำทุกครั้ง ส่ง Sticker เข้าบัญชี $groupid (groupchat) ถ้าไม่กำหนด $groupid ก็จะไม่ส่ง
if(strlen($groupid) > 0 && $message == "boy") {
$arrayPostData['to'] = $groupid; // ส่งเข้า groupchat ทุกครั้ง
$arrayPostData['messages'][0]['type'] = "sticker";
$arrayPostData['messages'][0]['packageId'] = 11537;
$arrayPostData['messages'][0]['stickerId'] = 52002739; // Brown คำนับ
pushMsg($arrayHeader,$arrayPostData);
}
// === function replyMsg กับ pushMsg ===
function replyMsg($arrayHeader,$arrayPostData){
$strUrl = "https://api.line.me/v2/bot/message/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 pushMsg($arrayHeader,$arrayPostData){
$strUrl = "https://api.line.me/v2/bot/message/push";
$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);
}
// ===
if(isset($_GET["time"])) echo date('H:i:s', time()) . "<br><a href='https://line.me/R/ti/p/%40362lmjyg'>Add friend : thaiallbot เพื่อทดสอบ แล้วพิมพ์ id</a>";
/* ถ้าเรียก url ข้างล่างนี้จะส่ง sticker เข้า $myid
http://myapp630306.herokuapp.com/linebot.php?time=now */
?>