Skip to content

Commit d229c16

Browse files
authored
Merge pull request #103 from Vaduz/feature/image_carousel
Support image carousel message and datetime picker
2 parents 8c8ca4a + 2c37845 commit d229c16

File tree

8 files changed

+433
-2
lines changed

8 files changed

+433
-2
lines changed

src/LINEBot/Constant/ActionType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ class ActionType
2323
const MESSAGE = 'message';
2424
const POSTBACK = 'postback';
2525
const URI = 'uri';
26+
const DATETIME_PICKER = 'datetimepicker';
2627
}

src/LINEBot/Constant/TemplateType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ class TemplateType
2323
const CONFIRM = 'confirm';
2424
const BUTTONS = 'buttons';
2525
const CAROUSEL = 'carousel';
26+
const IMAGE_CAROUSEL = 'image_carousel';
2627
}

src/LINEBot/Event/PostbackEvent.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,16 @@ public function getPostbackData()
4444
{
4545
return $this->event['postback']['data'];
4646
}
47+
48+
/**
49+
* Returns the params of postback.
50+
*
51+
* @return array|null
52+
*/
53+
public function getPostbackParams()
54+
{
55+
return array_key_exists('params', $this->event['postback'])
56+
? $this->event['postback']['params']
57+
: null;
58+
}
4759
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2016 LINE Corporation
5+
*
6+
* LINE Corporation licenses this file to you under the Apache License,
7+
* version 2.0 (the "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at:
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
* License for the specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
namespace LINE\LINEBot\MessageBuilder\TemplateBuilder;
20+
21+
use LINE\LINEBot\MessageBuilder\TemplateBuilder;
22+
use LINE\LINEBot\TemplateActionBuilder;
23+
24+
/**
25+
* A builder class for column of image carousel template.
26+
*
27+
* @package LINE\LINEBot\MessageBuilder\TemplateBuilder
28+
*/
29+
class ImageCarouselColumnTemplateBuilder implements TemplateBuilder
30+
{
31+
/** @var string */
32+
private $imageUrl;
33+
/** @var TemplateActionBuilder */
34+
private $actionBuilder;
35+
36+
/** @var array */
37+
private $template;
38+
39+
/**
40+
* ImageCarouselColumnTemplateBuilder constructor.
41+
*
42+
* @param string $imageUrl
43+
* @param TemplateActionBuilder $actionBuilder
44+
*/
45+
public function __construct($imageUrl, $actionBuilder)
46+
{
47+
$this->imageUrl = $imageUrl;
48+
$this->actionBuilder = $actionBuilder;
49+
}
50+
51+
/**
52+
* Builds column of image carousel template structure.
53+
*
54+
* @return array
55+
*/
56+
public function buildTemplate()
57+
{
58+
if (!empty($this->template)) {
59+
return $this->template;
60+
}
61+
62+
$this->template = [
63+
'imageUrl' => $this->imageUrl,
64+
'action' => $this->actionBuilder->buildTemplateAction(),
65+
];
66+
67+
return $this->template;
68+
}
69+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2016 LINE Corporation
5+
*
6+
* LINE Corporation licenses this file to you under the Apache License,
7+
* version 2.0 (the "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at:
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
* License for the specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
namespace LINE\LINEBot\MessageBuilder\TemplateBuilder;
20+
21+
use LINE\LINEBot\Constant\TemplateType;
22+
use LINE\LINEBot\MessageBuilder\TemplateBuilder;
23+
24+
/**
25+
* A builder class for image carousel template.
26+
*
27+
* @package LINE\LINEBot\MessageBuilder\TemplateBuilder
28+
*/
29+
class ImageCarouselTemplateBuilder implements TemplateBuilder
30+
{
31+
/** @var ImageCarouselColumnTemplateBuilder[] */
32+
private $columnTemplateBuilders;
33+
34+
/** @var array */
35+
private $template;
36+
37+
/**
38+
* ImageCarouselTemplateBuilder constructor.
39+
*
40+
* @param ImageCarouselColumnTemplateBuilder[] $columnTemplateBuilders
41+
*/
42+
public function __construct(array $columnTemplateBuilders)
43+
{
44+
$this->columnTemplateBuilders = $columnTemplateBuilders;
45+
}
46+
47+
/**
48+
* Builds image carousel template structure.
49+
*
50+
* @return array
51+
*/
52+
public function buildTemplate()
53+
{
54+
if (!empty($this->template)) {
55+
return $this->template;
56+
}
57+
58+
$columns = [];
59+
foreach ($this->columnTemplateBuilders as $columnTemplateBuilder) {
60+
$columns[] = $columnTemplateBuilder->buildTemplate();
61+
}
62+
63+
$this->template = [
64+
'type' => TemplateType::IMAGE_CAROUSEL,
65+
'columns' => $columns,
66+
];
67+
68+
return $this->template;
69+
}
70+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2016 LINE Corporation
5+
*
6+
* LINE Corporation licenses this file to you under the Apache License,
7+
* version 2.0 (the "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at:
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
* License for the specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
namespace LINE\LINEBot\TemplateActionBuilder;
20+
21+
use LINE\LINEBot\Constant\ActionType;
22+
use LINE\LINEBot\TemplateActionBuilder;
23+
24+
/**
25+
* A builder class for datetime picker action.
26+
*
27+
* When this action is tapped, a postback event is returned via webhook with the date and
28+
* time selected by the user from the date and time selection dialog.
29+
*
30+
* @package LINE\LINEBot\TemplateActionBuilder
31+
*/
32+
class DatetimePickerTemplateActionBuilder implements TemplateActionBuilder
33+
{
34+
/** @var string */
35+
private $label;
36+
/** @var string */
37+
private $data;
38+
/** @var string */
39+
private $mode;
40+
/** @var string */
41+
private $initial;
42+
/** @var string */
43+
private $max;
44+
/** @var string */
45+
private $min;
46+
47+
/**
48+
* DatetimePickerAction constructor.
49+
*
50+
* @param string $label Label for the action
51+
* Required for templates other than image carousel. Max: 20 characters
52+
* Optional for image carousel templates. Max: 12 characters.
53+
* @param string $data String returned via webhook in the postback.data property of the postback event
54+
* Max: 300 characters
55+
* @param string $mode Action mode
56+
* date: Pick date
57+
* time: Pick time
58+
* datetime: Pick date and time
59+
* @param string initial Initial value of date or time
60+
* @param string max Largest date or time value that can be selected.
61+
* Must be greater than the min value.
62+
* @param string min Smallest date or time value that can be selected.
63+
* Must be less than the max value.
64+
*/
65+
public function __construct($label, $data, $mode, $initial = null, $max = null, $min = null)
66+
{
67+
$this->label = $label;
68+
$this->data = $data;
69+
$this->mode = $mode;
70+
$this->initial = $initial;
71+
$this->max = $max;
72+
$this->min = $min;
73+
}
74+
75+
/**
76+
* Builds datetime picker action structure.
77+
*
78+
* @return array Built datetime picker action structure.
79+
*/
80+
public function buildTemplateAction()
81+
{
82+
return [
83+
'type' => ActionType::DATETIME_PICKER,
84+
'label' => $this->label,
85+
'data' => $this->data,
86+
'mode' => $this->mode,
87+
'initial' => $this->initial,
88+
'max' => $this->max,
89+
'min' => $this->min,
90+
];
91+
}
92+
}

tests/LINEBot/EventRequestParserTest.php

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,51 @@ class EventRequestParserTest extends \PHPUnit_Framework_TestCase
243243
"fileName": "file.txt",
244244
"fileSize": 2138
245245
}
246+
},
247+
{
248+
"replyToken": "replytoken",
249+
"type": "postback",
250+
"timestamp": 1501234567890,
251+
"source": {
252+
"type": "user",
253+
"userId": "userid"
254+
},
255+
"postback": {
256+
"data":"postback",
257+
"params": {
258+
"date": "2013-04-01"
259+
}
260+
}
261+
},
262+
{
263+
"replyToken": "replytoken",
264+
"type": "postback",
265+
"timestamp": 1501234567890,
266+
"source": {
267+
"type": "user",
268+
"userId": "userid"
269+
},
270+
"postback": {
271+
"data":"postback",
272+
"params": {
273+
"time": "10:00"
274+
}
275+
}
276+
},
277+
{
278+
"replyToken": "replytoken",
279+
"type": "postback",
280+
"timestamp": 1501234567890,
281+
"source": {
282+
"type": "user",
283+
"userId": "userid"
284+
},
285+
"postback": {
286+
"data":"postback",
287+
"params": {
288+
"datetime": "2013-04-01T10:00"
289+
}
290+
}
246291
}
247292
]
248293
}
@@ -252,9 +297,9 @@ public function testParseEventRequest()
252297
{
253298
$bot = new LINEBot(new DummyHttpClient($this, function () {
254299
}), ['channelSecret' => 'testsecret']);
255-
$events = $bot->parseEventRequest($this::$json, '56hhe+3PxvEaYotu9e2ZXmYQ0RMc6/74/jNyKQ6w6U0=');
300+
$events = $bot->parseEventRequest($this::$json, 'a4mKmptGCa6Kx/5PU6Ug3zC2SyLzzf9whz9/FbaR4HQ=');
256301

257-
$this->assertEquals(count($events), 17);
302+
$this->assertEquals(count($events), 20);
258303

259304
{
260305
// text
@@ -387,6 +432,7 @@ public function testParseEventRequest()
387432
/** @var PostbackEvent $event */
388433
$this->assertEquals('replytoken', $event->getReplyToken());
389434
$this->assertEquals('postback', $event->getPostbackData());
435+
$this->assertEquals(null, $event->getPostbackParams());
390436
}
391437

392438
{
@@ -447,5 +493,35 @@ public function testParseEventRequest()
447493
$this->assertEquals('325708', $event->getMessageId());
448494
$this->assertEquals('file', $event->getMessageType());
449495
}
496+
497+
{
498+
// postback date
499+
$event = $events[17];
500+
$this->assertInstanceOf('LINE\LINEBot\Event\PostbackEvent', $event);
501+
/** @var PostbackEvent $event */
502+
$this->assertEquals('replytoken', $event->getReplyToken());
503+
$this->assertEquals('postback', $event->getPostbackData());
504+
$this->assertEquals(["date" => "2013-04-01"], $event->getPostbackParams());
505+
}
506+
507+
{
508+
// postback time
509+
$event = $events[18];
510+
$this->assertInstanceOf('LINE\LINEBot\Event\PostbackEvent', $event);
511+
/** @var PostbackEvent $event */
512+
$this->assertEquals('replytoken', $event->getReplyToken());
513+
$this->assertEquals('postback', $event->getPostbackData());
514+
$this->assertEquals(["time" => "10:00"], $event->getPostbackParams());
515+
}
516+
517+
{
518+
// postback datetime
519+
$event = $events[19];
520+
$this->assertInstanceOf('LINE\LINEBot\Event\PostbackEvent', $event);
521+
/** @var PostbackEvent $event */
522+
$this->assertEquals('replytoken', $event->getReplyToken());
523+
$this->assertEquals('postback', $event->getPostbackData());
524+
$this->assertEquals(["datetime" => "2013-04-01T10:00"], $event->getPostbackParams());
525+
}
450526
}
451527
}

0 commit comments

Comments
 (0)