สรุปใช้ Line bot sdk แบบรวบรัด ด้วยขั้นตอนง่ายๆ อัพเดทปี 2021

เขียนเมื่อ 3 ปีก่อน โดย Ninenik Narkdee
line line messaging api line bot sdk

คำสั่ง การ กำหนด รูปแบบ ตัวอย่าง เทคนิค ลูกเล่น การประยุกต์ การใช้งาน เกี่ยวกับ line line messaging api line bot sdk

ดูแล้ว 9,265 ครั้ง


จากเนื้อหา Line Messaging API ที่เป็นบทความตั้งแต่
ตอนที่ 1 ถึงตอนที่ 12 เป็นบทความที่มีเนื้อหาต่อเนื่อง โดยผู้เขียน
บทความทดสอบและบันทึกเป็นข้อมูลไว้สำหรับใช้งาน ลักษณะของเนื้อหา
จึงเป็นการค่อยๆ เพิ่มส่วนใหม่ๆ เข้าไป จนกระทั้งถึงเนื้อหาตอนที่ 12 ซึ่ง
คิดว่าน่าจะเป็นตอนสุดท้ายแล้วของบทความ
 
เพื่อให้คนที่สนใจสามารถศึกษาและใช้งานได้สะดวกยิ่งขึ้น จึงจะมาขอสรุป
เนื้อหาหลัก เป็นแนวทาง ให้ปรับประยุกต์ใช้ได้ง่ายขึ้น โดยเนื้อหาทั้ง 12 ตอนที่ผ่านมา
ก็จะยังเป็นรายละเอียดหลัก ที่เราต้องเข้าดูเพิ่มเติม ถ้าสนใจในหัวข้อที่เกี่ยวข้องนั้น
 

สรุปใช้ Line bot sdk สำหรัย PHP

    จะขอลำดับการเตรียมที่จะใช้งาน Line bot sdk ตามลำดับดังนี้
 
1. เราต้องสร้างส่วนที่ใช้เชื่อมต่อกับ Application Line App ก่อนเพื่อให้ได้ข้อมูลที่จำเป็น
มาใช้งาน ในไลน์ จะเรียกว่า การสร้าง Channel ก็เปรียบเสมือนว่า  app ที่ใช้งานร่วมกับไลน์
เนื้อหานี้จะอยู่ในตอนที่ 1
 
การใช้งาน LINE Messaging API สร้าง Bot ให้บริการและโต้ตอบกับผู้ใช้ ตอนที่ 1 http://niik.in/834 
 
2. เมื่อเราได้สร้าง app สำหรับกำหนดค่าต่างๆ ในการใช้งาน LINE Messaging API ต่อไปก็เป็นส่วนของ
การติดตั้ง library เพื่อช่วยในการเขียนคำสั่งการทำงานร่วมกับ app และ line bot เนื้อหาตอนนี้ก็จะ
อยู่ในตอนที่ 2 ดูเฉพาะส่วนของการติดตั้ง LINE bot sdk php ก็พอ
 
ส่งข้อความด้วย LINE bot sdk php สำหรับ LINE Messaging API ตอนที่ 2 http://niik.in/835 
 
3. ต่อไปก็เป็นส่วนของไฟล์ตั้งต้น จะขอรวมการเรียกใช้ class ที่จำเป็นทั้งหมดจากเนื้อหาตอนท้ายๆ 
มารวมไว้ จะประกอบไปด้วย 4 ไฟล์ 
 
    - ไฟล์ bot_settings.php // ไฟล์สำหรับตั้งค่า ของ app 
    - ไฟล์ bot.php // ไฟล์หลักที่รวบรวมส่วนต่างๆ มาไว้ด้วยกัน ใช้เป็น webhook ไฟล์
    - ไฟล์ bot_action.php // ไฟล์สำหรับกำหนดการทำงาน
    - ไฟล์ flex_gen.php // ไฟล์ฟังก์ชั่น ที่ผู้เขียนทำเพิ่ม แปลง xml เป็น flex
 
จะขอลงรายละเอียดโค้ดแต่ละไฟล์ โดยเอาเฉพาะที่เป็นหลักสำคัญเริ่มต้น
โค้ดแต่ละไฟล์จะเป็นดังนี้
 

ไฟล์ bot_settings.php

 
1
2
3
4
5
6
<?php
/// การตั้งค่าเกี่ยวกับ bot ใน LINE Messaging API ดูค่าต่างๆ ใน http://niik.in/834
define('LINE_MESSAGE_CHANNEL_ID','กรอก ค่า Channel ID');
define('LINE_MESSAGE_CHANNEL_SECRET','กรอกค่า Channel secret');
define('LINE_MESSAGE_ACCESS_TOKEN','กรอกค่า Channel access token');
?>
 
ไฟล์นี้จะใช้สำหรับตั้งค่า มีแค่ 3 บรรทัดหลัก
 

ไฟล์ flex_gen.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<?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\Event\AccountLinkEvent;
use LINE\LINEBot\Event\MemberJoinEvent;
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;
use LINE\LINEBot\QuickReplyBuilder;
use LINE\LINEBot\QuickReplyBuilder\QuickReplyMessageBuilder;
use LINE\LINEBot\QuickReplyBuilder\ButtonBuilder\QuickReplyButtonBuilder;
use LINE\LINEBot\TemplateActionBuilder\CameraRollTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\CameraTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\LocationTemplateActionBuilder;
use LINE\LINEBot\RichMenuBuilder;
use LINE\LINEBot\RichMenuBuilder\RichMenuSizeBuilder;
use LINE\LINEBot\RichMenuBuilder\RichMenuAreaBuilder;
use LINE\LINEBot\RichMenuBuilder\RichMenuAreaBoundsBuilder;
use LINE\LINEBot\Constant\Flex\ComponentIconSize;
use LINE\LINEBot\Constant\Flex\ComponentImageSize;
use LINE\LINEBot\Constant\Flex\ComponentImageAspectRatio;
use LINE\LINEBot\Constant\Flex\ComponentImageAspectMode;
use LINE\LINEBot\Constant\Flex\ComponentFontSize;
use LINE\LINEBot\Constant\Flex\ComponentFontWeight;
use LINE\LINEBot\Constant\Flex\ComponentMargin;
use LINE\LINEBot\Constant\Flex\ComponentSpacing;
use LINE\LINEBot\Constant\Flex\ComponentButtonStyle;
use LINE\LINEBot\Constant\Flex\ComponentButtonHeight;
use LINE\LINEBot\Constant\Flex\ComponentSpaceSize;
use LINE\LINEBot\Constant\Flex\ComponentGravity;
use LINE\LINEBot\Constant\Flex\BubleContainerSize;
use LINE\LINEBot\MessageBuilder\FlexMessageBuilder;
use LINE\LINEBot\MessageBuilder\Flex\BubbleStylesBuilder;
use LINE\LINEBot\MessageBuilder\Flex\BlockStyleBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ContainerBuilder\BubbleContainerBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ContainerBuilder\CarouselContainerBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\BoxComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\ButtonComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\IconComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\ImageComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\SpacerComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\FillerComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\SeparatorComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\TextComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\SpanComponentBuilder;
 
 
$is_carousel = NULL;
$is_bubble = NULL;
$is_singleBubble = NULL;
$count_container = 0;
$count_bubble = 0;
$name_container = NULL;
 
