Skip to content

Commit

Permalink
Merge pull request #216 from moririnson/add-new-prop-for-webhook
Browse files Browse the repository at this point in the history
Add support for new property of webhook
  • Loading branch information
moririnson authored Apr 23, 2019
2 parents f773f2c + 671783f commit b873035
Show file tree
Hide file tree
Showing 16 changed files with 299 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ public function __construct($bot, $logger, \Slim\Http\Request $req, AudioMessage

public function handle()
{
$replyToken = $this->audioMessage->getReplyToken();

$contentProvider = $this->audioMessage->getContentProvider();
if ($contentProvider->isExternal()) {
$this->bot->replyMessage(
$replyToken,
new AudioMessageBuilder(
$contentProvider->getOriginalContentUrl(),
$this->audioMessage->getDuration()
)
);
return;
}

$contentId = $this->audioMessage->getMessageId();
$audio = $this->bot->getMessageContent($contentId)->getRawBody();

Expand All @@ -64,14 +78,11 @@ public function handle()
fwrite($fh, $audio);
fclose($fh);

$replyToken = $this->audioMessage->getReplyToken();

$url = UrlBuilder::buildUrl($this->req, ['static', 'tmpdir', $filename]);

$resp = $this->bot->replyMessage(
$this->bot->replyMessage(
$replyToken,
new AudioMessageBuilder($url, 100)
);
$this->logger->info($resp->getRawBody());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ public function __construct($bot, $logger, \Slim\Http\Request $req, ImageMessage

public function handle()
{
$replyToken = $this->imageMessage->getReplyToken();

$contentProvider = $this->imageMessage->getContentProvider();
if ($contentProvider->isExternal()) {
$this->bot->replyMessage(
$replyToken,
new ImageMessageBuilder(
$contentProvider->getOriginalContentUrl(),
$contentProvider->getPreviewImageUrl()
)
);
return;
}

$contentId = $this->imageMessage->getMessageId();
$image = $this->bot->getMessageContent($contentId)->getRawBody();

Expand All @@ -64,8 +78,6 @@ public function handle()
fwrite($fh, $image);
fclose($fh);

$replyToken = $this->imageMessage->getReplyToken();

$url = UrlBuilder::buildUrl($this->req, ['static', 'tmpdir', $filename]);

// NOTE: You should pass the url of small image to `previewImageUrl`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ public function __construct($bot, $logger, \Slim\Http\Request $req, VideoMessage

public function handle()
{
$replyToken = $this->videoMessage->getReplyToken();

$contentProvider = $this->videoMessage->getContentProvider();
if ($contentProvider->isExternal()) {
$this->bot->replyMessage(
$replyToken,
new VideoMessageBuilder(
$contentProvider->getOriginalContentUrl(),
$contentProvider->getPreviewImageUrl()
)
);
return;
}

$contentId = $this->videoMessage->getMessageId();
$video = $this->bot->getMessageContent($contentId)->getRawBody();

Expand All @@ -65,8 +79,6 @@ public function handle()
fwrite($fh, $video);
fclose($fh);

$replyToken = $this->videoMessage->getReplyToken();

$url = UrlBuilder::buildUrl($this->req, ['static', 'tmpdir', $filename]);

// NOTE: You should pass the url of thumbnail image to `previewImageUrl`.
Expand Down
9 changes: 5 additions & 4 deletions src/LINEBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(HTTPClient $httpClient, array $args)
$this->channelSecret = $args['channelSecret'];

$this->endpointBase = LINEBot::DEFAULT_ENDPOINT_BASE;
if (array_key_exists('endpointBase', $args) && !empty($args['endpointBase'])) {
if (!empty($args['endpointBase'])) {
$this->endpointBase = $args['endpointBase'];
}
}
Expand Down Expand Up @@ -191,13 +191,14 @@ public function leaveRoom($roomId)
*
* @param string $body Request body.
* @param string $signature Signature of request.
* @return LINEBot\Event\BaseEvent[]
* @param bool $eventOnly if this flag on, get events only.
* @return mixed
* @throws LINEBot\Exception\InvalidEventRequestException
* @throws LINEBot\Exception\InvalidSignatureException
*/
public function parseEventRequest($body, $signature)
public function parseEventRequest($body, $signature, $eventOnly = true)
{
return EventRequestParser::parseEventRequest($body, $this->channelSecret, $signature);
return EventRequestParser::parseEventRequest($body, $this->channelSecret, $signature, $eventOnly);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/LINEBot/Constant/MessageContentProviderType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Constant;

class MessageContentProviderType
{
const LINE = 'line';
const EXTERNAL = 'external';
}
8 changes: 4 additions & 4 deletions src/LINEBot/Event/BaseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getTimestamp()
*/
public function getReplyToken()
{
return array_key_exists('replyToken', $this->event) ? $this->event['replyToken'] : null;
return isset($this->event['replyToken']) ? $this->event['replyToken'] : null;
}

/**
Expand Down Expand Up @@ -120,7 +120,7 @@ public function isUnknownEvent()
*/
public function getUserId()
{
return array_key_exists('userId', $this->event['source'])
return isset($this->event['source']['userId'])
? $this->event['source']['userId']
: null;
}
Expand All @@ -136,7 +136,7 @@ public function getGroupId()
if (!$this->isGroupEvent()) {
throw new InvalidEventSourceException('This event source is not a group type');
}
return array_key_exists('groupId', $this->event['source'])
return isset($this->event['source']['groupId'])
? $this->event['source']['groupId']
: null;
}
Expand All @@ -152,7 +152,7 @@ public function getRoomId()
if (!$this->isRoomEvent()) {
throw new InvalidEventSourceException('This event source is not a room type');
}
return array_key_exists('roomId', $this->event['source'])
return isset($this->event['source']['roomId'])
? $this->event['source']['roomId']
: null;
}
Expand Down
24 changes: 24 additions & 0 deletions src/LINEBot/Event/MessageEvent/AudioMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
*/
class AudioMessage extends MessageEvent
{
/** @var ContentProvider */
private $contentProvider;

/**
* AudioMessage constructor.
*
Expand All @@ -35,5 +38,26 @@ class AudioMessage extends MessageEvent
public function __construct($event)
{
parent::__construct($event);
$this->contentProvider = new ContentProvider($this->message['contentProvider']);
}

/**
* Returns duration of the audio message.
*
* @return int
*/
public function getDuration()
{
return $this->message['duration'];
}

/**
* Returns contentProvider of the audio message.
*
* @return ContentProvider
*/
public function getContentProvider()
{
return $this->contentProvider;
}
}
81 changes: 81 additions & 0 deletions src/LINEBot/Event/MessageEvent/ContentProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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\Event\MessageEvent;

use LINE\LINEBot\Constant\MessageContentProviderType;

class ContentProvider
{
/** @var array */
private $contentProvider;

/**
* ContentProvider constructor.
*
* @param array $contentProvider
*/
public function __construct($contentProvider)
{
$this->contentProvider = $contentProvider;
}

/**
* Returns contentProvider type is 'line' or not.
*
* @return bool
*/
public function isLine()
{
return $this->contentProvider['type'] == MessageContentProviderType::LINE;
}

/**
* Returns contentProvider type is 'external' or not.
*
* @return bool
*/
public function isExternal()
{
return $this->contentProvider['type'] == MessageContentProviderType::EXTERNAL;
}

/**
* Returns contentProvider getOriginalContentUrl.
*
* @return string|null
*/
public function getOriginalContentUrl()
{
return isset($this->contentProvider['originalContentUrl'])
? $this->contentProvider['originalContentUrl']
: null;
}

/**
* Returns contentProvider getPreviewImageUrl.
*
* @return string|null
*/
public function getPreviewImageUrl()
{
return isset($this->contentProvider['previewImageUrl'])
? $this->contentProvider['previewImageUrl']
: null;
}
}
14 changes: 14 additions & 0 deletions src/LINEBot/Event/MessageEvent/ImageMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
*/
class ImageMessage extends MessageEvent
{
/** @var ContentProvider */
private $contentProvider;

/**
* ImageMessage constructor.
*
Expand All @@ -35,5 +38,16 @@ class ImageMessage extends MessageEvent
public function __construct($event)
{
parent::__construct($event);
$this->contentProvider = new ContentProvider($this->message['contentProvider']);
}

/**
* Returns contentProvider of the image message.
*
* @return ContentProvider
*/
public function getContentProvider()
{
return $this->contentProvider;
}
}
4 changes: 2 additions & 2 deletions src/LINEBot/Event/MessageEvent/LocationMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct($event)
*/
public function getTitle()
{
return array_key_exists('title', $this->message) ? $this->message['title'] : null;
return isset($this->message['title']) ? $this->message['title'] : null;
}

/**
Expand All @@ -54,7 +54,7 @@ public function getTitle()
*/
public function getAddress()
{
return array_key_exists('address', $this->message) ? $this->message['address'] : null;
return isset($this->message['address']) ? $this->message['address'] : null;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/LINEBot/Event/MessageEvent/VideoMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
*/
class VideoMessage extends MessageEvent
{
/** @var ContentProvider */
private $contentProvider;

/**
* VideoMessage constructor.
*
Expand All @@ -35,5 +38,26 @@ class VideoMessage extends MessageEvent
public function __construct($event)
{
parent::__construct($event);
$this->contentProvider = new ContentProvider($this->message['contentProvider']);
}

/**
* Returns duration of the video message.
*
* @return int
*/
public function getDuration()
{
return $this->message['duration'];
}

/**
* Returns contentProvider of the video message.
*
* @return ContentProvider
*/
public function getContentProvider()
{
return $this->contentProvider;
}
}
Loading

0 comments on commit b873035

Please sign in to comment.