Skip to content

Commit b534664

Browse files
committed
Change to be able to set text field to PostbackTemplate
Fix #47
1 parent cc21aa4 commit b534664

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
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: 50 additions & 3 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
@@ -147,4 +147,51 @@ public function testPushTemplate()
147147
$this->assertTrue($res->isSucceeded());
148148
$this->assertEquals(200, $res->getJSONDecodedBody()['status']);
149149
}
150+
151+
public function testPushTemplateWithText()
152+
{
153+
// Support text field on postback template
154+
// Ref: https://github.com/line/line-bot-sdk-php/issues/47
155+
$mock = function ($testRunner, $httpMethod, $url, $data) {
156+
/** @var \PHPUnit_Framework_TestCase $testRunner */
157+
$testRunner->assertEquals('POST', $httpMethod);
158+
$testRunner->assertEquals('https://api.line.me/v2/bot/message/push', $url);
159+
160+
$testRunner->assertEquals('DESTINATION', $data['to']);
161+
$testRunner->assertEquals(1, count($data['messages']));
162+
163+
$message = $data['messages'][0];
164+
$template = $message['template'];
165+
$actions = $template['actions'];
166+
167+
$testRunner->assertEquals(3, count($actions));
168+
$testRunner->assertEquals(ActionType::POSTBACK, $actions[0]['type']);
169+
$testRunner->assertEquals('postback label', $actions[0]['label']);
170+
$testRunner->assertEquals('post=back', $actions[0]['data']);
171+
$testRunner->assertEquals('extend text', $actions[0]['text']);
172+
173+
return ['status' => 200];
174+
};
175+
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
176+
$res = $bot->pushMessage(
177+
'DESTINATION',
178+
new LINEBot\MessageBuilder\TemplateMessageBuilder(
179+
'alt test',
180+
new ButtonTemplateBuilder(
181+
'button title',
182+
'button button',
183+
'https://example.com/thumbnail.jpg',
184+
[
185+
new PostbackTemplateActionBuilder('postback label', 'post=back', 'extend text'), // <= Test here
186+
new MessageTemplateActionBuilder('message label', 'test message'),
187+
new UriTemplateActionBuilder('uri label', 'https://example.com'),
188+
]
189+
)
190+
)
191+
);
192+
193+
$this->assertEquals(200, $res->getHTTPStatus());
194+
$this->assertTrue($res->isSucceeded());
195+
$this->assertEquals(200, $res->getJSONDecodedBody()['status']);
196+
}
150197
}

0 commit comments

Comments
 (0)