$bubble_direction = array();
$bubble_size = array();
$bubble_action = array();
$block_header = array();
$block_hero = array();
$block_body = array();
$block_footer = array();
$block_header_style = array();
$block_hero_style = array();
$block_body_style = array();
$block_footer_style = array();
$box_header = array();
$box_hero = array();
$box_body = array();
$box_footer = array();
 
function bubbleContainerAttr($xmlObj){
    $bubbleContainer = $xmlObj;
    $bubbleContainerAttr = array(null,null,null);
    foreach($bubbleContainer->attributes() as $key=>$val){
        if($key=="direction"){$bubbleContainerAttr[0]=implode("",(array)$val[0]);}
        if($key=="size"){$bubbleContainerAttr[1]=implode("",(array)$val[0]);}
        if($key=="action"){
            $textButton = "u";
            $strAction = str_replace(")","",implode("",(array)$val[0]));
            switch($strAction){
                case (preg_match('/^p\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new PostbackTemplateActionBuilder($textButton,$data);             
                    break;
                case (preg_match('/^m\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new MessageTemplateActionBuilder($textButton,$data);              
                    break;
                case (preg_match('/^u\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,$data);          
                    break;
                case (preg_match('/^c\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/camera/");    
                    break;
                case (preg_match('/^cs\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/cameraRoll/single/"); 
                    break;
                case (preg_match('/^cm\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/cameraRoll/multi/");              
                    break;
                case (preg_match('/^l\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/location");               
                    break;
                case (preg_match('/^d\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new DatetimePickerTemplateActionBuilder($textButton,$data,'datetime');
                    break;                                                                                                                                         
                default:
                    $actionVal = new PostbackTemplateActionBuilder($textButton,"");
                    break;                 
            }                  
            $bubbleContainerAttr[2] = $actionVal;
        }      
    }  
    return $bubbleContainerAttr;           
}
function blockStyleAttr($xmlObj){
    $blockStyle = $xmlObj;
    $blockStyleAttr = array(null,null,null);
    foreach($blockStyle->attributes() as $key=>$val){
        if($key=="backgroundColor"){$blockStyleAttr[0]=implode("",(array)$val[0]);}
        if($key=="separator"){$blockStyleAttr[1]=(boolean)implode("",(array)$val[0]);}
        if($key=="separatorColor"){$blockStyleAttr[2]=implode("",(array)$val[0]);}
    }  
    if(count($blockStyle->attributes())>0){
        return $blockStyleAttr;        
    }else{
        return NULL;   
    }
}
function textComponentArr($xmlObj){
    $textComponent = $xmlObj;
    $textComponentAttr = array(trim((string)$textComponent),null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
    foreach($textComponent->attributes() as $key=>$val){
        if($key=="flex"){$textComponentAttr[1]=(int)implode("",(array)$val[0]);}
        if($key=="margin"){$textComponentAttr[2]=implode("",(array)$val[0]);}
        if($key=="size"){$textComponentAttr[3]=implode("",(array)$val[0]);}
        if($key=="align"){$textComponentAttr[4]=implode("",(array)$val[0]);}
        if($key=="gravity"){$textComponentAttr[5]=implode("",(array)$val[0]);}
        if($key=="wrap"){$textComponentAttr[6]=(boolean)implode("",(array)$val[0]);}
        if($key=="maxLines"){$textComponentAttr[7]=(int)implode("",(array)$val[0]);}
        if($key=="weight"){$textComponentAttr[8]=implode("",(array)$val[0]);}      
        if($key=="color"){$textComponentAttr[9]=implode("",(array)$val[0]);}
        if($key=="action"){
            $textButton = "u";
            $strAction = str_replace(")","",implode("",(array)$val[0]));
            switch($strAction){
                case (preg_match('/^p\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new PostbackTemplateActionBuilder($textButton,$data);             
                    break;
                case (preg_match('/^m\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new MessageTemplateActionBuilder($textButton,$data);              
                    break;
                case (preg_match('/^u\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,$data);          
                    break;
                case (preg_match('/^c\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/camera/");    
                    break;
                case (preg_match('/^cs\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/cameraRoll/single/"); 
                    break;
                case (preg_match('/^cm\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/cameraRoll/multi/");              
                    break;
                case (preg_match('/^l\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/location");               
                    break;
                case (preg_match('/^d\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new DatetimePickerTemplateActionBuilder($textButton,$data,'datetime');
                    break;                                                                                                                                         
                default:
                    $actionVal = new PostbackTemplateActionBuilder($textButton,"");
                    break;                 
            }                  
            $textComponentAttr[10] = $actionVal;
        }
        if($key=="position"){$textComponentAttr[11]=implode("",(array)$val[0]);}
        if($key=="offsetTop"){$textComponentAttr[12]=implode("",(array)$val[0]);}
        if($key=="offsetBottom"){$textComponentAttr[13]=implode("",(array)$val[0]);}
        if($key=="offsetStart"){$textComponentAttr[14]=implode("",(array)$val[0]);}
        if($key=="offsetEnd"){$textComponentAttr[15]=implode("",(array)$val[0]);}
        if($key=="style"){$textComponentAttr[16]=implode("",(array)$val[0]);}
        if($key=="decoration"){$textComponentAttr[17]=implode("",(array)$val[0]);}
    }  
    return $textComponentAttr;
}
function boxComponentArr($xmlObj,$arrComponent = array()){
    $boxComponent = $xmlObj;
    $boxComponentAttr = array(null,$arrComponent,null,null,null,null,null,null,null,null,null,null,null,null
    ,null,null,null,null,null,null,null,null);
    foreach($boxComponent->attributes() as $key=>$val){
        if($key=="layout"){$boxComponentAttr[0]=implode("",(array)$val[0]);}
        if($key=="flex"){$boxComponentAttr[2]=(int)implode("",(array)$val[0]);}
        if($key=="spacing"){$boxComponentAttr[3]=implode("",(array)$val[0]);}
        if($key=="margin"){$boxComponentAttr[4]=implode("",(array)$val[0]);}
        if($key=="action"){
            $textButton = "u";
            $strAction = str_replace(")","",implode("",(array)$val[0]));
            switch($strAction){
                case (preg_match('/^p\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new PostbackTemplateActionBuilder($textButton,$data);             
                    break;
                case (preg_match('/^m\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new MessageTemplateActionBuilder($textButton,$data);              
                    break;
                case (preg_match('/^u\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,$data);          
                    break;
                case (preg_match('/^c\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/camera/");    
                    break;
                case (preg_match('/^cs\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/cameraRoll/single/"); 
                    break;
                case (preg_match('/^cm\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/cameraRoll/multi/");              
                    break;
                case (preg_match('/^l\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/location");               
                    break;
                case (preg_match('/^d\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new DatetimePickerTemplateActionBuilder($textButton,$data,'datetime');
                    break;                                                                                                                                         
                default:
                    $actionVal = new PostbackTemplateActionBuilder($textButton,"");
                    break;                 
            }      
            $boxComponentAttr[5] = $actionVal;
        }
        if($key=="paddingAll"){$boxComponentAttr[6]=implode("",(array)$val[0]);}
        if($key=="paddingTop"){$boxComponentAttr[7]=implode("",(array)$val[0]);}
        if($key=="paddingBottom"){$boxComponentAttr[8]=implode("",(array)$val[0]);}
        if($key=="paddingStart"){$boxComponentAttr[9]=implode("",(array)$val[0]);}
        if($key=="paddingEnd"){$boxComponentAttr[10]=implode("",(array)$val[0]);}
        if($key=="backgroundColor"){$boxComponentAttr[11]=implode("",(array)$val[0]);}
        if($key=="borderColor"){$boxComponentAttr[12]=implode("",(array)$val[0]);}
        if($key=="borderWidth"){$boxComponentAttr[13]=implode("",(array)$val[0]);}
        if($key=="cornerRadius"){$boxComponentAttr[14]=implode("",(array)$val[0]);}
        if($key=="width"){$boxComponentAttr[15]=implode("",(array)$val[0]);}
        if($key=="height"){$boxComponentAttr[16]=implode("",(array)$val[0]);}
        if($key=="position"){$boxComponentAttr[17]=implode("",(array)$val[0]);}
        if($key=="offsetTop"){$boxComponentAttr[18]=implode("",(array)$val[0]);}
        if($key=="offsetBottom"){$boxComponentAttr[19]=implode("",(array)$val[0]);}
        if($key=="offsetStart"){$boxComponentAttr[20]=implode("",(array)$val[0]);}
        if($key=="offsetEnd"){$boxComponentAttr[21]=implode("",(array)$val[0]);}
    }  
    return $boxComponentAttr;
}
function buttonComponentArr($xmlObj){
    $buttonComponent = $xmlObj;
    $textButton = trim((string)$buttonComponent);
    $buttonComponentAttr = array(new PostbackTemplateActionBuilder($textButton,"nothing"),null,null,null,
    null,null,null,null,null,null,null,null);
    foreach($buttonComponent->attributes() as $key=>$val){
        if($key=="action"){
            $strAction = str_replace(")","",implode("",(array)$val[0]));
            switch($strAction){
                case (preg_match('/^p\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new PostbackTemplateActionBuilder($textButton,$data);             
                    break;
                case (preg_match('/^m\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new MessageTemplateActionBuilder($textButton,$data);              
                    break;
                case (preg_match('/^u\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,$data);          
                    break;
                case (preg_match('/^c\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/camera/");    
                    break;
                case (preg_match('/^cs\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/cameraRoll/single/"); 
                    break;
                case (preg_match('/^cm\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/cameraRoll/multi/");              
                    break;
                case (preg_match('/^l\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/location");               
                    break;
                case (preg_match('/^d\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new DatetimePickerTemplateActionBuilder($textButton,$data,'datetime');
                    break;                                                                                                                                         
                default:
                    $actionVal = new PostbackTemplateActionBuilder($textButton,"false");   
                    break;                 
            }
            $buttonComponentAttr[0] = $actionVal;
        }
        if($key=="flex"){$buttonComponentAttr[1]=(int)implode("",(array)$val[0]);}
        if($key=="margin"){$buttonComponentAttr[2]=implode("",(array)$val[0]);}
        if($key=="height"){$buttonComponentAttr[3]=implode("",(array)$val[0]);}
        if($key=="style"){$buttonComponentAttr[4]=implode("",(array)$val[0]);}
        if($key=="color"){$buttonComponentAttr[5]=implode("",(array)$val[0]);}
        if($key=="gravity"){$buttonComponentAttr[6]=implode("",(array)$val[0]);}
         
        if($key=="position"){$buttonComponentAttr[7]=implode("",(array)$val[0]);}
        if($key=="offsetTop"){$buttonComponentAttr[8]=implode("",(array)$val[0]);}
        if($key=="offsetBottom"){$buttonComponentAttr[9]=implode("",(array)$val[0]);}
        if($key=="offsetStart"){$buttonComponentAttr[10]=implode("",(array)$val[0]);}
        if($key=="offsetEnd"){$buttonComponentAttr[11]=implode("",(array)$val[0]);}    
         
    }  
    return $buttonComponentAttr;
}
function iconComponentAttr($xmlObj){
    $iconComponent = $xmlObj;
    $iconComponentAttr = array(null,null,null,null,null,null,null,null,null);
    foreach($iconComponent->attributes() as $key=>$val){
        if($key=="url"){$iconComponentAttr[0]=implode("",(array)$val[0]);}
        if($key=="margin"){$iconComponentAttr[1]=implode("",(array)$val[0]);}
        if($key=="size"){$iconComponentAttr[2]=implode("",(array)$val[0]);}
        if($key=="aspectRatio"){$iconComponentAttr[3]=implode("",(array)$val[0]);}
         
        if($key=="position"){$iconComponentAttr[4]=implode("",(array)$val[0]);}
        if($key=="offsetTop"){$iconComponentAttr[5]=implode("",(array)$val[0]);}
        if($key=="offsetBottom"){$iconComponentAttr[6]=implode("",(array)$val[0]);}
        if($key=="offsetStart"){$iconComponentAttr[7]=implode("",(array)$val[0]);}
        if($key=="offsetEnd"){$iconComponentAttr[8]=implode("",(array)$val[0]);}               
         
    }  
    return $iconComponentAttr;     
}
function spanComponentAttr($xmlObj){
    $spanComponent = $xmlObj;
    $spanComponentAttr = array("test span",null,null,null,null,null);  
    foreach($spanComponent->attributes() as $key=>$val){
        if($key=="size"){$spanComponentAttr[1]=implode("",(array)$val[0]);}
        if($key=="color"){$spanComponentAttr[2]=implode("",(array)$val[0]);}       
        if($key=="weight"){$spanComponentAttr[3]=implode("",(array)$val[0]);}
        if($key=="style"){$spanComponentAttr[4]=implode("",(array)$val[0]);}
        if($key=="decoration"){$spanComponentAttr[5]=implode("",(array)$val[0]);}      
    }  
    return $spanComponentAttr;     
}
function separatorComponentAttr($xmlObj){
    $separatorComponent = $xmlObj;
    $separatorComponentAttr = array();
    foreach($separatorComponent->attributes() as $key=>$val){
        if($key=="margin"){$separatorComponentAttr[0]=implode("",(array)$val[0]);}
        if($key=="color"){$separatorComponentAttr[1]=implode("",(array)$val[0]);}
    }  
    return $separatorComponentAttr;    
}
function fillerComponentAttr($xmlObj){
    $fillerComponent= $xmlObj;
    $fillerComponentAttr = array();
    foreach($fillerComponent->attributes() as $key=>$val){
        if($key=="flex"){$fillerComponentAttr[0]=(int)implode("",(array)$val[0]);}
    }  
    return $fillerComponentAttr;               
}
function spacerComponentAttr($xmlObj){
    $spacerComponent = $xmlObj;
    $spacerComponentAttr = array("md");
    foreach($spacerComponent->attributes() as $key=>$val){
        if($key=="size"){$spacerComponentAttr[0]=implode("",(array)$val[0]);}
    }  
    return $spacerComponentAttr;   
}
function imageComponentArr($xmlObj){
    $imageComponent = $xmlObj;
    $imageComponentAttr = array(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
    foreach($imageComponent->attributes() as $key=>$val){
        if($key=="url"){$imageComponentAttr[0]=implode("",(array)$val[0]);}
        if($key=="flex"){$imageComponentAttr[1]=(int)implode("",(array)$val[0]);}
        if($key=="margin"){$imageComponentAttr[2]=implode("",(array)$val[0]);}
        if($key=="align"){$imageComponentAttr[3]=implode("",(array)$val[0]);}
        if($key=="gravity"){$imageComponentAttr[4]=implode("",(array)$val[0]);}
        if($key=="size"){$imageComponentAttr[5]=implode("",(array)$val[0]);}
        if($key=="aspectRatio"){$imageComponentAttr[6]=implode("",(array)$val[0]);}
        if($key=="aspectMode"){$imageComponentAttr[7]=implode("",(array)$val[0]);}     
        if($key=="backgroundColor"){$imageComponentAttr[8]=implode("",(array)$val[0]);}
        if($key=="action"){
            $textButton = "u";
            $strAction = str_replace(")","",implode("",(array)$val[0]));
            switch($strAction){
                case (preg_match('/^p\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new PostbackTemplateActionBuilder($textButton,$data);             
                    break;
                case (preg_match('/^m\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new MessageTemplateActionBuilder($textButton,$data);              
                    break;
                case (preg_match('/^u\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,$data);          
                    break;
                case (preg_match('/^c\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/camera/");    
                    break;
                case (preg_match('/^cs\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/cameraRoll/single/"); 
                    break;
                case (preg_match('/^cm\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/cameraRoll/multi/");              
                    break;
                case (preg_match('/^l\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new UriTemplateActionBuilder($textButton,"line://nv/location");               
                    break;
                case (preg_match('/^d\(/',$strAction) ? true : false):
                    list($ac,$data) = explode("(",$strAction);
                    $actionVal = new DatetimePickerTemplateActionBuilder($textButton,$data,'datetime');
                    break;                                                                                                                                         
                default:
                    $actionVal = new PostbackTemplateActionBuilder($textButton,"");
                    break;                 
            }
            $imageComponentAttr[9] = $actionVal;           
        }
        if($key=="position"){$imageComponentAttr[10]=implode("",(array)$val[0]);}
        if($key=="offsetTop"){$imageComponentAttr[11]=implode("",(array)$val[0]);}
        if($key=="offsetBottom"){$imageComponentAttr[12]=implode("",(array)$val[0]);}
        if($key=="offsetStart"){$imageComponentAttr[13]=implode("",(array)$val[0]);}
        if($key=="offsetEnd"){$imageComponentAttr[14]=implode("",(array)$val[0]);}             
    }  
    return $imageComponentAttr;
}
function genComponent($elName,$childObj,$arr_Element=null){
    if($elName=="box"){
        $arr_childObj = array_chunk(boxComponentArr($childObj,listElement($childObj,$arr_Element)), 6, true);
        $param_childObj = array_shift($arr_childObj);
        $option_attr = boxComponentArr($childObj);
        $boxObj_C = new BoxComponentBuilder(...$param_childObj);   
        if(is_array($option_attr) && count($option_attr)>0){
            $boxObj_C->setPaddingAll($option_attr[6])
            ->setPaddingTop($option_attr[7])
            ->setPaddingBottom($option_attr[8])
            ->setPaddingStart($option_attr[9])
            ->setPaddingEnd($option_attr[10])
            ->setBackgroundColor($option_attr[11])
            ->setBorderColor($option_attr[12])
            ->setBorderWidth($option_attr[13])
            ->setCornerRadius($option_attr[14])
            ->setWidth($option_attr[15])                                                                                                        
            ->setHeight($option_attr[16])
            ->setPosition($option_attr[17])
            ->setOffsetTop($option_attr[18])
            ->setOffsetBottom($option_attr[19])
            ->setOffsetStart($option_attr[20])
            ->setOffsetEnd($option_attr[21]);
        }  
        return $boxObj_C;
    }
    if($elName=="text"){
        $arr_childObj = array_chunk(textComponentArr($childObj), 11, true);
        $param_childObj = array_shift($arr_childObj);
        $option_attr = textComponentArr($childObj);
        $textObj_C = new TextComponentBuilder(...$param_childObj);
        if(is_array($option_attr) && count($option_attr)>0){
            $textObj_C->setPosition($option_attr[11])
            ->setOffsetTop($option_attr[12])
            ->setOffsetBottom($option_attr[13])
            ->setOffsetStart($option_attr[14])
            ->setOffsetEnd($option_attr[15])
            ->setStyle($option_attr[16])
            ->setDecoration($option_attr[17]);
        }
        $spanObj_C = array();
        foreach ($childObj->children() as $_childObj) {     
            $spanObj_C[] = new SpanComponentBuilder(...spanComponentAttr($_childObj));
        }
        $textObj_C->setContents($spanObj_C);
        return $textObj_C;
    }
    if($elName=="button"){
        $arr_childObj = array_chunk(buttonComponentArr($childObj), 7, true);
        $param_childObj = array_shift($arr_childObj);
        $option_attr = buttonComponentArr($childObj);
        $buttonObj_C = new ButtonComponentBuilder(...$param_childObj);
        if(is_array($option_attr) && count($option_attr)>0){
            $buttonObj_C->setPosition($option_attr[7])
            ->setOffsetTop($option_attr[8])
            ->setOffsetBottom($option_attr[9])
            ->setOffsetStart($option_attr[10])
            ->setOffsetEnd($option_attr[11]);
        }
        return $buttonObj_C;
    }
    if($elName=="icon"){
        $arr_childObj = array_chunk(iconComponentAttr($childObj), 4, true);
        $param_childObj = array_shift($arr_childObj);
        $option_attr = iconComponentAttr($childObj);
        $iconObj_C = new IconComponentBuilder(...$param_childObj);
        if(is_array($option_attr) && count($option_attr)>0){
            $iconObj_C->setPosition($option_attr[4])
            ->setOffsetTop($option_attr[5])
            ->setOffsetBottom($option_attr[6])
            ->setOffsetStart($option_attr[7])
            ->setOffsetEnd($option_attr[8]);
        }
        return $iconObj_C;
    }
    if($elName=="image"){
        $arr_childObj = array_chunk(imageComponentArr($childObj), 10, true);
        $param_childObj = array_shift($arr_childObj);
        $option_attr = imageComponentArr($childObj);
        $imageObj_C = new ImageComponentBuilder(...$param_childObj);
        if(is_array($option_attr) && count($option_attr)>0){
            $imageObj_C->setPosition($option_attr[10])
            ->setOffsetTop($option_attr[11])
            ->setOffsetBottom($option_attr[12])
            ->setOffsetStart($option_attr[13])
            ->setOffsetEnd($option_attr[14]);
        }
        return $imageObj_C;
    }          
    if($elName=="filler"){
        $fillerObj_C = new FillerComponentBuilder();   
        $option_attr = fillerComponentAttr($childObj);
        if(is_array($option_attr) && count($option_attr)>0){
            $fillerObj_C->setFlex($option_attr[0]);
        }
        return $fillerObj_C;
    }
}
function listElement($xmlObj,$arr_Element = array()){
    $arr_final = array();
    foreach ($xmlObj->children() as $childObj) {    
        $elName = $childObj->getName(); 
        if($elName=="box"){
            $arr_final[] = genComponent("box",$childObj,$arr_Element);
        }else{         
            if($elName=="text"){
                $arr_final[] = genComponent("text",$childObj);
            }elseif($elName=="button"){    
                $arr_final[] = genComponent("button",$childObj);       
            }elseif($elName=="icon"){          
                $arr_final[] = genComponent("icon",$childObj);             
            }elseif($elName=="image"){         
                $arr_final[] = genComponent("image",$childObj);
            }elseif($elName=="filler"){    
                $arr_final[] = genComponent("filler",$childObj);       
            }elseif($elName=="span"){              
                $arr_final[] = new SpanComponentBuilder(...spanComponentAttr($childObj)); //                           
            }elseif($elName=="spacer"){            
                $arr_final[] = new SpacerComponentBuilder(...spacerComponentAttr($childObj)); //
            }elseif($elName=="separator"){             
                $arr_final[] = new SeparatorComponentBuilder(...separatorComponentAttr($childObj)); //                                     
            }else{
                $arr_final[] = array();
            }
        }
    }  
    return $arr_final;
}
function createFlex($xmlstr){
    //$objXML = new SimpleXMLElement($xmlstr);
    $xmlIterator = new SimpleXMLIterator($xmlstr);
    $xmlIterator->rewind();
    $name_container = $xmlIterator->getName();
    $count_container = $xmlIterator->count();
     
    if($name_container!="carousel"){
        $is_singleBubble = true;
    }
    $arr_bubble = array();
    $hasBlockStyle = false;
    $i = 0;
    for( $xmlIterator; $xmlIterator->valid(); $xmlIterator->next() ) {
        $bubble_direction[$i] = NULL;
        $bubble_size[$i] = NULL;
        $bubble_action[$i] = NULL;
        $bubble_direction[$i] = bubbleContainerAttr($xmlIterator->current());
        $bubble_size[$i] = bubbleContainerAttr($xmlIterator->current());
        $bubble_action[$i] = bubbleContainerAttr($xmlIterator->current());
         
        $block_header[$i] = NULL;
        $block_hero[$i] = NULL;
        $block_body[$i] = NULL;
        $block_footer[$i] = NULL;
        $block_header_style[$i] = NULL;
        $block_hero_style[$i] = NULL;
        $block_body_style[$i] = NULL;
        $block_footer_style[$i] = NULL;
        foreach ($xmlIterator->current()->children() as $blockChild) {
            if($blockChild->getName()=="header"){
                $block_header[$i]=$blockChild;
                $block_header_style[$i] = (!is_null(blockStyleAttr($blockChild)))?new BlockStyleBuilder(...blockStyleAttr($blockChild)):NULL;  
                $box_header[$i] = listElement($blockChild->children());
            }
            if($blockChild->getName()=="hero"){
                $block_hero[$i]=$blockChild;
                $block_hero_style[$i] = (!is_null(blockStyleAttr($blockChild)))?new BlockStyleBuilder(...blockStyleAttr($blockChild)):NULL;
                $box_hero[$i] = listElement($blockChild->children());
            }
            if($blockChild->getName()=="body"){
                $block_body[$i]=$blockChild;   
                $block_body_style[$i] = (!is_null(blockStyleAttr($blockChild)))?new BlockStyleBuilder(...blockStyleAttr($blockChild)):NULL;
                $box_body[$i] = listElement($blockChild->children());
            }
            if($blockChild->getName()=="footer"){
                $block_footer[$i]=$blockChild
                $block_footer_style[$i] = (!is_null(blockStyleAttr($blockChild)))?new BlockStyleBuilder(...blockStyleAttr($blockChild)):NULL;  
                $box_footer[$i] = listElement($blockChild->children());
            }
     
        }
        $style_bubble[$i]=NULL;
        if(!is_null($block_header_style[$i]) || !is_null($block_hero_style[$i]) || !is_null($block_body_style[$i]) || !is_null($block_footer_style[$i])){
                $style_bubble[$i] = new BubbleStylesBuilder(
                    $block_header_style[$i],
                    $block_hero_style[$i],
                    $block_body_style[$i],
                    $block_footer_style[$i]
                );
        }
        $i++;
     
    }
    for($i=0;$i<$count_container;$i++){
        $containDirection = (!is_null($bubble_direction[$i]))?$bubble_direction[$i][0]:"ltr";
        $block_HeaderObj = (!is_null($block_header[$i]))?genComponent("box",$block_header[$i]->children(),$box_header[$i]):NULL;
        $block_HeroObj = (!is_null($block_hero[$i]))?genComponent("box",$block_hero[$i]->children(),$box_hero[$i]):NULL;
        $block_BodyObj = (!is_null($block_body[$i]))?genComponent("box",$block_body[$i]->children(),$box_body[$i]):NULL;
        $block_FooterObj = (!is_null($block_footer[$i]))?genComponent("box",$block_footer[$i]->children(),$box_footer[$i]):NULL;
         
        $containHeader = $block_HeaderObj;
        $containHero = $block_HeroObj;
        $containBody = $block_BodyObj;
        $containFooter = $block_FooterObj;
        $containSize = (!is_null($bubble_size[$i]))?$bubble_size[$i][1]:NULL;
        $containAction = (!is_null($bubble_action[$i]))?$bubble_action[$i][2]:NULL;
        $bubbleObj = new BubbleContainerBuilder(
            $containDirection,$containHeader,$containHero,$containBody,$containFooter,$style_bubble[$i],$containSize
        );
        if(!is_null($containAction)){
            $bubbleObj = $bubbleObj->setAction($containAction);
        }      
        $arr_bubble[] = $bubbleObj;
    }      
    $arr_FlexMessage = NULL;
    if(!is_null($is_singleBubble)){
        $arr_FlexMessage = $arr_bubble[0];
    }else{
        $arr_FlexMessage = new CarouselContainerBuilder(
            $arr_bubble
        );
    }
    $textReplyMessage = $arr_FlexMessage;
    return $textReplyMessage;
}
 
ไฟล์นี้สำหรับแปลง xml เป็น flex ที่ผู้เขียนทำขึ้นมา จะมีหรือไม่ก็ได้ ถ้าไม่ได้เรียกใช้งานก็ไม่ต้อง
มีไฟล์นี้ก็ได้
 

ไฟล์ 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?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\Event\AccountLinkEvent;
use LINE\LINEBot\Event\MemberJoinEvent;
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;
use LINE\LINEBot\QuickReplyBuilder;
use LINE\LINEBot\QuickReplyBuilder\QuickReplyMessageBuilder;
use LINE\LINEBot\QuickReplyBuilder\ButtonBuilder\QuickReplyButtonBuilder;
use LINE\LINEBot\TemplateActionBuilder\CameraRollTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\CameraTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\LocationTemplateActionBuilder;
use LINE\LINEBot\RichMenuBuilder;
use LINE\LINEBot\RichMenuBuilder\RichMenuSizeBuilder;
use LINE\LINEBot\RichMenuBuilder\RichMenuAreaBuilder;
use LINE\LINEBot\RichMenuBuilder\RichMenuAreaBoundsBuilder;
use LINE\LINEBot\Constant\Flex\ComponentIconSize;
use LINE\LINEBot\Constant\Flex\ComponentImageSize;
use LINE\LINEBot\Constant\Flex\ComponentImageAspectRatio;
use LINE\LINEBot\Constant\Flex\ComponentImageAspectMode;
use LINE\LINEBot\Constant\Flex\ComponentFontSize;
use LINE\LINEBot\Constant\Flex\ComponentFontWeight;
use LINE\LINEBot\Constant\Flex\ComponentMargin;
use LINE\LINEBot\Constant\Flex\ComponentSpacing;
use LINE\LINEBot\Constant\Flex\ComponentButtonStyle;
use LINE\LINEBot\Constant\Flex\ComponentButtonHeight;
use LINE\LINEBot\Constant\Flex\ComponentSpaceSize;
use LINE\LINEBot\Constant\Flex\ComponentGravity;
use LINE\LINEBot\MessageBuilder\FlexMessageBuilder;
use LINE\LINEBot\MessageBuilder\Flex\BubbleStylesBuilder;
use LINE\LINEBot\MessageBuilder\Flex\BlockStyleBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ContainerBuilder\BubbleContainerBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ContainerBuilder\CarouselContainerBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\BoxComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\ButtonComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\IconComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\ImageComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\SpacerComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\FillerComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\SeparatorComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\TextComponentBuilder;
 
$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');
  
// กำหนดค่า signature สำหรับตรวจสอบข้อมูลที่ส่งมาว่าเป็นข้อมูลจาก LINE
$hash = hash_hmac('sha256', $content, LINE_MESSAGE_CHANNEL_SECRET, true);
$signature = base64_encode($hash);
  
// แปลงค่าข้อมูลที่ได้รับจาก LINE เป็น array ของ Event Object
$events = $bot->parseEventRequest($content, $signature);
$eventObj = $events[0]; // Event Object ของ array แรก
  
// ดึงค่าประเภทของ Event มาไว้ในตัวแปร มีทั้งหมด 7 event
$eventType = $eventObj->getType();
  
// สร้างตัวแปร ไว้เก็บ sourceId ของแต่ละประเภท
$userId = NULL;
$groupId = NULL;
$roomId = NULL;
// สร้างตัวแปรเก็บ source id และ source type
$sourceId = NULL;
$sourceType = NULL;
// สร้างตัวแปร replyToken และ replyData สำหรับกรณีใช้ตอบกลับข้อความ
$replyToken = NULL;
$replyData = NULL;
// สร้างตัวแปร ไว้เก็บค่าว่าเป้น Event ประเภทไหน
$eventMessage = NULL;
$eventPostback = NULL;
$eventJoin = NULL;
$eventLeave = NULL;
$eventFollow = NULL;
$eventUnfollow = NULL;
$eventBeacon = NULL;
$eventAccountLink = NULL;
$eventMemberJoined = NULL;
$eventMemberLeft = NULL;
// เงื่อนไขการกำหนดประเภท Event
switch($eventType){
    case 'message': $eventMessage = true; break;   
    case 'postback': $eventPostback = true; break
    case 'join': $eventJoin = true; break
    case 'leave': $eventLeave = true; break;   
    case 'follow': $eventFollow = true; break
    case 'unfollow': $eventUnfollow = true; break
    case 'beacon': $eventBeacon = true; break;    
    case 'accountLink': $eventAccountLink = true; break;      
    case 'memberJoined': $eventMemberJoined = true; break;      
    case 'memberLeft': $eventMemberLeft = true; break;                                          
}
// สร้างตัวแปรเก็บค่า userId กรณีเป็น Event ที่เกิดขึ้นใน USER
if($eventObj->isUserEvent()){
    $userId = $eventObj->getUserId(); 
    $sourceType = "USER";
}
// สร้างตัวแปรเก็บค่า groupId กรณีเป็น Event ที่เกิดขึ้นใน GROUP
if($eventObj->isGroupEvent()){
    $groupId = $eventObj->getGroupId(); 
    $userId = $eventObj->getUserId(); 
    $sourceType = "GROUP";
}
// สร้างตัวแปรเก็บค่า roomId กรณีเป็น Event ที่เกิดขึ้นใน ROOM
if($eventObj->isRoomEvent()){
    $roomId = $eventObj->getRoomId();       
    $userId = $eventObj->getUserId();     
    $sourceType = "ROOM";
}
// เก็บค่า sourceId ปกติจะเป็นค่าเดียวกันกับ userId หรือ roomId หรือ groupId ขึ้นกับว่าเป็น event แบบใด
$sourceId = $eventObj->getEventSourceId();
// ดึงค่า replyToken มาไว้ใช้งาน ทุกๆ Event ที่ไม่ใช่ Leave และ Unfollow Event และ  MemberLeft
// replyToken ไว้สำหรับส่งข้อความจอบกลับ
if(is_null($eventLeave) && is_null($eventUnfollow) && is_null($eventMemberLeft)){
    $replyToken = $eventObj->getReplyToken();   
}
 
////////////////////////////  ส่วนของการทำงาน
 include_once("bot_action.php");
//////////////////////////
 
$response = $bot->replyMessage($replyToken,$replyData);
if ($response->isSucceeded()) {
    echo 'Succeeded!';
    return;
}
// Failed
echo $response->getHTTPStatus() . ' ' . $response->getRawBody();
?>
 
ไฟล์นี้จะเป็นไฟล์สำหรับ webhook url จะไม่ได้ต้องแก้ไขอะไรมากนักในไฟล์นี้ เพราะการทำงาน เรา
จะเอาไปกำหนดในไฟล์ bot_action.php 
 

ไฟล์ bot_action.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
<?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\Event\AccountLinkEvent;
use LINE\LINEBot\Event\MemberJoinEvent;
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;
use LINE\LINEBot\QuickReplyBuilder;
use LINE\LINEBot\QuickReplyBuilder\QuickReplyMessageBuilder;
use LINE\LINEBot\QuickReplyBuilder\ButtonBuilder\QuickReplyButtonBuilder;
use LINE\LINEBot\TemplateActionBuilder\CameraRollTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\CameraTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\LocationTemplateActionBuilder;
use LINE\LINEBot\RichMenuBuilder;
use LINE\LINEBot\RichMenuBuilder\RichMenuSizeBuilder;
use LINE\LINEBot\RichMenuBuilder\RichMenuAreaBuilder;
use LINE\LINEBot\RichMenuBuilder\RichMenuAreaBoundsBuilder;
use LINE\LINEBot\Constant\Flex\ComponentIconSize;
use LINE\LINEBot\Constant\Flex\ComponentImageSize;
use LINE\LINEBot\Constant\Flex\ComponentImageAspectRatio;
use LINE\LINEBot\Constant\Flex\ComponentImageAspectMode;
use LINE\LINEBot\Constant\Flex\ComponentFontSize;
use LINE\LINEBot\Constant\Flex\ComponentFontWeight;
use LINE\LINEBot\Constant\Flex\ComponentMargin;
use LINE\LINEBot\Constant\Flex\ComponentSpacing;
use LINE\LINEBot\Constant\Flex\ComponentButtonStyle;
use LINE\LINEBot\Constant\Flex\ComponentButtonHeight;
use LINE\LINEBot\Constant\Flex\ComponentSpaceSize;
use LINE\LINEBot\Constant\Flex\ComponentGravity;
use LINE\LINEBot\Constant\Flex\BubleContainerSize;
use LINE\LINEBot\MessageBuilder\FlexMessageBuilder;
use LINE\LINEBot\MessageBuilder\Flex\BubbleStylesBuilder;
use LINE\LINEBot\MessageBuilder\Flex\BlockStyleBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ContainerBuilder\BubbleContainerBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ContainerBuilder\CarouselContainerBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\BoxComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\ButtonComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\IconComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\ImageComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\SpacerComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\FillerComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\SeparatorComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\TextComponentBuilder;
use LINE\LINEBot\MessageBuilder\Flex\ComponentBuilder\SpanComponentBuilder;
 
// ไฟล์ฟังก์ชั่นสำหรับแปลง xml เป็น flex
require_once("flex_gen.php");
 
// ส่วนของการทำงาน
if(!is_null($events)){
 
 
 
}
?>
 
ไฟล์นี้จะเป็นส่วนของไฟล์หลัก ที่เราจะกำหนดคำสั่งและการทำงานต่างๆ ซึ่ง ถ้าเราสนใจเกี่ยวกับ
การจัดการเรื่องใดๆ ก็ไปดูบทความในตอนที่ 1 - 12 และเพิ่มเฉพาะโค้ดที่ต้องการ
 
เมื่อเราได้ไฟล์ครบทั้ง 4 ไฟล์ที่จำเป็นแล้ว และกำหนดค่าต่างๆ ครบแล้ว เราก็ทดสอบเพิ่มโค้ด
การใช้งานเบื้องต้นกันดังนี้
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// ส่วนของการทำงาน
if(!is_null($events)){
 
    //////////////////////////// Message event //////////////////////////////////
    // ถ้าเป้น Message Event
    if(!is_null($eventMessage)){
         
        // สร้างตัวแปรเก็ยค่าประเภทของ Message จากทั้งหมด 7 ประเภท
        $typeMessage = $eventObj->getMessageType(); 
        //  text | image | sticker | location | audio | video | file 
        // เก็บค่า id ของข้อความ
        $idMessage = $eventObj->getMessageId();         
         
        // ถ้าเป็นข้อความ
        if($typeMessage=='text'){
            $userMessage = $eventObj->getText(); // เก็บค่าข้อความที่ผู้ใช้พิมพ์
            $message = $userMessage;
            $replyData = new TextMessageBuilder($message);                                 
        }
         
    }
 
}
 
ถ้าผู้ใช้พิมพ์ข้อความอะไรมา ก็ให้ตอบกลับเป็นข้อความนั้น การทำงานก็จะได้ผลลัพทธ์ดังรูปด้านล่าง
 
 

 
 
จะเห็นว่า แค่นี้เราก็สามารถเริ่มต้น การเขียนคำสั่งโต้ตอบกับผู้ใช้ผ่าน bot ได้อย่างง่ายๆ 
อย่างที่บอกไปว่า โค้ดนี้เราตัดส่วนของการใช้งานอื่นๆ ออกให้หมด และเอาเฉพาะค่าเริ่มต้น แล้วถ้า
ต้องการส่วนไหน ก็ค่อยเพิ่มเข้าไป โดยดูเนื้อหาจากบทความทั้ง 12 ตอน
 
เรามาทดสอบเพิ่มอีก สมมติอยากให้ ตอบกลับแบบหลายข้อความ หรือเป็นข้อความ 1 ข้อความและ
เป็น template อีก 1 แบบ ก็จะได้เป็นดังนี้
 
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
// ส่วนของการทำงาน
if(!is_null($events)){
 
    //////////////////////////// Message event //////////////////////////////////
    // ถ้าเป้น Message Event
    if(!is_null($eventMessage)){
         
        // สร้างตัวแปรเก็ยค่าประเภทของ Message จากทั้งหมด 7 ประเภท
        $typeMessage = $eventObj->getMessageType(); 
        //  text | image | sticker | location | audio | video | file 
        // เก็บค่า id ของข้อความ
        $idMessage = $eventObj->getMessageId();         
         
        // ถ้าเป็นข้อความ
        if($typeMessage=='text'){
 
            // ส่วนกำหนด ตอบกลับ ที่เป็น ข้อความ
            $msg = "Bot ตอบกลับคุณเป็นข้อความ";
            $textMessage = new TextMessageBuilder($msg);
 
            // ส่วนกำหนดตอบกลับที่เป็น template
            // กำหนด action 4 ปุ่ม 4 ประเภท
            $actionBuilder = array(
                new MessageTemplateActionBuilder(
                    'Message Template',// ข้อความแสดงในปุ่ม
                    'This is Text' // ข้อความที่จะแสดงฝั่งผู้ใช้ เมื่อคลิกเลือก
                ),
                new UriTemplateActionBuilder(
                    'Uri Template', // ข้อความแสดงในปุ่ม
                    'https://www.ninenik.com'
                ),
                new DatetimePickerTemplateActionBuilder(
                    'Datetime Picker', // ข้อความแสดงในปุ่ม
                    http_build_query(array(
                        'action'=>'reservation',
                        'person'=>5
                    )), // ข้อมูลที่จะส่งไปใน webhook ผ่าน postback event
                    'datetime', // date | time | datetime รูปแบบข้อมูลที่จะส่ง ในที่นี้ใช้ datatime
                    substr_replace(date("Y-m-d H:i"),'T',10,1), // วันที่ เวลา ค่าเริ่มต้นที่ถูกเลือก
                    substr_replace(date("Y-m-d H:i",strtotime("+5 day")),'T',10,1), //วันที่ เวลา มากสุดที่เลือกได้
                    substr_replace(date("Y-m-d H:i"),'T',10,1) //วันที่ เวลา น้อยสุดที่เลือกได้
                ),     
                new PostbackTemplateActionBuilder(
                    'Postback', // ข้อความแสดงในปุ่ม
                    http_build_query(array(
                        'action'=>'buy',
                        'item'=>100
                    )), // ข้อมูลที่จะส่งไปใน webhook ผ่าน postback event
                    'Postback Text'  // ข้อความที่จะแสดงฝั่งผู้ใช้ เมื่อคลิกเลือก
                ),     
            );
            $templateMessage = new TemplateMessageBuilder('Button Template',
                new ButtonTemplateBuilder(
                        'button template builder', // กำหนดหัวเรื่อง
                        'Please select', // กำหนดรายละเอียด
                        $imageUrl, // กำหนด url รุปภาพ
                        $actionBuilder  // กำหนด action object
                )
            );    
 
            // ส่งข้อความแบบหลายรูปแบบพร้อมกัน
            $multiMessage =     new MultiMessageBuilder;
            $multiMessage->add($textMessage);
            $multiMessage->add($templateMessage);
            $replyData = $multiMessage;    
 
        }
         
    }
 
}
 
ผลลัพธ์ก็จะได้เป็น ดังนี้ รูปแรกผ่านเครื่อง PC จะมองไม่เห็นการแสดงผลของ template
 
 

 
 
ผ่านมือถือ
 
 

 
 
ข้างต้นเป็นแนวทางตัวอย่างเท่านั้น สิ่งสำคัญในการกำหนด จะให้ทำงานอย่างไร ก็คือ เราต้องรู้
วัตถุประสงค์ ของการทำงานของ bot ว่าจะให้ครอบคลุมแค่ไหน ในตัวอย่างเป็นการเช็คแค่ว่า
ถ้ามีข้อความมา ก็ตอบกลับตามที่กำหนด ไม่ได้เช็คว่า เป็นข้อความคำว่าอะไร หรืออื่นๆ ที่ซับซ้อน
 
เงื่อนไขต่างๆ ที่เราสามารถกำหนดเพิ่ม เช่น 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ถ้าเป็น image
if($typeMessage=='image'){
 
}              
// ถ้าเป็น audio
 
if($typeMessage=='audio'){
 
}      
// ถ้าเป็น video
if($typeMessage=='video'){
 
}  
// ถ้าเป็น file
if($typeMessage=='file'){
    $FileName = $eventObj->getFileName();
    $FileSize = $eventObj->getFileSize();
}
 
และอื่นๆ อีกมากมาย จะมีแทรกอยู่ในแต่ละบทความ  
สมมติเราสนใจ อยากให้มีการใช้งาน quick reply ก็ให้ดูบทความตอนที่ 7
 
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
// ส่วนของการทำงาน
if(!is_null($events)){
 
    //////////////////////////// Message event //////////////////////////////////
    // ถ้าเป้น Message Event
    if(!is_null($eventMessage)){
         
        // สร้างตัวแปรเก็ยค่าประเภทของ Message จากทั้งหมด 7 ประเภท
        $typeMessage = $eventObj->getMessageType(); 
        //  text | image | sticker | location | audio | video | file 
        // เก็บค่า id ของข้อความ
        $idMessage = $eventObj->getMessageId();         
         
        // ถ้าเป็นข้อความ
        if($typeMessage=='text'){
            $userMessage = $eventObj->getText(); // เก็บค่าข้อความที่ผู้ใช้พิมพ์
             
            // ถ้าส่งข้อความคำว่า help เข้ามา
            if($userMessage == "help"){
             
               $postback = new PostbackTemplateActionBuilder(
                    'Postback', // ข้อความแสดงในปุ่ม
                    http_build_query(array(
                        'action'=>'buy',
                        'item'=>100
                    )), // ข้อมูลที่จะส่งไปใน webhook ผ่าน postback event
                     'Buy'  // ข้อความที่จะแสดงฝั่งผู้ใช้ เมื่อคลิกเลือก
                );
                $txtMsg = new MessageTemplateActionBuilder(
                    'ข้อความภาษาไทย',// ข้อความแสดงในปุ่ม
                    'thai' // ข้อความที่จะแสดงฝั่งผู้ใช้ เมื่อคลิกเลือก
                );
                $datetimePicker = new DatetimePickerTemplateActionBuilder(
                    'Datetime Picker', // ข้อความแสดงในปุ่ม
                    http_build_query(array(
                        'action'=>'reservation',
                        'person'=>5
                    )), // ข้อมูลที่จะส่งไปใน webhook ผ่าน postback event
                    'datetime', // date | time | datetime รูปแบบข้อมูลที่จะส่ง ในที่นี้ใช้ datatime
                    substr_replace(date("Y-m-d H:i"),'T',10,1), // วันที่ เวลา ค่าเริ่มต้นที่ถูกเลือก
                    substr_replace(date("Y-m-d H:i",strtotime("+5 day")),'T',10,1), //วันที่ เวลา มากสุดที่เลือกได้
                    substr_replace(date("Y-m-d H:i"),'T',10,1) //วันที่ เวลา น้อยสุดที่เลือกได้
                );
                $quickReply = new QuickReplyMessageBuilder(
                    array(
                        new QuickReplyButtonBuilder(new LocationTemplateActionBuilder('Location')),
                        new QuickReplyButtonBuilder(new CameraTemplateActionBuilder('Camera')),
                        new QuickReplyButtonBuilder(new CameraRollTemplateActionBuilder('Camera roll')),
                        new QuickReplyButtonBuilder($postback),
                        new QuickReplyButtonBuilder($datetimePicker),
                        new QuickReplyButtonBuilder(
                            $txtMsg,
                            "https://www.ninenik.com/images/ninenik_page_logo.png"
                        ),
                    )
                );
                $textReplyMessage = "ลองเลือกตัวช่วยด้านล่างสิ ";
                $replyData = new TextMessageBuilder($textReplyMessage,$quickReply);                
                 
            }else{ // ถ้าส่งข้อความอื่นที่ไม่ใ่ช่คำว่า help เข้ามา
                // เราจะให้ส่งปุ่ม ที่ให้กด แล้วมีข้อความคำว่า help 
                // แล้วก็จะเข้าเงื่อนไขด้านบน เปิดการแสดง quick reply
                 
                $replyData = new TemplateMessageBuilder('Confirm Template',
                    new ConfirmTemplateBuilder(
                            'ต้องการความช่วยเหลือไหม?', // ข้อความแนะนำหรือบอกวิธีการ หรือคำอธิบาย
                            array(
                                new MessageTemplateActionBuilder(
                                    'ใช่', // ข้อความสำหรับปุ่มแรก
                                    'help'  // ข้อความที่จะแสดงฝั่งผู้ใช้ เมื่อคลิกเลือก
                                ),
                                new MessageTemplateActionBuilder(
                                    'ไม่', // ข้อความสำหรับปุ่มแรก
                                    'No' // ข้อความที่จะแสดงฝั่งผู้ใช้ เมื่อคลิกเลือก
                                )
                            )
                    )
                );         
             
            }
        }
         
    }
 
}
 
เมื่อเราทักไป ก็จะมีปุ่มยืนยัน หรือปุ่มตัวเลือก พอเรากด ใช่ ก็จะส่งคำว่า help เข้าเงื่อนไขให้ตอบกลับ
เป็น quick reply ก็จะมีตัวเลือกขึ้นมาให้เราทำคำสั่งพิเศษที่ต้องการ ดูรูปประกอบ
 
 

 
 

 
 
เท่านี้เราก็ได้ไฟล์ตั้งต้นสำหรับใช้งาน Line bot sdk แบบรวมรัด ซึ่งยิ่งเราต้องการกำหนดความสามารถ
ของ bot มากเท่าไหร่ ก็จำเป็นต้องกำหนดเงื่อนไขเพิ่มมากขึ้นตามไปด้วย หรือจะใช้งานร่วมกับ 
DialogFlow ก็จะช่วยลดขั้นตอนการเขียนโค้ดไปได้เยอะ 
 
หวังว่าจะเป็นแนวทางนำไปประยุกต์ใช้งานต่อไป อาจจะมีประโยชน์ไม่มากก็น้อย


กด Like หรือ Share เป็นกำลังใจ ให้มีบทความใหม่ๆ เรื่อยๆ น่ะครับ







เนื้อหาที่เกี่ยวข้อง






เนื้อหาพิเศษ เฉพาะสำหรับสมาชิก

กรุณาล็อกอิน เพื่ออ่านเนื้อหาบทความ

ยังไม่เป็นสมาชิก

สมาชิกล็อกอิน



( หรือ เข้าใช้งานผ่าน Social Login )




URL สำหรับอ้างอิง










เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