-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.php
71 lines (68 loc) · 3.67 KB
/
bot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
$accessToken = "S2B0THWttmRr+2XdO05WlDLvk45/JK29c5ma9CmxWWxBkp/2+W694OkNovA6hVTyQ+V7Jn+q9wkHhw97exxyInfCjgfLggLDnvm4xDQR788FwfCLrl9PzQBGzxko0P0AZrc0eOQg/2kvacXSRlcjyAdB04t89/1O/w1cDnyilFU=";//copy Channel access token ตอนที่ตั้งค่ามาใส่
$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'];
#ตัวอย่าง Message Type "Text"
if($message == "สวัสดี"){
$arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
$arrayPostData['messages'][0]['type'] = "text";
$arrayPostData['messages'][0]['text'] = "สวัสดีจ้าาา";
replyMsg($arrayHeader,$arrayPostData);
}
#ตัวอย่าง Message Type "Sticker"
else if($message == "ฝันดี"){
$arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
$arrayPostData['messages'][0]['type'] = "sticker";
$arrayPostData['messages'][0]['packageId'] = "2";
$arrayPostData['messages'][0]['stickerId'] = "46";
replyMsg($arrayHeader,$arrayPostData);
}
#ตัวอย่าง Message Type "Image"
else if($message == "รูปน้องแมว"){
$image_url = "https://i.pinimg.com/originals/cc/22/d1/cc22d10d9096e70fe3dbe3be2630182b.jpg";
$arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
$arrayPostData['messages'][0]['type'] = "image";
$arrayPostData['messages'][0]['originalContentUrl'] = $image_url;
$arrayPostData['messages'][0]['previewImageUrl'] = $image_url;
replyMsg($arrayHeader,$arrayPostData);
}
#ตัวอย่าง Message Type "Location"
else if($message == "พิกัดสยามพารากอน"){
$arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
$arrayPostData['messages'][0]['type'] = "location";
$arrayPostData['messages'][0]['title'] = "สยามพารากอน";
$arrayPostData['messages'][0]['address'] = "13.7465354,100.532752";
$arrayPostData['messages'][0]['latitude'] = "13.7465354";
$arrayPostData['messages'][0]['longitude'] = "100.532752";
replyMsg($arrayHeader,$arrayPostData);
}
#ตัวอย่าง Message Type "Text + Sticker ใน 1 ครั้ง"
else if($message == "ลาก่อน"){
$arrayPostData['replyToken'] = $arrayJson['events'][0]['replyToken'];
$arrayPostData['messages'][0]['type'] = "text";
$arrayPostData['messages'][0]['text'] = "อย่าทิ้งกันไป";
$arrayPostData['messages'][1]['type'] = "sticker";
$arrayPostData['messages'][1]['packageId'] = "1";
$arrayPostData['messages'][1]['stickerId'] = "131";
replyMsg($arrayHeader,$arrayPostData);
}
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);
}
exit;
?>