Skip to content

Commit cb86697

Browse files
author
Satoru Yoshihara
committed
Implemented datetime picker
1 parent 767a3f7 commit cb86697

File tree

5 files changed

+232
-18
lines changed

5 files changed

+232
-18
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/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: 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(array("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(array("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(array("datetime" => "2013-04-01T10:00"), $event->getPostbackParams());
525+
}
450526
}
451527
}

tests/LINEBot/SendTemplateTest.php

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder;
2929
use LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder;
3030
use LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder;
31+
use LINE\LINEBot\TemplateActionBuilder\DatetimePickerTemplateActionBuilder;
3132
use LINE\Tests\LINEBot\Util\DummyHttpClient;
3233

3334
class SendTemplateTest extends \PHPUnit_Framework_TestCase
@@ -176,24 +177,34 @@ public function testImageCarouselTemplate()
176177
$columns = $template['columns'];
177178
$testRunner->assertEquals(4, count($columns));
178179
$testRunner->assertEquals('https://example.com/image1.png', $columns[0]['imageUrl']);
179-
$testRunner->assertEquals(ActionType::POSTBACK, $columns[0]['action']['type']);
180-
$testRunner->assertEquals('postback label', $columns[0]['action']['label']);
181-
$testRunner->assertEquals('post=back', $columns[0]['action']['data']);
180+
$testRunner->assertEquals(ActionType::DATETIME_PICKER, $columns[0]['action']['type']);
181+
$testRunner->assertEquals('datetime picker date', $columns[0]['action']['label']);
182+
$testRunner->assertEquals('action=sell&itemid=2&mode=date', $columns[0]['action']['data']);
183+
$testRunner->assertEquals('date', $columns[0]['action']['mode']);
184+
$testRunner->assertEquals('2013-04-01', $columns[0]['action']['initial']);
185+
$testRunner->assertEquals('2011-06-23', $columns[0]['action']['max']);
186+
$testRunner->assertEquals('2017-09-08', $columns[0]['action']['min']);
182187

183188
$testRunner->assertEquals('https://example.com/image2.png', $columns[1]['imageUrl']);
184-
$testRunner->assertEquals(ActionType::POSTBACK, $columns[1]['action']['type']);
185-
$testRunner->assertEquals('postback label2', $columns[1]['action']['label']);
186-
$testRunner->assertEquals('post=back2', $columns[1]['action']['data']);
189+
$testRunner->assertEquals(ActionType::DATETIME_PICKER, $columns[1]['action']['type']);
190+
$testRunner->assertEquals('datetime picker time', $columns[1]['action']['label']);
191+
$testRunner->assertEquals('action=sell&itemid=2&mode=time', $columns[1]['action']['data']);
192+
$testRunner->assertEquals('time', $columns[1]['action']['mode']);
193+
$testRunner->assertEquals('10:00', $columns[1]['action']['initial']);
194+
$testRunner->assertEquals('00:00', $columns[1]['action']['max']);
195+
$testRunner->assertEquals('23:59', $columns[1]['action']['min']);
187196

188197
$testRunner->assertEquals('https://example.com/image3.png', $columns[2]['imageUrl']);
189-
$testRunner->assertEquals(ActionType::MESSAGE, $columns[2]['action']['type']);
190-
$testRunner->assertEquals('message label', $columns[2]['action']['label']);
191-
$testRunner->assertEquals('test message', $columns[2]['action']['text']);
198+
$testRunner->assertEquals(ActionType::DATETIME_PICKER, $columns[2]['action']['type']);
199+
$testRunner->assertEquals('datetime picker date', $columns[2]['action']['label']);
200+
$testRunner->assertEquals('action=sell&itemid=2&mode=date', $columns[2]['action']['data']);
201+
$testRunner->assertEquals('date', $columns[2]['action']['mode']);
192202

193203
$testRunner->assertEquals('https://example.com/image4.png', $columns[3]['imageUrl']);
194-
$testRunner->assertEquals(ActionType::URI, $columns[3]['action']['type']);
195-
$testRunner->assertEquals('uri label', $columns[3]['action']['label']);
196-
$testRunner->assertEquals('https://example.com', $columns[3]['action']['uri']);
204+
$testRunner->assertEquals(ActionType::DATETIME_PICKER, $columns[3]['action']['type']);
205+
$testRunner->assertEquals('datetime picker time', $columns[3]['action']['label']);
206+
$testRunner->assertEquals('action=sell&itemid=2&mode=time', $columns[3]['action']['data']);
207+
$testRunner->assertEquals('time', $columns[3]['action']['mode']);
197208

198209
return ['status' => 200];
199210
};
@@ -206,19 +217,41 @@ public function testImageCarouselTemplate()
206217
[
207218
new ImageCarouselColumnTemplateBuilder(
208219
'https://example.com/image1.png',
209-
new PostbackTemplateActionBuilder('postback label', 'post=back')
220+
new DatetimePickerTemplateActionBuilder(
221+
'datetime picker date',
222+
'action=sell&itemid=2&mode=date',
223+
'date',
224+
'2013-04-01',
225+
'2011-06-23',
226+
'2017-09-08'
227+
)
210228
),
211229
new ImageCarouselColumnTemplateBuilder(
212230
'https://example.com/image2.png',
213-
new PostbackTemplateActionBuilder('postback label2', 'post=back2', 'extend text')
231+
new DatetimePickerTemplateActionBuilder(
232+
'datetime picker time',
233+
'action=sell&itemid=2&mode=time',
234+
'time',
235+
'10:00',
236+
'00:00',
237+
'23:59'
238+
)
214239
),
215240
new ImageCarouselColumnTemplateBuilder(
216241
'https://example.com/image3.png',
217-
new MessageTemplateActionBuilder('message label', 'test message')
242+
new DatetimePickerTemplateActionBuilder(
243+
'datetime picker date',
244+
'action=sell&itemid=2&mode=date',
245+
'date'
246+
)
218247
),
219248
new ImageCarouselColumnTemplateBuilder(
220249
'https://example.com/image4.png',
221-
new UriTemplateActionBuilder('uri label', 'https://example.com')
250+
new DatetimePickerTemplateActionBuilder(
251+
'datetime picker time',
252+
'action=sell&itemid=2&mode=time',
253+
'time'
254+
)
222255
),
223256
]
224257
)

0 commit comments

Comments
 (0)