Skip to content

Commit 99e7e90

Browse files
authored
Merge pull request #51 from line/fix_template_message/47
Change to be able to set text field to PostbackTemplate
2 parents 6987d93 + 82e4af0 commit 99e7e90

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/LINEBot/TemplateActionBuilder/PostbackTemplateActionBuilder.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,21 @@ class PostbackTemplateActionBuilder implements TemplateActionBuilder
3232
private $label;
3333
/** @var string */
3434
private $data;
35+
/** @var string|null */
36+
private $text;
3537

3638
/**
3739
* PostbackAction constructor.
3840
*
3941
* @param string $label Label of action.
4042
* @param string $data Data of postback.
43+
* @param string|null $text The text which will be sent when action is executed (optional).
4144
*/
42-
public function __construct($label, $data)
45+
public function __construct($label, $data, $text = null)
4346
{
4447
$this->label = $label;
4548
$this->data = $data;
49+
$this->text = $text;
4650
}
4751

4852
/**
@@ -52,10 +56,17 @@ public function __construct($label, $data)
5256
*/
5357
public function buildTemplateAction()
5458
{
55-
return [
59+
$action = [
5660
'type' => ActionType::POSTBACK,
5761
'label' => $this->label,
5862
'data' => $this->data,
5963
];
64+
65+
if (isset($this->text)) {
66+
// If text is set, append extend field.
67+
$action['text'] = $this->text;
68+
}
69+
70+
return $action;
6071
}
6172
}

tests/LINEBot/SendTemplateTest.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
namespace LINE\Tests\LINEBot;
2020

2121
use LINE\LINEBot;
22-
use LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder;
23-
use LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder;
24-
use LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder;
2522
use LINE\LINEBot\Constant\ActionType;
2623
use LINE\LINEBot\Constant\MessageType;
2724
use LINE\LINEBot\Constant\TemplateType;
2825
use LINE\LINEBot\MessageBuilder\TemplateBuilder\ButtonTemplateBuilder;
26+
use LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder;
27+
use LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder;
28+
use LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder;
2929
use LINE\Tests\LINEBot\Util\DummyHttpClient;
3030

3131
class SendTemplateTest extends \PHPUnit_Framework_TestCase
@@ -110,18 +110,22 @@ public function testPushTemplate()
110110
$testRunner->assertEquals('https://example.com/thumbnail.jpg', $template['thumbnailImageUrl']);
111111

112112
$actions = $template['actions'];
113-
$testRunner->assertEquals(3, count($actions));
113+
$testRunner->assertEquals(4, count($actions));
114114
$testRunner->assertEquals(ActionType::POSTBACK, $actions[0]['type']);
115115
$testRunner->assertEquals('postback label', $actions[0]['label']);
116116
$testRunner->assertEquals('post=back', $actions[0]['data']);
117117

118-
$testRunner->assertEquals(ActionType::MESSAGE, $actions[1]['type']);
119-
$testRunner->assertEquals('message label', $actions[1]['label']);
120-
$testRunner->assertEquals('test message', $actions[1]['text']);
118+
$testRunner->assertEquals(ActionType::POSTBACK, $actions[1]['type']);
119+
$testRunner->assertEquals('postback label2', $actions[1]['label']);
120+
$testRunner->assertEquals('post=back2', $actions[1]['data']);
121121

122-
$testRunner->assertEquals(ActionType::URI, $actions[2]['type']);
123-
$testRunner->assertEquals('uri label', $actions[2]['label']);
124-
$testRunner->assertEquals('https://example.com', $actions[2]['uri']);
122+
$testRunner->assertEquals(ActionType::MESSAGE, $actions[2]['type']);
123+
$testRunner->assertEquals('message label', $actions[2]['label']);
124+
$testRunner->assertEquals('test message', $actions[2]['text']);
125+
126+
$testRunner->assertEquals(ActionType::URI, $actions[3]['type']);
127+
$testRunner->assertEquals('uri label', $actions[3]['label']);
128+
$testRunner->assertEquals('https://example.com', $actions[3]['uri']);
125129

126130
return ['status' => 200];
127131
};
@@ -136,6 +140,7 @@ public function testPushTemplate()
136140
'https://example.com/thumbnail.jpg',
137141
[
138142
new PostbackTemplateActionBuilder('postback label', 'post=back'),
143+
new PostbackTemplateActionBuilder('postback label2', 'post=back2', 'extend text'),
139144
new MessageTemplateActionBuilder('message label', 'test message'),
140145
new UriTemplateActionBuilder('uri label', 'https://example.com'),
141146
]

0 commit comments

Comments
 (0)