Skip to content

Commit 4d04ec1

Browse files
authored
Merge branch 'master' into feature/access-token-v21
2 parents b468e79 + 9369440 commit 4d04ec1

File tree

6 files changed

+107
-15
lines changed

6 files changed

+107
-15
lines changed

src/LINEBot.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
namespace LINE;
2020

2121
use LINE\LINEBot\Event\Parser\EventRequestParser;
22+
use LINE\LINEBot\Constant\HTTPHeader;
2223
use LINE\LINEBot\HTTPClient;
2324
use LINE\LINEBot\MessageBuilder;
2425
use LINE\LINEBot\MessageBuilder\TextMessageBuilder;
@@ -170,15 +171,20 @@ public function replyText($replyToken, $text, $extraTexts = null)
170171
* @param string $to Identifier of destination.
171172
* @param MessageBuilder $messageBuilder Message builder to send.
172173
* @param boolean $notificationDisabled Don't send push notifications(=true) or send(=false)
174+
* @param string|null $retryKey UUID(example: 123e4567-e89b-12d3-a456-426614174000) or Not needed retry(=null)
173175
* @return Response
174176
*/
175-
public function pushMessage($to, MessageBuilder $messageBuilder, $notificationDisabled = false)
177+
public function pushMessage($to, MessageBuilder $messageBuilder, $notificationDisabled = false, $retryKey = null)
176178
{
179+
$headers = ['Content-Type: application/json; charset=utf-8'];
180+
if (isset($retryKey)) {
181+
$headers[] = HTTPHeader::LINE_RETRY_KEY . ': ' .$retryKey;
182+
}
177183
return $this->httpClient->post($this->endpointBase . '/v2/bot/message/push', [
178184
'to' => $to,
179185
'messages' => $messageBuilder->buildMessage(),
180186
'notificationDisabled' => $notificationDisabled,
181-
]);
187+
], $headers);
182188
}
183189

184190
/**
@@ -187,15 +193,24 @@ public function pushMessage($to, MessageBuilder $messageBuilder, $notificationDi
187193
* @param array $tos Identifiers of destination.
188194
* @param MessageBuilder $messageBuilder Message builder to send.
189195
* @param boolean $notificationDisabled Don't send push notifications(=true) or send(=false)
196+
* @param string|null $retryKey UUID(example: 123e4567-e89b-12d3-a456-426614174000) or Not needed retry(=null)
190197
* @return Response
191198
*/
192-
public function multicast(array $tos, MessageBuilder $messageBuilder, $notificationDisabled = false)
193-
{
199+
public function multicast(
200+
array $tos,
201+
MessageBuilder $messageBuilder,
202+
$notificationDisabled = false,
203+
$retryKey = null
204+
) {
205+
$headers = ['Content-Type: application/json; charset=utf-8'];
206+
if (isset($retryKey)) {
207+
$headers[] = HTTPHeader::LINE_RETRY_KEY . ': ' .$retryKey;
208+
}
194209
return $this->httpClient->post($this->endpointBase . '/v2/bot/message/multicast', [
195210
'to' => $tos,
196211
'messages' => $messageBuilder->buildMessage(),
197212
'notificationDisabled' => $notificationDisabled,
198-
]);
213+
], $headers);
199214
}
200215

201216
/**
@@ -204,14 +219,19 @@ public function multicast(array $tos, MessageBuilder $messageBuilder, $notificat
204219
*
205220
* @param MessageBuilder $messageBuilder Message builder to send.
206221
* @param boolean $notificationDisabled Don't send push notifications(=true) or send(=false)
222+
* @param string|null $retryKey UUID(example: 123e4567-e89b-12d3-a456-426614174000) or Not needed retry(=null)
207223
* @return Response
208224
*/
209-
public function broadcast(MessageBuilder $messageBuilder, $notificationDisabled = false)
225+
public function broadcast(MessageBuilder $messageBuilder, $notificationDisabled = false, $retryKey = null)
210226
{
227+
$headers = ['Content-Type: application/json; charset=utf-8'];
228+
if (isset($retryKey)) {
229+
$headers[] = HTTPHeader::LINE_RETRY_KEY . ': ' .$retryKey;
230+
}
211231
return $this->httpClient->post($this->endpointBase . '/v2/bot/message/broadcast', [
212232
'messages' => $messageBuilder->buildMessage(),
213233
'notificationDisabled' => $notificationDisabled,
214-
]);
234+
], $headers);
215235
}
216236

