1 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # https://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
15 """linebot.models.events module."""
17 from __future__ import unicode_literals
19 from abc import ABCMeta
21 from future.utils import with_metaclass
23 from .base import Base
24 from .messages import (
33 from .sources import SourceUser, SourceGroup, SourceRoom
36 class Event(with_metaclass(ABCMeta, Base)):
37 """Abstract Base Class of Webhook Event.
39 https://devdocs.line.me/en/#webhook-event-object
42 def __init__(self, timestamp=None, source=None, **kwargs):
45 :param long timestamp: Time of the event in milliseconds
46 :param source: Source object
47 :type source: T <= :py:class:`linebot.models.sources.Source`
50 super(Event, self).__init__(**kwargs)
53 self.timestamp = timestamp
54 self.source = self.get_or_new_from_json_dict_with_types(
63 class MessageEvent(Event):
64 """Webhook MessageEvent.
66 https://devdocs.line.me/en/#message-event
68 Event object which contains the sent message.
69 The message field contains a message object which corresponds with the message type.
70 You can reply to message events.
73 def __init__(self, timestamp=None, source=None, reply_token=None, message=None, **kwargs):
76 :param long timestamp: Time of the event in milliseconds
77 :param source: Source object
78 :type source: T <= :py:class:`linebot.models.sources.Source`
79 :param str reply_token: Reply token
80 :param message: Message object
81 :type message: T <= :py:class:`linebot.models.messages.Message`
84 super(MessageEvent, self).__init__(
85 timestamp=timestamp, source=source, **kwargs
89 self.reply_token = reply_token
90 self.message = self.get_or_new_from_json_dict_with_types(
93 'image': ImageMessage,
94 'video': VideoMessage,
95 'audio': AudioMessage,
96 'location': LocationMessage,
97 'sticker': StickerMessage,
103 class FollowEvent(Event):
104 """Webhook FollowEvent.
106 https://devdocs.line.me/en/#follow-event
108 Event object for when your account is added as a friend (or unblocked).
109 You can reply to follow events.
112 def __init__(self, timestamp=None, source=None, reply_token=None, **kwargs):
115 :param long timestamp: Time of the event in milliseconds
116 :param source: Source object
117 :type source: T <= :py:class:`linebot.models.sources.Source`
118 :param str reply_token: Reply token
121 super(FollowEvent, self).__init__(
122 timestamp=timestamp, source=source, **kwargs
126 self.reply_token = reply_token
129 class UnfollowEvent(Event):
130 """Webhook UnfollowEvent.
132 https://devdocs.line.me/en/#unfollow-event
134 Event object for when your account is blocked.
137 def __init__(self, timestamp=None, source=None, **kwargs):
140 :param long timestamp: Time of the event in milliseconds
141 :param source: Source object
142 :type source: T <= :py:class:`linebot.models.sources.Source`
145 super(UnfollowEvent, self).__init__(
146 timestamp=timestamp, source=source, **kwargs
149 self.type = 'unfollow'
152 class JoinEvent(Event):
153 """Webhook JoinEvent.
155 https://devdocs.line.me/en/#join-event
157 Event object for when your account joins a group or talk room.
158 You can reply to join events.
161 def __init__(self, timestamp=None, source=None, reply_token=None, **kwargs):
164 :param long timestamp: Time of the event in milliseconds
165 :param source: Source object
166 :type source: T <= :py:class:`linebot.models.sources.Source`
167 :param str reply_token: Reply token
170 super(JoinEvent, self).__init__(
171 timestamp=timestamp, source=source, **kwargs
175 self.reply_token = reply_token
178 class LeaveEvent(Event):
179 """Webhook LeaveEvent.
181 https://devdocs.line.me/en/#leave-event
183 Event object for when your account leaves a group.
186 def __init__(self, timestamp=None, source=None, **kwargs):
189 :param long timestamp: Time of the event in milliseconds
190 :param source: Source object
191 :type source: T <= :py:class:`linebot.models.sources.Source`
194 super(LeaveEvent, self).__init__(
195 timestamp=timestamp, source=source, **kwargs
201 class PostbackEvent(Event):
202 """Webhook PostbackEvent.
204 https://devdocs.line.me/en/#postback-event
206 Event object for when a user performs an action on
207 a template message which initiates a postback.
208 You can reply to postback events.
211 def __init__(self, timestamp=None, source=None, reply_token=None, postback=None, **kwargs):
214 :param long timestamp: Time of the event in milliseconds
215 :param source: Source object
216 :type source: T <= :py:class:`linebot.models.sources.Source`
217 :param str reply_token: Reply token
218 :param postback: Postback object
219 :type postback: :py:class:`linebot.models.events.Postback`
222 super(PostbackEvent, self).__init__(
223 timestamp=timestamp, source=source, **kwargs
226 self.type = 'postback'
227 self.reply_token = reply_token
228 self.postback = self.get_or_new_from_json_dict(
233 class BeaconEvent(Event):
234 """Webhook BeaconEvent.
236 https://devdocs.line.me/en/#beacon-event
238 Event object for when a user detects a LINE Beacon. You can reply to beacon events.
241 def __init__(self, timestamp=None, source=None, reply_token=None,
242 beacon=None, **kwargs):
245 :param long timestamp: Time of the event in milliseconds
246 :param source: Source object
247 :type source: T <= :py:class:`linebot.models.sources.Source`
248 :param str reply_token: Reply token
249 :param beacon: Beacon object
250 :type beacon: :py:class:`linebot.models.events.Beacon`
253 super(BeaconEvent, self).__init__(
254 timestamp=timestamp, source=source, **kwargs
258 self.reply_token = reply_token
259 self.beacon = self.get_or_new_from_json_dict(
264 class AccountLinkEvent(Event):
265 """Webhook AccountLinkEvent.
267 https://developers.line.me/en/docs/messaging-api/reference/#account-link-event
269 Event object for when a user has linked his/her LINE account with a provider's service account.
270 You can reply to account link events.
271 If the link token has expired or has already been used,
272 no webhook event will be sent and the user will be shown an error.
275 def __init__(self, timestamp=None, source=None, reply_token=None, link=None, **kwargs):
278 :param long timestamp: Time of the event in milliseconds
279 :param source: Source object
280 :type source: T <= :py:class:`linebot.models.sources.Source`
281 :param str reply_token: Reply token
282 :param link: Link object
283 :type link: :py:class:`linebot.models.events.Link`
286 super(AccountLinkEvent, self).__init__(
287 timestamp=timestamp, source=source, **kwargs
290 self.type = 'accountLink'
291 self.reply_token = reply_token
292 self.link = self.get_or_new_from_json_dict(
297 class Postback(Base):
300 https://devdocs.line.me/en/#postback-event
303 def __init__(self, data=None, params=None, **kwargs):
306 :param str data: Postback data
307 :param dict params: JSON object with the date and time
308 selected by a user through a datetime picker action.
309 Only returned for postback actions via the datetime picker.
312 super(Postback, self).__init__(**kwargs)
321 https://devdocs.line.me/en/#beacon-event
324 def __init__(self, type=None, hwid=None, dm=None, **kwargs):
327 :param str type: Type of beacon event
328 :param str hwid: Hardware ID of the beacon that was detected
329 :param str dm: Optional. Device message of beacon which is hex string
332 super(Beacon, self).__init__(**kwargs)
339 def device_message(self):
340 """Get dm(device_message) as bytearray.
345 return bytearray.fromhex(self.dm) if self.dm is not None else None
351 https://developers.line.me/en/docs/messaging-api/reference/#link-object
354 def __init__(self, result=None, nonce=None, **kwargs):
357 :param str result: Indicate whether the link was successful or not.
358 :param str nonce: Specified nonce when verifying the user ID.
360 super(Link, self).__init__(**kwargs)