diff --git a/src/LINEBot.php b/src/LINEBot.php index baf467e2..617ac2d4 100644 --- a/src/LINEBot.php +++ b/src/LINEBot.php @@ -1159,4 +1159,40 @@ public function updateAuthorityLevel($authorityLevel) 'authorityLevel' => $authorityLevel, ]); } + + /** + * Get webhook endpoint information + * + * @return Response + */ + public function getWebhookEndpointInfo() + { + return $this->httpClient->get($this->endpointBase . '/v2/bot/channel/webhook/endpoint'); + } + + /** + * Set webhook endpoint URL + * + * @param string $endpoint + * @return Response + */ + public function setWebhookEndpoint($endpoint) + { + return $this->httpClient->put($this->endpointBase . '/v2/bot/channel/webhook/endpoint', [ + 'endpoint' => $endpoint, + ]); + } + + /** + * Checks if the configured webhook endpoint can receive a test webhook event + * + * @param string $endpoint + * @return Response + */ + public function testWebhookEndpoint($endpoint) + { + return $this->httpClient->post($this->endpointBase . '/v2/bot/channel/webhook/test', [ + 'endpoint' => $endpoint, + ]); + } } diff --git a/tests/LINEBot/WebhookEndpointTest.php b/tests/LINEBot/WebhookEndpointTest.php new file mode 100644 index 00000000..753c2cae --- /dev/null +++ b/tests/LINEBot/WebhookEndpointTest.php @@ -0,0 +1,101 @@ +assertEquals('GET', $httpMethod); + $testRunner->assertEquals('https://api.line.me/v2/bot/channel/webhook/endpoint', $url); + + return [ + 'endpoint' => 'https://example.herokuapp.com/test', + 'active' => 'true', + ]; + }; + $bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']); + + $res = $bot->getWebhookEndpointInfo(); + + $this->assertEquals(200, $res->getHTTPStatus()); + $this->assertTrue($res->isSucceeded()); + + $data = $res->getJSONDecodedBody(); + $this->assertEquals('https://example.herokuapp.com/test', $data['endpoint']); + $this->assertEquals('true', $data['active']); + } + + public function testSetWebhookEndpoint() + { + $mock = function ($testRunner, $httpMethod, $url, $data) { + /** @var \PHPUnit\Framework\TestCase $testRunner */ + $testRunner->assertEquals('PUT', $httpMethod); + $testRunner->assertEquals('https://api.line.me/v2/bot/channel/webhook/endpoint', $url); + $testRunner->assertEquals('https://example.herokuapp.com/test', $data['endpoint']); + + return []; + }; + $bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']); + + $res = $bot->setWebhookEndpoint('https://example.herokuapp.com/test'); + + $this->assertEquals(200, $res->getHTTPStatus()); + $this->assertTrue($res->isSucceeded()); + + $this->assertEmpty($res->getJSONDecodedBody()); + } + + public function testTestWebhookEndpoint() + { + $mock = function ($testRunner, $httpMethod, $url, $data) { + /** @var \PHPUnit\Framework\TestCase $testRunner */ + $testRunner->assertEquals('POST', $httpMethod); + $testRunner->assertEquals('https://api.line.me/v2/bot/channel/webhook/test', $url); + $testRunner->assertEquals('https://example.herokuapp.com/test', $data['endpoint']); + + return [ + 'success' => 'true', + 'timestamp' => '2020-09-30T05:38:20.031Z', + 'statusCode' => 200, + 'reason' => 'OK', + 'detail' => '200', + ]; + }; + $bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']); + + $res = $bot->testWebhookEndpoint('https://example.herokuapp.com/test'); + + $this->assertEquals(200, $res->getHTTPStatus()); + $this->assertTrue($res->isSucceeded()); + + $data = $res->getJSONDecodedBody(); + $this->assertEquals('true', $data['success']); + $this->assertEquals('2020-09-30T05:38:20.031Z', $data['timestamp']); + $this->assertEquals(200, $data['statusCode']); + $this->assertEquals('OK', $data['reason']); + $this->assertEquals('200', $data['detail']); + } +}