217237
/**
@@ -802,13 +822,15 @@ public function getChannelAccessToken21Keys($jwt)
802822
* @param RecipientBuilder|null $recipientBuilder
803823
* @param DemographicFilterBuilder|null $demographicFilterBuilder
804824
* @param int|null $limit
825+
* @param string|null $retryKey UUID(example: 123e4567-e89b-12d3-a456-426614174000) or Not needed retry(=null)
805826
* @return Response
806827
*/
807828
public function sendNarrowcast(
808829
MessageBuilder $messageBuilder,
809830
RecipientBuilder $recipientBuilder = null,
810831
DemographicFilterBuilder $demographicFilterBuilder = null,
811-
$limit = null
832+
$limit = null,
833+
$retryKey = null
812834
) {
813835
$params = [
814836
'messages' => $messageBuilder->buildMessage()
@@ -826,7 +848,11 @@ public function sendNarrowcast(
826848
'max' => $limit
827849
];
828850
}
829-
return $this->httpClient->post($this->endpointBase . '/v2/bot/message/narrowcast', $params);
851+
$headers = ['Content-Type: application/json; charset=utf-8'];
852+
if (isset($retryKey)) {
853+
$headers[] = HTTPHeader::LINE_RETRY_KEY . ': ' .$retryKey;
854+
}
855+
return $this->httpClient->post($this->endpointBase . '/v2/bot/message/narrowcast', $params, $headers);
830856
}
831857

832858
/**

src/LINEBot/Constant/HTTPHeader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
class HTTPHeader
2222
{
2323
const LINE_SIGNATURE = 'X_LINE_SIGNATURE';
24+
const LINE_RETRY_KEY = 'X-Line-Retry-Key';
2425
}

tests/LINEBot/BroadcastTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2020 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\Tests\LINEBot;
20+
21+
use LINE\LINEBot;
22+
use LINE\LINEBot\Constant\MessageType;
23+
use LINE\LINEBot\MessageBuilder\TextMessageBuilder;
24+
use LINE\Tests\LINEBot\Util\DummyHttpClient;
25+
use PHPUnit\Framework\TestCase;
26+
27+
class BroadcastTest extends TestCase
28+
{
29+
public function testBroadcast()
30+
{
31+
$mock = function ($testRunner, $httpMethod, $url, $data, $headers) {
32+
/** @var \PHPUnit\Framework\TestCase $testRunner */
33+
$testRunner->assertEquals('POST', $httpMethod);
34+
$testRunner->assertEquals('https://api.line.me/v2/bot/message/broadcast', $url);
35+
36+
$testRunner->assertEquals(1, count($data['messages']));
37+
$testRunner->assertEquals(MessageType::TEXT, $data['messages'][0]['type']);
38+
$testRunner->assertEquals('test text', $data['messages'][0]['text']);
39+
$testRunner->assertTrue(\in_array('X-Line-Retry-Key: 123e4567-e89b-12d3-a456-426614174000', $headers));
40+
41+
return ['status' => 200];
42+
};
43+
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
44+
$res = $bot->broadcast(
45+
new TextMessageBuilder("test text"),
46+
false,
47+
'123e4567-e89b-12d3-a456-426614174000'
48+
);
49+
50+
$this->assertEquals(200, $res->getHTTPStatus());
51+
$this->assertTrue($res->isSucceeded());
52+
$this->assertEquals(200, $res->getJSONDecodedBody()['status']);
53+
}
54+
}

