diff --git a/examples/KitchenSink/public/static/preview.jpg b/examples/KitchenSink/public/static/preview.jpg new file mode 100644 index 00000000..9f70eb78 Binary files /dev/null and b/examples/KitchenSink/public/static/preview.jpg differ diff --git a/examples/KitchenSink/public/static/video.mp4 b/examples/KitchenSink/public/static/video.mp4 new file mode 100644 index 00000000..2cff6622 Binary files /dev/null and b/examples/KitchenSink/public/static/video.mp4 differ diff --git a/examples/KitchenSink/src/LINEBot/KitchenSink/EventHandler/MessageHandler/TextMessageHandler.php b/examples/KitchenSink/src/LINEBot/KitchenSink/EventHandler/MessageHandler/TextMessageHandler.php index 75501f70..46e28eec 100644 --- a/examples/KitchenSink/src/LINEBot/KitchenSink/EventHandler/MessageHandler/TextMessageHandler.php +++ b/examples/KitchenSink/src/LINEBot/KitchenSink/EventHandler/MessageHandler/TextMessageHandler.php @@ -38,6 +38,8 @@ use LINE\LINEBot\KitchenSink\EventHandler\MessageHandler\Flex\FlexSampleShopping; use LINE\LINEBot\KitchenSink\EventHandler\MessageHandler\Util\UrlBuilder; use LINE\LINEBot\MessageBuilder\Imagemap\BaseSizeBuilder; +use LINE\LINEBot\MessageBuilder\Imagemap\VideoBuilder; +use LINE\LINEBot\MessageBuilder\Imagemap\ExternalLinkBuilder; use LINE\LINEBot\MessageBuilder\ImagemapMessageBuilder; use LINE\LINEBot\MessageBuilder\TemplateMessageBuilder; use LINE\LINEBot\MessageBuilder\TemplateBuilder\ButtonTemplateBuilder; @@ -174,6 +176,40 @@ public function handle() ); $this->bot->replyMessage($replyToken, $imagemapMessageBuilder); break; + case 'imagemapVideo': + $richMessageUrl = UrlBuilder::buildUrl($this->req, ['static', 'rich']); + $imagemapMessageBuilder = new ImagemapMessageBuilder( + $richMessageUrl, + 'This is alt text', + new BaseSizeBuilder(1040, 1040), + [ + new ImagemapUriActionBuilder( + 'https://store.line.me/family/manga/en', + new AreaBuilder(0, 0, 520, 520) + ), + new ImagemapUriActionBuilder( + 'https://store.line.me/family/music/en', + new AreaBuilder(520, 0, 520, 520) + ), + new ImagemapUriActionBuilder( + 'https://store.line.me/family/play/en', + new AreaBuilder(0, 520, 520, 520) + ), + new ImagemapMessageActionBuilder( + 'URANAI!', + new AreaBuilder(520, 520, 520, 520) + ) + ], + null, + new VideoBuilder( + UrlBuilder::buildUrl($this->req, ['static', 'video.mp4']), + UrlBuilder::buildUrl($this->req, ['static', 'preview.jpg']), + new AreaBuilder(260, 260, 520, 520), + new ExternalLinkBuilder('https://line.me', 'LINE') + ) + ); + $this->bot->replyMessage($replyToken, $imagemapMessageBuilder); + break; case 'restaurant': $flexMessageBuilder = FlexSampleRestaurant::get(); $this->bot->replyMessage($replyToken, $flexMessageBuilder); diff --git a/src/LINEBot/MessageBuilder/Imagemap/ExternalLinkBuilder.php b/src/LINEBot/MessageBuilder/Imagemap/ExternalLinkBuilder.php new file mode 100644 index 00000000..184ccd80 --- /dev/null +++ b/src/LINEBot/MessageBuilder/Imagemap/ExternalLinkBuilder.php @@ -0,0 +1,57 @@ +linkUri = $linkUri; + $this->label = $label; + } + + /** + * Builds external link of imagemap video. + * + * @return array + */ + public function build() + { + return [ + 'linkUri' => $this->linkUri, + 'label' => $this->label + ]; + } +} diff --git a/src/LINEBot/MessageBuilder/Imagemap/VideoBuilder.php b/src/LINEBot/MessageBuilder/Imagemap/VideoBuilder.php new file mode 100644 index 00000000..2eff8190 --- /dev/null +++ b/src/LINEBot/MessageBuilder/Imagemap/VideoBuilder.php @@ -0,0 +1,76 @@ +originalContentUrl = $originalContentUrl; + $this->previewImageUrl = $previewImageUrl; + $this->areaBuilder = $areaBuilder; + $this->externalLinkBuilder = $externalLinkBuilder; + } + + /** + * Builds video of imagemap. + * + * @return array + */ + public function build() + { + $video = [ + 'originalContentUrl' => $this->originalContentUrl, + 'previewImageUrl' => $this->previewImageUrl, + 'area' => $this->areaBuilder->build() + ]; + if ($this->externalLinkBuilder) { + $video['externalLink'] = $this->externalLinkBuilder->build(); + } + return $video; + } +} diff --git a/src/LINEBot/MessageBuilder/ImagemapMessageBuilder.php b/src/LINEBot/MessageBuilder/ImagemapMessageBuilder.php index 587f8d2f..1e78b6f3 100644 --- a/src/LINEBot/MessageBuilder/ImagemapMessageBuilder.php +++ b/src/LINEBot/MessageBuilder/ImagemapMessageBuilder.php @@ -22,6 +22,7 @@ use LINE\LINEBot\ImagemapActionBuilder; use LINE\LINEBot\MessageBuilder; use LINE\LINEBot\MessageBuilder\Imagemap\BaseSizeBuilder; +use LINE\LINEBot\MessageBuilder\Imagemap\VideoBuilder; use LINE\LINEBot\QuickReplyBuilder; /** @@ -40,6 +41,9 @@ class ImagemapMessageBuilder implements MessageBuilder /** @var BaseSizeBuilder */ private $baseSizeBuilder; + /** @var VideoBuilder|null */ + private $videoBuilder; + /** @var ImagemapActionBuilder[] */ private $imagemapActionBuilders; @@ -57,19 +61,22 @@ class ImagemapMessageBuilder implements MessageBuilder * @param BaseSizeBuilder $baseSizeBuilder * @param ImagemapActionBuilder[] $imagemapActionBuilders * @param QuickReplyBuilder|null $quickReply + * @param VideoBuilder|null $videoBuilder */ public function __construct( $baseUrl, $altText, $baseSizeBuilder, array $imagemapActionBuilders, - QuickReplyBuilder $quickReply = null + QuickReplyBuilder $quickReply = null, + VideoBuilder $videoBuilder = null ) { $this->baseUrl = $baseUrl; $this->altText = $altText; $this->baseSizeBuilder = $baseSizeBuilder; $this->imagemapActionBuilders = $imagemapActionBuilders; $this->quickReply = $quickReply; + $this->videoBuilder = $videoBuilder; } /** @@ -100,6 +107,10 @@ public function buildMessage() $imagemapMessage['quickReply'] = $this->quickReply->buildQuickReply(); } + if ($this->videoBuilder) { + $imagemapMessage['video'] = $this->videoBuilder->build(); + } + $this->message[] = $imagemapMessage; return $this->message; diff --git a/tests/LINEBot/SendImagemapTest.php b/tests/LINEBot/SendImagemapTest.php index 77f15dc5..4fa96e52 100644 --- a/tests/LINEBot/SendImagemapTest.php +++ b/tests/LINEBot/SendImagemapTest.php @@ -25,6 +25,8 @@ use LINE\LINEBot\ImagemapActionBuilder\ImagemapMessageActionBuilder; use LINE\LINEBot\ImagemapActionBuilder\ImagemapUriActionBuilder; use LINE\LINEBot\MessageBuilder\Imagemap\BaseSizeBuilder; +use LINE\LINEBot\MessageBuilder\Imagemap\VideoBuilder; +use LINE\LINEBot\MessageBuilder\Imagemap\ExternalLinkBuilder; use LINE\LINEBot\MessageBuilder\ImagemapMessageBuilder; use LINE\Tests\LINEBot\Util\DummyHttpClient; use PHPUnit\Framework\TestCase; @@ -88,6 +90,79 @@ public function testReplyImagemap() $this->assertEquals(200, $res->getJSONDecodedBody()['status']); } + public function testReplyImagemapVideo() + { + $mock = function ($testRunner, $httpMethod, $url, $data) { + /** @var \PHPUnit\Framework\TestCase $testRunner */ + $testRunner->assertEquals('POST', $httpMethod); + $testRunner->assertEquals('https://api.line.me/v2/bot/message/reply', $url); + + $testRunner->assertEquals('REPLY-TOKEN', $data['replyToken']); + $testRunner->assertEquals(1, count($data['messages'])); + + $message = $data['messages'][0]; + $testRunner->assertEquals(MessageType::IMAGEMAP, $message['type']); + $testRunner->assertEquals('https://example.com/imagemap_base', $message['baseUrl']); + $testRunner->assertEquals('alt test', $message['altText']); + $testRunner->assertEquals(1040, $message['baseSize']['width']); + $testRunner->assertEquals(1040, $message['baseSize']['height']); + + $testRunner->assertEquals(2, count($message['actions'])); + $testRunner->assertEquals(ActionType::URI, $message['actions'][0]['type']); + $testRunner->assertEquals(0, $message['actions'][0]['area']['x']); + $testRunner->assertEquals(0, $message['actions'][0]['area']['y']); + $testRunner->assertEquals(1040, $message['actions'][0]['area']['width']); + $testRunner->assertEquals(520, $message['actions'][0]['area']['height']); + + $testRunner->assertEquals(ActionType::MESSAGE, $message['actions'][1]['type']); + $testRunner->assertEquals(0, $message['actions'][1]['area']['x']); + $testRunner->assertEquals(520, $message['actions'][1]['area']['y']); + $testRunner->assertEquals(1040, $message['actions'][1]['area']['width']); + $testRunner->assertEquals(520, $message['actions'][1]['area']['height']); + + $testRunner->assertEquals('https://example.com/video.mp4', $message['video']['originalContentUrl']); + $testRunner->assertEquals('https://example.com/preview.jpg', $message['video']['previewImageUrl']); + $testRunner->assertEquals(0, $message['video']['area']['x']); + $testRunner->assertEquals(0, $message['video']['area']['y']); + $testRunner->assertEquals(520, $message['video']['area']['width']); + $testRunner->assertEquals(520, $message['video']['area']['height']); + $testRunner->assertEquals('https://example.com/foo/bar', $message['video']['externalLink']['linkUri']); + $testRunner->assertEquals('test', $message['video']['externalLink']['label']); + + return ['status' => 200]; + }; + $bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']); + $res = $bot->replyMessage( + 'REPLY-TOKEN', + new ImagemapMessageBuilder( + 'https://example.com/imagemap_base', + 'alt test', + new BaseSizeBuilder(1040, 1040), + [ + new ImagemapUriActionBuilder( + 'https://example.com/foo/bar', + new AreaBuilder(0, 0, 1040, 520) + ), + new ImagemapMessageActionBuilder( + 'Fortune', + new AreaBuilder(0, 520, 1040, 520) + ), + ], + null, + new VideoBuilder( + 'https://example.com/video.mp4', + 'https://example.com/preview.jpg', + new AreaBuilder(0, 0, 520, 520), + new ExternalLinkBuilder('https://example.com/foo/bar', 'test') + ) + ) + ); + + $this->assertEquals(200, $res->getHTTPStatus()); + $this->assertTrue($res->isSucceeded()); + $this->assertEquals(200, $res->getJSONDecodedBody()['status']); + } + public function testPushImagemap() { $mock = function ($testRunner, $httpMethod, $url, $data) {