Skip to content

Commit

Permalink
Merge pull request #225 from moririnson/enhanced-messaging-api
Browse files Browse the repository at this point in the history
Enhanced Messaging API features after redesign(04/18)
  • Loading branch information
moririnson authored May 22, 2019
2 parents f97cf38 + be8b386 commit cd266bb
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/LINEBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ public function getMessageContent($messageId)
return $this->httpClient->get($this->endpointBase . '/v2/bot/message/' . urlencode($messageId) . '/content');
}

/**
* Gets the target limit for additional messages in the current month.
*
* @return Response
*/
public function getNumberOfLimitForAdditional()
{
return $this->httpClient->get($this->endpointBase . '/v2/bot/message/quota');
}

/**
* Gets the number of messages sent in the current month.
*
* @return Response
*/
public function getNumberOfSentThisMonth()
{
return $this->httpClient->get($this->endpointBase . '/v2/bot/message/quota/consumption');
}

/**
* Replies arbitrary message to destination which is associated with reply token.
*
Expand Down Expand Up @@ -139,13 +159,15 @@ public function replyText($replyToken, $text, $extraTexts = null)
*
* @param string $to Identifier of destination.
* @param MessageBuilder $messageBuilder Message builder to send.
* @param boolean $notificationDisabled Don't send push notifications(=true) or send(=false)
* @return Response
*/
public function pushMessage($to, MessageBuilder $messageBuilder)
public function pushMessage($to, MessageBuilder $messageBuilder, $notificationDisabled = false)
{
return $this->httpClient->post($this->endpointBase . '/v2/bot/message/push', [
'to' => $to,
'messages' => $messageBuilder->buildMessage(),
'notificationDisabled' => $notificationDisabled,
]);
}

Expand All @@ -154,13 +176,31 @@ public function pushMessage($to, MessageBuilder $messageBuilder)
*
* @param array $tos Identifiers of destination.
* @param MessageBuilder $messageBuilder Message builder to send.
* @param boolean $notificationDisabled Don't send push notifications(=true) or send(=false)
* @return Response
*/
public function multicast(array $tos, MessageBuilder $messageBuilder)
public function multicast(array $tos, MessageBuilder $messageBuilder, $notificationDisabled = false)
{
return $this->httpClient->post($this->endpointBase . '/v2/bot/message/multicast', [
'to' => $tos,
'messages' => $messageBuilder->buildMessage(),
'notificationDisabled' => $notificationDisabled,
]);
}

/**
* Sends push messages to multiple users at any time.
* LINE@ accounts cannot call this API endpoint. Please migrate it to a LINE official account.
*
* @param MessageBuilder $messageBuilder Message builder to send.
* @param boolean $notificationDisabled Don't send push notifications(=true) or send(=false)
* @return Response
*/
public function broadcast(MessageBuilder $messageBuilder, $notificationDisabled = false)
{
return $this->httpClient->post($this->endpointBase . '/v2/bot/message/broadcast', [
'messages' => $messageBuilder->buildMessage(),
'notificationDisabled' => $notificationDisabled,
]);
}

Expand Down Expand Up @@ -533,4 +573,17 @@ public function getNumberOfSentMulticastMessages(DateTime $datetime)
$datetime->setTimezone(new DateTimeZone('Asia/Tokyo'));
return $this->httpClient->get($url, ['date' => $datetime->format('Ymd')]);
}

/**
* Get number of sent broadcast messages
*
* @param DateTime $datetime Date the messages were sent.
* @return Response
*/
public function getNumberOfSentBroadcastMessages(DateTime $datetime)
{
$url = $this->endpointBase . '/v2/bot/message/delivery/broadcast';
$datetime->setTimezone(new DateTimeZone('Asia/Tokyo'));
return $this->httpClient->get($url, ['date' => $datetime->format('Ymd')]);
}
}
92 changes: 92 additions & 0 deletions tests/LINEBot/GetNumberOfSendMessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,96 @@ public function testGetNumberOfSentMulticastMessages()
$this->assertEquals('ready', $data['status']);
$this->assertEquals(10000, $data['success']);
}

public function testGetNumberOfLimitForAdditional()
{
// Test: type is 'limited'
$mock = function ($testRunner, $httpMethod, $url, $data) {
/** @var \PHPUnit\Framework\TestCase $testRunner */
$testRunner->assertEquals('GET', $httpMethod);
$testRunner->assertEquals('https://api.line.me/v2/bot/message/quota', $url);
$testRunner->assertEquals([], $data);
return [
'type' => 'limited',
'value' => 10000
];
};
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
$res = $bot->getNumberOfLimitForAdditional();

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

$data = $res->getJSONDecodedBody();
$this->assertEquals('limited', $data['type']);
$this->assertEquals(10000, $data['value']);

// Test: type is 'none'
$mock = function ($testRunner, $httpMethod, $url, $data) {
/** @var \PHPUnit\Framework\TestCase $testRunner */
$testRunner->assertEquals('GET', $httpMethod);
$testRunner->assertEquals('https://api.line.me/v2/bot/message/quota', $url);
$testRunner->assertEquals([], $data);
return [
'type' => 'none'
];
};
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
$res = $bot->getNumberOfLimitForAdditional();

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

$data = $res->getJSONDecodedBody();
$this->assertEquals('none', $data['type']);
$this->assertArrayNotHasKey('value', $data);
}

public function testGetNumberOfSentThisMonth()
{
$mock = function ($testRunner, $httpMethod, $url, $data) {
/** @var \PHPUnit\Framework\TestCase $testRunner */
$testRunner->assertEquals('GET', $httpMethod);
$testRunner->assertEquals('https://api.line.me/v2/bot/message/quota/consumption', $url);
$testRunner->assertEquals([], $data);
return [
'totalUsage' => 10000
];
};
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
$res = $bot->getNumberOfSentThisMonth();

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

$data = $res->getJSONDecodedBody();
$this->assertEquals(10000, $data['totalUsage']);
}

public function testGetNumberOfSentBroadcastMessages()
{
$date = new DateTime();
$mock = function ($testRunner, $httpMethod, $url, $data) use ($date) {
/** @var \PHPUnit\Framework\TestCase $testRunner */
$testRunner->assertEquals('GET', $httpMethod);
$testRunner->assertEquals('https://api.line.me/v2/bot/message/delivery/broadcast', $url);
$date->setTimezone(new DateTimeZone('Asia/Tokyo'));
$testRunner->assertEquals([
'date' => $date->format('Ymd')
], $data);
return [
'status' => 'ready',
'success' => 10000
];
};
$bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']);
$res = $bot->getNumberOfSentBroadcastMessages($date);

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

$data = $res->getJSONDecodedBody();
$this->assertEquals('ready', $data['status']);
$this->assertEquals(10000, $data['success']);
}
}

0 comments on commit cd266bb

Please sign in to comment.