Skip to content

Commit

Permalink
Add Unknown event (#424)
Browse files Browse the repository at this point in the history
* Define UnkownEvent

* Add example

* Format
  • Loading branch information
Yang-33 committed Mar 13, 2023
1 parent 524fd94 commit 3efd85d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
7 changes: 6 additions & 1 deletion examples/flask-kitchensink/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
StickerMessage, StickerSendMessage, LocationMessage, LocationSendMessage,
ImageMessage, VideoMessage, AudioMessage, FileMessage,
UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent,
MemberJoinedEvent, MemberLeftEvent,
MemberJoinedEvent, MemberLeftEvent, UnknownEvent,
FlexSendMessage, BubbleContainer, ImageComponent, BoxComponent,
TextComponent, IconComponent, ButtonComponent,
SeparatorComponent, QuickReply, QuickReplyButton,
Expand Down Expand Up @@ -680,6 +680,11 @@ def handle_member_left(event):
app.logger.info("Got memberLeft event")


@handler.add(UnknownEvent)
def handle_unknown_left(event):
app.logger.info(f"unknown event {event}")


@app.route('/static/<path:path>')
def send_static_content(path):
return send_from_directory('static', path)
Expand Down
1 change: 1 addition & 0 deletions linebot/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
MemberLeftEvent,
BeaconEvent,
ThingsEvent,
UnknownEvent,
Postback,
Beacon,
Link,
Expand Down
16 changes: 16 additions & 0 deletions linebot/models/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,22 @@ def __init__(self, mode=None, timestamp=None, source=None, reply_token=None,
)


class UnknownEvent(Event):
"""Unknown event.
We welcome your contribution to line-bot-sdk-python!
"""

def __init__(self, **kwargs):
"""__init__ method.
:param kwargs:
"""
super(UnknownEvent, self).__init__(**kwargs)

self.type = 'unknown'


class Postback(Base):
"""Postback.
Expand Down
8 changes: 6 additions & 2 deletions linebot/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
AccountLinkEvent,
MemberJoinedEvent,
MemberLeftEvent,
ThingsEvent, UnsendEvent, VideoPlayCompleteEvent,
ThingsEvent,
UnsendEvent,
VideoPlayCompleteEvent,
UnknownEvent,
)
from .utils import LOGGER, PY3, safe_compare_digest

Expand Down Expand Up @@ -172,7 +175,8 @@ def parse(self, body, signature, as_payload=False):
elif event_type == 'videoPlayComplete':
events.append(VideoPlayCompleteEvent.new_from_json_dict(event))
else:
LOGGER.warn('Unknown event type. type=' + event_type)
LOGGER.info('Unknown event type. type=' + event_type)
events.append(UnknownEvent.new_from_json_dict(event))

if as_payload:
return WebhookPayload(events=events, destination=body_json.get('destination'))
Expand Down
6 changes: 5 additions & 1 deletion tests/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
MessageEvent, FollowEvent, UnfollowEvent, JoinEvent,
LeaveEvent, PostbackEvent, BeaconEvent, AccountLinkEvent,
MemberJoinedEvent, MemberLeftEvent, ThingsEvent,
UnknownEvent,
TextMessage, ImageMessage, VideoMessage, AudioMessage,
LocationMessage, StickerMessage, FileMessage,
SourceUser, SourceRoom, SourceGroup,
Expand Down Expand Up @@ -69,7 +70,7 @@ def test_parse(self):
events = self.parser.parse(body, 'channel_secret')

# events count
self.assertEqual(len(events), 29)
self.assertEqual(len(events), 30)

# MessageEvent, SourceUser, TextMessage
self.assertIsInstance(events[0], MessageEvent)
Expand Down Expand Up @@ -567,6 +568,9 @@ def test_parse(self):
self.assertEqual(events[28].message.type, 'text')
self.assertEqual(events[28].message.text, 'Hello, world')

# UnknownEvent
self.assertIsInstance(events[29], UnknownEvent)

def test_parse_webhook_req_without_destination(self):
body = """
{
Expand Down
16 changes: 16 additions & 0 deletions tests/text/webhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,22 @@
"type": "text",
"text": "Hello, world"
}
},
{
"type": "undefined",
"mode": "active",
"timestamp": 1462629479859,
"source": {
"type": "user",
"userId": "U206d25c2ea6bd87c17655609a1c37cb8"
},
"webhookEventId": "testwebhookeventid",
"deliveryContext": {
"isRedelivery": true
},
"undefinedField": {
"1": 1
}
}
]
}

0 comments on commit 3efd85d

Please sign in to comment.