-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot-error.php
149 lines (140 loc) · 7.74 KB
/
bot-error.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
// กรณีต้องการตรวจสอบการแจ้ง error ให้เปิด 3 บรรทัดล่างนี้ให้ทำงาน กรณีไม่ ให้ comment ปิดไป
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// include composer autoload
require_once 'vendor/autoload.php';
// การตั้งเกี่ยวกับ bot
require_once 'bot_settings.php';
// กรณีมีการเชื่อมต่อกับฐานข้อมูล
//require_once("dbconnect.php");
///////////// ส่วนของการเรียกใช้งาน class ผ่าน namespace
use LINE\LINEBot;
use LINE\LINEBot\HTTPClient;
use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot\Event;
use LINE\LINEBot\Event\BaseEvent;
use LINE\LINEBot\Event\MessageEvent;
use LINE\LINEBot\MessageBuilder;
use LINE\LINEBot\MessageBuilder\TextMessageBuilder;
use LINE\LINEBot\MessageBuilder\StickerMessageBuilder;
use LINE\LINEBot\MessageBuilder\ImageMessageBuilder;
use LINE\LINEBot\MessageBuilder\LocationMessageBuilder;
use LINE\LINEBot\MessageBuilder\AudioMessageBuilder;
use LINE\LINEBot\MessageBuilder\VideoMessageBuilder;
use LINE\LINEBot\ImagemapActionBuilder;
use LINE\LINEBot\ImagemapActionBuilder\AreaBuilder;
use LINE\LINEBot\ImagemapActionBuilder\ImagemapMessageActionBuilder ;
use LINE\LINEBot\ImagemapActionBuilder\ImagemapUriActionBuilder;
use LINE\LINEBot\MessageBuilder\Imagemap\BaseSizeBuilder;
use LINE\LINEBot\MessageBuilder\ImagemapMessageBuilder;
use LINE\LINEBot\MessageBuilder\MultiMessageBuilder;
use LINE\LINEBot\TemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\DatetimePickerTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder;
use LINE\LINEBot\MessageBuilder\TemplateBuilder;
use LINE\LINEBot\MessageBuilder\TemplateMessageBuilder;
use LINE\LINEBot\MessageBuilder\TemplateBuilder\ButtonTemplateBuilder;
use LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselTemplateBuilder;
use LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselColumnTemplateBuilder;
use LINE\LINEBot\MessageBuilder\TemplateBuilder\ConfirmTemplateBuilder;
use LINE\LINEBot\MessageBuilder\TemplateBuilder\ImageCarouselTemplateBuilder;
use LINE\LINEBot\MessageBuilder\TemplateBuilder\ImageCarouselColumnTemplateBuilder;
// เชื่อมต่อกับ LINE Messaging API
$httpClient = new CurlHTTPClient(LINE_MESSAGE_ACCESS_TOKEN);
$bot = new LINEBot($httpClient, array('channelSecret' => LINE_MESSAGE_CHANNEL_SECRET));
// คำสั่งรอรับการส่งค่ามาของ LINE Messaging API
$content = file_get_contents('php://input');
// แปลงข้อความรูปแบบ JSON ให้อยู่ในโครงสร้างตัวแปร array
$events = json_decode($content, true);
if(!is_null($events)){
// ถ้ามีค่า สร้างตัวแปรเก็บ replyToken ไว้ใช้งาน
$replyToken = $events['events'][0]['replyToken'];
$typeMessage = $events['events'][0]['message']['type'];
$userMessage = $events['events'][0]['message']['text'];
$userMessage = strtolower($userMessage);
switch ($typeMessage){
case 'text':
switch ($userMessage) {
case "t":
$textReplyMessage = "Bot ตอบกลับคุณเป็นข้อความ";
$replyData = new TextMessageBuilder($textReplyMessage);
break;
case "i":
$picFullSize = 'https://www.mywebsite.com/imgsrc/photos/f/simpleflower';
$picThumbnail = 'https://www.mywebsite.com/imgsrc/photos/f/simpleflower/240';
$replyData = new ImageMessageBuilder($picFullSize,$picThumbnail);
break;
case "v":
$picThumbnail = 'https://www.mywebsite.com/imgsrc/photos/f/sampleimage/240';
$videoUrl = "https://www.mywebsite.com/simplevideo.mp4";
$replyData = new VideoMessageBuilder($videoUrl,$picThumbnail);
break;
case "a":
$audioUrl = "https://www.mywebsite.com/simpleaudio.mp3";
$replyData = new AudioMessageBuilder($audioUrl,27000);
break;
case "l":
$placeName = "ที่ตั้งร้าน";
$placeAddress = "แขวง พลับพลา เขต วังทองหลาง กรุงเทพมหานคร ประเทศไทย";
$latitude = 13.780401863217657;
$longitude = 100.61141967773438;
$replyData = new LocationMessageBuilder($placeName, $placeAddress, $latitude ,$longitude);
break;
case "s":
$stickerID = 22;
$packageID = 2;
$replyData = new StickerMessageBuilder($packageID,$stickerID);
break;
case "im":
$imageMapUrl = 'https://www.mywebsite.com/imgsrc/photos/w/sampleimagemap';
$replyData = new ImagemapMessageBuilder(
$imageMapUrl,
'This is Title',
new BaseSizeBuilder(699,1040),
array(
new ImagemapMessageActionBuilder(
'test image map',
new AreaBuilder(0,0,520,699)
),
new ImagemapUriActionBuilder(
'http://www.ninenik.com',
new AreaBuilder(520,0,520,699)
)
));
break;
case "tm":
$replyData = new TemplateMessageBuilder('Confirm Template',
new ConfirmTemplateBuilder(
'Confirm template builder',
array(
new MessageTemplateActionBuilder(
'Yes',
'Text Yes'
),
new MessageTemplateActionBuilder(
'No',
'Text NO'
)
)
)
);
break;
default:
$textReplyMessage = " คุณไม่ได้พิมพ์ ค่า ตามที่กำหนด";
$replyData = new TextMessageBuilder($textReplyMessage);
break;
}
break;
default:
$textReplyMessage = json_encode($events);
$replyData = new TextMessageBuilder($textReplyMessage);
break;
}
}
//l ส่วนของคำสั่งตอบกลับข้อความ
$response = $bot->replyMessage($replyToken,$replyData);
?>