Skip to content

Commit 99fce14

Browse files
authored
Merge pull request #81 from line/support_unknown_event
Support unknown event
2 parents d827436 + 6d04587 commit 99fce14

File tree

9 files changed

+208
-89
lines changed

9 files changed

+208
-89
lines changed

examples/EchoBot/src/LINEBot/EchoBot/Route.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@
1818

1919
namespace LINE\LINEBot\EchoBot;
2020

21-
use LINE\LINEBot;
2221
use LINE\LINEBot\Constant\HTTPHeader;
2322
use LINE\LINEBot\Event\MessageEvent;
2423
use LINE\LINEBot\Event\MessageEvent\TextMessage;
2524
use LINE\LINEBot\Exception\InvalidEventRequestException;
2625
use LINE\LINEBot\Exception\InvalidSignatureException;
27-
use LINE\LINEBot\Exception\UnknownEventTypeException;
28-
use LINE\LINEBot\Exception\UnknownMessageTypeException;
2926

3027
class Route
3128
{
@@ -47,10 +44,6 @@ public function register(\Slim\App $app)
4744
$events = $bot->parseEventRequest($req->getBody(), $signature[0]);
4845
} catch (InvalidSignatureException $e) {
4946
return $res->withStatus(400, 'Invalid signature');
50-
} catch (UnknownEventTypeException $e) {
51-
return $res->withStatus(400, 'Unknown event type has come');
52-
} catch (UnknownMessageTypeException $e) {
53-
return $res->withStatus(400, 'Unknown message type has come');
5447
} catch (InvalidEventRequestException $e) {
5548
return $res->withStatus(400, "Invalid event request");
5649
}

examples/KitchenSink/src/LINEBot/KitchenSink/Route.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
use LINE\LINEBot\Event\MessageEvent\LocationMessage;
3131
use LINE\LINEBot\Event\MessageEvent\StickerMessage;
3232
use LINE\LINEBot\Event\MessageEvent\TextMessage;
33+
use LINE\LINEBot\Event\MessageEvent\UnknownMessage;
3334
use LINE\LINEBot\Event\MessageEvent\VideoMessage;
3435
use LINE\LINEBot\Event\PostbackEvent;
3536
use LINE\LINEBot\Event\UnfollowEvent;
37+
use LINE\LINEBot\Event\UnknownEvent;
3638
use LINE\LINEBot\Exception\InvalidEventRequestException;
3739
use LINE\LINEBot\Exception\InvalidSignatureException;
38-
use LINE\LINEBot\Exception\UnknownEventTypeException;
39-
use LINE\LINEBot\Exception\UnknownMessageTypeException;
4040
use LINE\LINEBot\KitchenSink\EventHandler\BeaconEventHandler;
4141
use LINE\LINEBot\KitchenSink\EventHandler\FollowEventHandler;
4242
use LINE\LINEBot\KitchenSink\EventHandler\JoinEventHandler;
@@ -71,10 +71,6 @@ public function register(\Slim\App $app)
7171
} catch (InvalidSignatureException $e) {
7272
$logger->info('Invalid signature');
7373
return $res->withStatus(400, 'Invalid signature');
74-
} catch (UnknownEventTypeException $e) {
75-
return $res->withStatus(400, 'Unknown event type has come');
76-
} catch (UnknownMessageTypeException $e) {
77-
return $res->withStatus(400, 'Unknown message type has come');
7874
} catch (InvalidEventRequestException $e) {
7975
return $res->withStatus(400, "Invalid event request");
8076
}
@@ -96,9 +92,12 @@ public function register(\Slim\App $app)
9692
$handler = new AudioMessageHandler($bot, $logger, $req, $event);
9793
} elseif ($event instanceof VideoMessage) {
9894
$handler = new VideoMessageHandler($bot, $logger, $req, $event);
95+
} elseif ($event instanceof UnknownMessage) {
96+
$logger->info(sprintf('Unknown message type has come [message type: %s]', $event->getMessageType()));
9997
} else {
100-
// Just in case...
101-
$logger->info('Unknown message type has come');
98+
// Unexpected behavior (just in case)
99+
// something wrong if reach here
100+
$logger->info(sprintf('Unexpected message type has come, something wrong [class name: %s]', get_class($event)));
102101
continue;
103102
}
104103
} elseif ($event instanceof UnfollowEvent) {
@@ -113,9 +112,12 @@ public function register(\Slim\App $app)
113112
$handler = new PostbackEventHandler($bot, $logger, $event);
114113
} elseif ($event instanceof BeaconDetectionEvent) {
115114
$handler = new BeaconEventHandler($bot, $logger, $event);
115+
} elseif ($event instanceof UnknownEvent) {
116+
$logger->info(sprintf('Unknown message type has come [type: %s]', $event->getType()));
116117
} else {
117-
// Just in case...
118-
$logger->info('Unknown event type has come');
118+
// Unexpected behavior (just in case)
119+
// something wrong if reach here
120+
$logger->info(sprintf('Unexpected event type has come, something wrong [class name: %s]', get_class($event)));
119121
continue;
120122
}
121123

src/LINEBot/Event/BaseEvent.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ public function isRoomEvent()
103103
return $this->event['source']['type'] === EventSourceType::ROOM;
104104
}
105105