tests/LINEBot/MulticastTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MulticastTest extends TestCase
2828
{
2929
public function testMulticast()
3030
{
31-
$mock = function ($testRunner, $httpMethod, $url, $data) {
31+
$mock = function ($testRunner, $httpMethod, $url, $data, $headers) {
3232
/** @var \PHPUnit\Framework\TestCase $testRunner */
3333
$testRunner->assertEquals('POST', $httpMethod);
3434
$testRunner->assertEquals('https://api.line.me/v2/bot/message/multicast', $url);
@@ -37,13 +37,16 @@ public function testMulticast()
3737
$testRunner->assertEquals(1, count($data['messages']));
3838
$testRunner->assertEquals(MessageType::TEXT, $data['messages'][0]['type']);
3939
$testRunner->assertEquals('test text', $data['messages'][0]['text']);
40+
$testRunner->assertTrue(\in_array('X-Line-Retry-Key: 123e4567-e89b-12d3-a456-426614174000', $headers));
4041

4142
return ['status' => 200];
4243
};
4344
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
4445
$res = $bot->multicast(
4546
['DESTINATION1', 'DESTINATION2'],
46-
new TextMessageBuilder("test text")
47+
new TextMessageBuilder("test text"),
48+
false,
49+
'123e4567-e89b-12d3-a456-426614174000'
4750
);
4851

4952
$this->assertEquals(200, $res->getHTTPStatus());

tests/LINEBot/NarrowCastTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class NarrowCastTest extends TestCase
3333
{
3434
public function testSendNarrowcast()
3535
{
36-
$mock = function ($testRunner, $httpMethod, $url, $data) {
36+
$mock = function ($testRunner, $httpMethod, $url, $data, $headers) {
3737
/** @var \PHPUnit\Framework\TestCase $testRunner */
3838
$testRunner->assertEquals('POST', $httpMethod);
3939
$testRunner->assertEquals('https://api.line.me/v2/bot/message/narrowcast', $url);
@@ -70,6 +70,7 @@ public function testSendNarrowcast()
7070
]
7171
], $data['filter']['demographic']['and'][2]);
7272
$testRunner->assertEquals(100, $data['limit']['max']);
73+
$testRunner->assertTrue(\in_array('X-Line-Retry-Key: 123e4567-e89b-12d3-a456-426614174000', $headers));
7374

7475
return ['status' => 200];
7576
};
@@ -99,7 +100,8 @@ public function testSendNarrowcast()
99100
->setOneOf(['ios', 'android'])
100101
)
101102
]),
102-
100
103+
100,
104+
'123e4567-e89b-12d3-a456-426614174000'
103105
);
104106

105107
$this->assertEquals(200, $res->getHTTPStatus());

tests/LINEBot/SendTextTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function testReplyMessageWithMultiTextsContainsEmoji()
184184

185185
public function testPushTextMessage()
186186
{
187-
$mock = function ($testRunner, $httpMethod, $url, $data) {
187+
$mock = function ($testRunner, $httpMethod, $url, $data, $headers) {
188188
/** @var \PHPUnit\Framework\TestCase $testRunner */
189189
$testRunner->assertEquals('POST', $httpMethod);
190190
$testRunner->assertEquals('https://api.line.me/v2/bot/message/push', $url);
@@ -197,12 +197,18 @@ public function testPushTextMessage()
197197
$testRunner->assertEquals('test text2', $data['messages'][1]['text']);
198198
$testRunner->assertEquals(MessageType::TEXT, $data['messages'][2]['type']);
199199
$testRunner->assertEquals('test text3', $data['messages'][2]['text']);
200+
$testRunner->assertTrue(\in_array('X-Line-Retry-Key: 123e4567-e89b-12d3-a456-426614174000', $headers));
200201

201202
return ['status' => 200];
202203
};
203204

204205
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
205-
$res = $bot->pushMessage('DESTINATION', new TextMessageBuilder('test text1', 'test text2', 'test text3'));
206+
$res = $bot->pushMessage(
207+
'DESTINATION',
208+
new TextMessageBuilder('test text1', 'test text2', 'test text3'),
209+
false,
210+
'123e4567-e89b-12d3-a456-426614174000'
211+
);
206212

207213
$this->assertEquals(200, $res->getHTTPStatus());
208214
$this->assertTrue($res->isSucceeded());

0 commit comments

Comments
 (0)