Skip to content

Commit b873035

Browse files
authored
Merge pull request #216 from moririnson/add-new-prop-for-webhook
Add support for new property of webhook
2 parents f773f2c + 671783f commit b873035

File tree

16 files changed

+299
-33
lines changed

16 files changed

+299
-33
lines changed

examples/KitchenSink/src/LINEBot/KitchenSink/EventHandler/MessageHandler/AudioMessageHandler.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ public function __construct($bot, $logger, \Slim\Http\Request $req, AudioMessage
5252

5353
public function handle()
5454
{
55+
$replyToken = $this->audioMessage->getReplyToken();
56+
57+
$contentProvider = $this->audioMessage->getContentProvider();
58+
if ($contentProvider->isExternal()) {
59+
$this->bot->replyMessage(
60+
$replyToken,
61+
new AudioMessageBuilder(
62+
$contentProvider->getOriginalContentUrl(),
63+
$this->audioMessage->getDuration()
64+
)
65+
);
66+
return;
67+
}
68+
5569
$contentId = $this->audioMessage->getMessageId();
5670
$audio = $this->bot->getMessageContent($contentId)->getRawBody();
5771

@@ -64,14 +78,11 @@ public function handle()
6478
fwrite($fh, $audio);
6579
fclose($fh);
6680

67-
$replyToken = $this->audioMessage->getReplyToken();
68-
6981
$url = UrlBuilder::buildUrl($this->req, ['static', 'tmpdir', $filename]);
7082

71-
$resp = $this->bot->replyMessage(
83+
$this->bot->replyMessage(
7284
$replyToken,
7385
new AudioMessageBuilder($url, 100)
7486
);
75-
$this->logger->info($resp->getRawBody());
7687
}
7788
}

examples/KitchenSink/src/LINEBot/KitchenSink/EventHandler/MessageHandler/ImageMessageHandler.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ public function __construct($bot, $logger, \Slim\Http\Request $req, ImageMessage
5252

5353
public function handle()
5454
{
55+
$replyToken = $this->imageMessage->getReplyToken();
56+
57+
$contentProvider = $this->imageMessage->getContentProvider();
58+
if ($contentProvider->isExternal()) {
59+
$this->bot->replyMessage(
60+
$replyToken,
61+
new ImageMessageBuilder(
62+
$contentProvider->getOriginalContentUrl(),
63+
$contentProvider->getPreviewImageUrl()
64+
)
65+
);
66+
return;
67+
}
68+
5569
$contentId = $this->imageMessage->getMessageId();
5670
$image = $this->bot->getMessageContent($contentId)->getRawBody();
5771

@@ -64,8 +78,6 @@ public function handle()
6478
fwrite($fh, $image);
6579
fclose($fh);
6680

67-
$replyToken = $this->imageMessage->getReplyToken();
68-
6981
$url = UrlBuilder::buildUrl($this->req, ['static', 'tmpdir', $filename]);
7082

7183
// NOTE: You should pass the url of small image to `previewImageUrl`.

examples/KitchenSink/src/LINEBot/KitchenSink/EventHandler/MessageHandler/VideoMessageHandler.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ public function __construct($bot, $logger, \Slim\Http\Request $req, VideoMessage
5353

5454
public function handle()
5555
{
56+
$replyToken = $this->videoMessage->getReplyToken();
57+
58+
$contentProvider = $this->videoMessage->getContentProvider();
59+
if ($contentProvider->isExternal()) {
60+
$this->bot->replyMessage(
61+
$replyToken,
62+
new VideoMessageBuilder(
63+
$contentProvider->getOriginalContentUrl(),
64+
$contentProvider->getPreviewImageUrl()
65+
)
66+
);
67+
return;
68+
}
69+
5670
$contentId = $this->videoMessage->getMessageId();
5771
$video = $this->bot->getMessageContent($contentId)->getRawBody();
5872

@@ -65,8 +79,6 @@ public function handle()
6579
fwrite($fh, $video);
6680
fclose($fh);
6781

68-
$replyToken = $this->videoMessage->getReplyToken();
69-
7082
$url = UrlBuilder::buildUrl($this->req, ['static', 'tmpdir', $filename]);
7183

7284
// NOTE: You should pass the url of thumbnail image to `previewImageUrl`.

src/LINEBot.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct(HTTPClient $httpClient, array $args)
5858
$this->channelSecret = $args['channelSecret'];
5959

6060
$this->endpointBase = LINEBot::DEFAULT_ENDPOINT_BASE;
61-
if (array_key_exists('endpointBase', $args) && !empty($args['endpointBase'])) {
61+
if (!empty($args['endpointBase'])) {
6262
$this->endpointBase = $args['endpointBase'];
6363
}
6464
}
@@ -191,13 +191,14 @@ public function leaveRoom($roomId)
191191
*
192192
* @param string $body Request body.
193193
* @param string $signature Signature of request.
194-
* @return LINEBot\Event\BaseEvent[]
194+
* @param bool $eventOnly if this flag on, get events only.
195+
* @return mixed
195196
* @throws LINEBot\Exception\InvalidEventRequestException
196197
* @throws LINEBot\Exception\InvalidSignatureException
197198
*/
198-
public function parseEventRequest($body, $signature)
199+
public function parseEventRequest($body, $signature, $eventOnly = true)
199200
{
200-
return EventRequestParser::parseEventRequest($body, $this->channelSecret, $signature);
201+
return EventRequestParser::parseEventRequest($body, $this->channelSecret, $signature, $eventOnly);
201202
}
202203

203204
/**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2019 LINE Corporation
5+
*
6+
* LINE Corporation licenses this file to you under the Apache License,
7+
* version 2.0 (the "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at:
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
* License for the specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
namespace LINE\LINEBot\Constant;
20+
21+
class MessageContentProviderType
22+
{
23+
const LINE = 'line';
24+
const EXTERNAL = 'external';
25+
}

src/LINEBot/Event/BaseEvent.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getTimestamp()
7070
*/
7171
public function getReplyToken()
7272
{
73-
return array_key_exists('replyToken', $this->event) ? $this->event['replyToken'] : null;
73+
return isset($this->event['replyToken']) ? $this->event['replyToken'] : null;
7474
}
7575

7676
/**
@@ -120,7 +120,7 @@ public function isUnknownEvent()
120120
*/
121121
public function getUserId()
122122
{
123-
return array_key_exists('userId', $this->event['source'])
123+
return isset($this->event['source']['userId'])
124124
? $this->event['source']['userId']
125125
: null;
126126
}
@@ -136,7 +136,7 @@ public function getGroupId()
136136
if (!$this->isGroupEvent()) {
137137
throw new InvalidEventSourceException('This event source is not a group type');
138138
}
139-
return array_key_exists('groupId', $this->event['source'])
139+
return isset($this->event['source']['groupId'])
140140
? $this->event['source']['groupId']
141141
: null;
142142
}
@@ -152,7 +152,7 @@ public function getRoomId()
152152
if (!$this->isRoomEvent()) {
153153
throw new InvalidEventSourceException('This event source is not a room type');
154154
}
155-
return array_key_exists('roomId', $this->event['source'])
155+
return isset($this->event['source']['roomId'])
156156
? $this->event['source']['roomId']
157157
: null;
158158
}

src/LINEBot/Event/MessageEvent/AudioMessage.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
*/
2828
class AudioMessage extends MessageEvent
2929
{
30+
/** @var ContentProvider */
31+
private $contentProvider;
32+
3033
/**
3134
* AudioMessage constructor.
3235
*
@@ -35,5 +38,26 @@ class AudioMessage extends MessageEvent
3538
public function __construct($event)
3639
{
3740
parent::__construct($event);
41+
$this->contentProvider = new ContentProvider($this->message['contentProvider']);
42+
}
43+
44+
/**
45+
* Returns duration of the audio message.
46+
*
47+
* @return int
48+
*/
49+
public function getDuration()
50+
{
51+
return $this->message['duration'];
52+
}
53+
54+
/**
55+
* Returns contentProvider of the audio message.
56+
*
57+
* @return ContentProvider
58+
*/
59+
public function getContentProvider()
60+
{
61+
return $this->contentProvider;
3862
}
3963
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2019 LINE Corporation
5+
*
6+
* LINE Corporation licenses this file to you under the Apache License,
7+
* version 2.0 (the "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at:
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
* License for the specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
namespace LINE\LINEBot\Event\MessageEvent;
20+
21+
use LINE\LINEBot\Constant\MessageContentProviderType;
22+
23+
class ContentProvider
24+
{
25+
/** @var array */
26+
private $contentProvider;
27+
28+
/**
29+
* ContentProvider constructor.
30+
*
31+
* @param array $contentProvider
32+
*/
33+
public function __construct($contentProvider)
34+
{
35+
$this->contentProvider = $contentProvider;
36+
}
37+
38+
/**
39+
* Returns contentProvider type is 'line' or not.
40+
*
41+
* @return bool
42+
*/
43+
public function isLine()
44+
{
45+
return $this->contentProvider['type'] == MessageContentProviderType::LINE;
46+
}
47+
48+
/**
49+
* Returns contentProvider type is 'external' or not.
50+
*
51+
* @return bool
52+
*/
53+
public function isExternal()
54+
{
55+
return $this->contentProvider['type'] == MessageContentProviderType::EXTERNAL;
56+
}
57+
58+
/**
59+
* Returns contentProvider getOriginalContentUrl.
60+
*
61+
* @return string|null
62+
*/
63+
public function getOriginalContentUrl()
64+
{
65+
return isset($this->contentProvider['originalContentUrl'])
66+
? $this->contentProvider['originalContentUrl']
67+
: null;
68+
}
69+
70+
/**
71+
* Returns contentProvider getPreviewImageUrl.
72+
*
73+
* @return string|null
74+
*/
75+
public function getPreviewImageUrl()
76+
{
77+
return isset($this->contentProvider['previewImageUrl'])
78+
? $this->contentProvider['previewImageUrl']
79+
: null;
80+
}
81+
}

src/LINEBot/Event/MessageEvent/ImageMessage.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
*/
2828
class ImageMessage extends MessageEvent
2929
{
30+
/** @var ContentProvider */
31+
private $contentProvider;
32+
3033
/**
3134
* ImageMessage constructor.
3235
*
@@ -35,5 +38,16 @@ class ImageMessage extends MessageEvent
3538
public function __construct($event)
3639
{
3740
parent::__construct($event);
41+
$this->contentProvider = new ContentProvider($this->message['contentProvider']);
42+
}
43+
44+
/**
45+
* Returns contentProvider of the image message.
46+
*
47+
* @return ContentProvider
48+
*/
49+
public function getContentProvider()
50+
{
51+
return $this->contentProvider;
3852
}
3953
}

src/LINEBot/Event/MessageEvent/LocationMessage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct($event)
4444
*/
4545
public function getTitle()
4646
{
47-
return array_key_exists('title', $this->message) ? $this->message['title'] : null;
47+
return isset($this->message['title']) ? $this->message['title'] : null;
4848
}
4949

5050
/**
@@ -54,7 +54,7 @@ public function getTitle()
5454
*/
5555
public function getAddress()
5656
{
57-
return array_key_exists('address', $this->message) ? $this->message['address'] : null;
57+
return isset($this->message['address']) ? $this->message['address'] : null;
5858
}
5959

6060
/**

0 commit comments

Comments
 (0)