Skip to content

Commit

Permalink
Merge pull request #209 from moririnson/add-support-video-for-imagemap
Browse files Browse the repository at this point in the history
Support for play videos in imagemap messages
  • Loading branch information
moririnson authored Apr 10, 2019
2 parents 7320390 + 51d6dc8 commit 7da8e93
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 1 deletion.
Binary file added examples/KitchenSink/public/static/preview.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/KitchenSink/public/static/video.mp4
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
57 changes: 57 additions & 0 deletions src/LINEBot/MessageBuilder/Imagemap/ExternalLinkBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* Copyright 2019 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

namespace LINE\LINEBot\MessageBuilder\Imagemap;

/**
* A builder class for external link of imagemap video.
*
* @package LINE\LINEBot\MessageBuilder\Imagemap
*/
class ExternalLinkBuilder
{
/** @var string */
private $linkUri;
/** @var string */
private $label;

/**
* ExternalLinkBuilder constructor.
*
* @param string $linkUri
* @param string $label
*/
public function __construct($linkUri, $label)
{
$this->linkUri = $linkUri;
$this->label = $label;
}

/**
* Builds external link of imagemap video.
*
* @return array
*/
public function build()
{
return [
'linkUri' => $this->linkUri,
'label' => $this->label
];
}
}
76 changes: 76 additions & 0 deletions src/LINEBot/MessageBuilder/Imagemap/VideoBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* Copyright 2019 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

namespace LINE\LINEBot\MessageBuilder\Imagemap;

use LINE\LINEBot\ImagemapActionBuilder\AreaBuilder;

/**
* A builder class for video of imagemap.
*
* @package LINE\LINEBot\MessageBuilder\Imagemap
*/
class VideoBuilder
{
/** @var string */
private $originalContentUrl;
/** @var string */
private $previewImageUrl;
/** @var AreaBuilder */
private $areaBuilder;
/** @var ExternalLinkBuilder|null */
private $externalLinkBuilder;

/**
* VideoBuilder constructor.
*
* @param string $originalContentUrl
* @param string $previewImageUrl
* @param AreaBuilder $area
* @param ExternalLinkBuilder|null $externalLink
*/
public function __construct(
$originalContentUrl,
$previewImageUrl,
AreaBuilder $areaBuilder,
ExternalLinkBuilder $externalLinkBuilder = null
) {
$this->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;
}
}
13 changes: 12 additions & 1 deletion src/LINEBot/MessageBuilder/ImagemapMessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -40,6 +41,9 @@ class ImagemapMessageBuilder implements MessageBuilder
/** @var BaseSizeBuilder */
private $baseSizeBuilder;

/** @var VideoBuilder|null */
private $videoBuilder;

/** @var ImagemapActionBuilder[] */
private $imagemapActionBuilders;

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand Down
75 changes: 75 additions & 0 deletions tests/LINEBot/SendImagemapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7da8e93

Please sign in to comment.