106+
/**
107+
* Returns the event is unknown or not.
108+
*
109+
* @return bool
110+
*/
111+
public function isUnknownEvent()
112+
{
113+
return !($this->isUserEvent() || $this->isGroupEvent() || $this->isRoomEvent());
114+
}
115+
106116
/**
107117
* Returns user ID of the event.
108118
*
@@ -156,8 +166,6 @@ public function getRoomId()
156166
* (i.e. userId, groupId or roomId).
157167
*
158168
* @return null|string
159-
*
160-
* @throws InvalidEventSourceException Raise when event source type is invalid
161169
*/
162170
public function getEventSourceId()
163171
{
@@ -173,6 +181,7 @@ public function getEventSourceId()
173181
return $this->getRoomId();
174182
}
175183

176-
throw new InvalidEventSourceException('Invalid event source type, neither `user`, `room` nor `group`');
184+
# Unknown event
185+
return null;
177186
}
178187
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2017 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\Event\MessageEvent;
22+
23+
/**
24+
* A class that represents the unknown message event.
25+
*
26+
* @package LINE\LINEBot\Event\MessageEvent
27+
*/
28+
class UnknownMessage extends MessageEvent
29+
{
30+
/**
31+
* UnknownMessage constructor.
32+
*
33+
* @param array $event
34+
*/
35+
public function __construct($event)
36+
{
37+
parent::__construct($event);
38+
}
39+
40+
/**
41+
* Returns unprocessed message body.
42+
*
43+
* You can handle the message with getting the message body through this even if the message type is unknown.
44+
*
45+
* @return array
46+
*/
47+
public function getMessageBody()
48+
{
49+
return $this->message;
50+
}
51+
}

src/LINEBot/Event/Parser/EventRequestParser.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
namespace LINE\LINEBot\Event\Parser;
2020

2121
use LINE\LINEBot\Event\MessageEvent;
22+
use LINE\LINEBot\Event\MessageEvent\UnknownMessage;
23+
use LINE\LINEBot\Event\UnknownEvent;
2224
use LINE\LINEBot\Exception\InvalidEventRequestException;
2325
use LINE\LINEBot\Exception\InvalidSignatureException;
24-
use LINE\LINEBot\Exception\UnknownEventTypeException;
25-
use LINE\LINEBot\Exception\UnknownMessageTypeException;
2626
use LINE\LINEBot\SignatureValidator;
2727

2828
class EventRequestParser
@@ -53,7 +53,6 @@ class EventRequestParser
5353
* @return \LINE\LINEBot\Event\BaseEvent[] array
5454
* @throws InvalidEventRequestException
5555
* @throws InvalidSignatureException
56-
* @throws UnknownEventTypeException
5756
*/
5857
public static function parseEventRequest($body, $channelSecret, $signature)
5958
{
@@ -74,11 +73,15 @@ public static function parseEventRequest($body, $channelSecret, $signature)
7473

7574
foreach ($parsedReq['events'] as $eventData) {
7675
$eventType = $eventData['type'];
77-
$eventClass = self::$eventType2class[$eventType];
78-
if (!isset($eventClass)) {
79-
throw new UnknownEventTypeException('Unknown event type has come: ' . $eventType);
76+
77+
if (!array_key_exists($eventType, self::$eventType2class)) {
78+
# Unknown event has come
79+
$events[] = new UnknownEvent($eventData);
80+
continue;
8081
}
8182

83+
$eventClass = self::$eventType2class[$eventType];
84+
8285
if ($eventType === 'message') {
8386
$events[] = self::parseMessageEvent($eventData);
8487
continue;
@@ -92,16 +95,16 @@ public static function parseEventRequest($body, $channelSecret, $signature)
9295

9396
/**
9497
* @param array $eventData
95-
* @return MessageEvent|object
96-
* @throws UnknownMessageTypeException
98+
* @return MessageEvent
9799
*/
98100
private static function parseMessageEvent($eventData)
99101
{
100102
$messageType = $eventData['message']['type'];
101-
$messageClass = self::$messageType2class[$messageType];
102-
if (!isset($messageClass)) {
103-
throw new UnknownMessageTypeException('Unknown message type has come: ' . $messageType);
103+
if (!array_key_exists($messageType, self::$messageType2class)) {
104+
return new UnknownMessage($eventData);
104105
}
106+
107+
$messageClass = self::$messageType2class[$messageType];
105108
return new $messageClass($eventData);
106109
}
107110
}

src/LINEBot/Event/UnknownEvent.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2017 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;
20+
21+
/**
22+
* A class that represents the unknown event.
23+
*
24+
* If the event is not supported by this SDK, the event will be instantiate to this.
25+
*
26+
* @package LINE\LINEBot\Event
27+
*/
28+
class UnknownEvent extends BaseEvent
29+
{
30+
/**
31+
* UnknownEvent constructor.
32+
*
33+
* @param array $event
34+
*/
35+
public function __construct($event)
36+
{
37+
parent::__construct($event);
38+
}
39+
40+
/**
41+
* Returns unprocessed event body.
42+
*
43+
* You can handle the event with getting the event body through this even if the event type is unknown.
44+
*
45+
* @return array
46+
*/
47+
public function getEventBody()
48+
{
49+
return $this->event;
50+
}
51+
}

src/LINEBot/Exception/UnknownEventTypeException.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/LINEBot/Exception/UnknownMessageTypeException.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)