OSDN Git Service

linebot フォルダー復活
[simple-tornado-bot/simple-tornado-bot.git] / linebot / models / events.py
1 # -*- coding: utf-8 -*-
2
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
6 #
7 #       https://www.apache.org/licenses/LICENSE-2.0
8 #
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
13 #  under the License.
14
15 """linebot.models.events module."""
16
17 from __future__ import unicode_literals
18
19 from abc import ABCMeta
20
21 from future.utils import with_metaclass
22
23 from .base import Base
24 from .messages import (
25     TextMessage,
26     ImageMessage,
27     VideoMessage,
28     AudioMessage,
29     LocationMessage,
30     StickerMessage,
31     FileMessage
32 )
33 from .sources import SourceUser, SourceGroup, SourceRoom
34
35
36 class Event(with_metaclass(ABCMeta, Base)):
37     """Abstract Base Class of Webhook Event.
38
39     https://devdocs.line.me/en/#webhook-event-object
40     """
41
42     def __init__(self, timestamp=None, source=None, **kwargs):
43         """__init__ method.
44
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`
48         :param kwargs:
49         """
50         super(Event, self).__init__(**kwargs)
51
52         self.type = None
53         self.timestamp = timestamp
54         self.source = self.get_or_new_from_json_dict_with_types(
55             source, {
56                 'user': SourceUser,
57                 'group': SourceGroup,
58                 'room': SourceRoom,
59             }
60         )
61
62
63 class MessageEvent(Event):
64     """Webhook MessageEvent.
65
66     https://devdocs.line.me/en/#message-event
67
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.
71     """
72
73     def __init__(self, timestamp=None, source=None, reply_token=None, message=None, **kwargs):
74         """__init__ method.
75
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`
82         :param kwargs:
83         """
84         super(MessageEvent, self).__init__(
85             timestamp=timestamp, source=source, **kwargs
86         )
87
88         self.type = 'message'
89         self.reply_token = reply_token
90         self.message = self.get_or_new_from_json_dict_with_types(
91             message, {
92                 'text': TextMessage,
93                 'image': ImageMessage,
94                 'video': VideoMessage,
95                 'audio': AudioMessage,
96                 'location': LocationMessage,
97                 'sticker': StickerMessage,
98                 'file': FileMessage
99             }
100         )
101
102
103 class FollowEvent(Event):
104     """Webhook FollowEvent.
105
106     https://devdocs.line.me/en/#follow-event
107
108     Event object for when your account is added as a friend (or unblocked).
109     You can reply to follow events.
110     """
111
112     def __init__(self, timestamp=None, source=None, reply_token=None, **kwargs):
113         """__init__ method.
114
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
119         :param kwargs:
120         """
121         super(FollowEvent, self).__init__(
122             timestamp=timestamp, source=source, **kwargs
123         )
124
125         self.type = 'follow'
126         self.reply_token = reply_token
127
128
129 class UnfollowEvent(Event):
130     """Webhook UnfollowEvent.
131
132     https://devdocs.line.me/en/#unfollow-event
133
134     Event object for when your account is blocked.
135     """
136
137     def __init__(self, timestamp=None, source=None, **kwargs):
138         """__init__ method.
139
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`
143         :param kwargs:
144         """
145         super(UnfollowEvent, self).__init__(
146             timestamp=timestamp, source=source, **kwargs
147         )
148
149         self.type = 'unfollow'
150
151
152 class JoinEvent(Event):
153     """Webhook JoinEvent.
154
155     https://devdocs.line.me/en/#join-event
156
157     Event object for when your account joins a group or talk room.
158     You can reply to join events.
159     """
160
161     def __init__(self, timestamp=None, source=None, reply_token=None, **kwargs):
162         """__init__ method.
163
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
168         :param kwargs:
169         """
170         super(JoinEvent, self).__init__(
171             timestamp=timestamp, source=source, **kwargs
172         )
173
174         self.type = 'join'
175         self.reply_token = reply_token
176
177
178 class LeaveEvent(Event):
179     """Webhook LeaveEvent.
180
181     https://devdocs.line.me/en/#leave-event
182
183     Event object for when your account leaves a group.
184     """
185
186     def __init__(self, timestamp=None, source=None, **kwargs):
187         """__init__ method.
188
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`
192         :param kwargs:
193         """
194         super(LeaveEvent, self).__init__(
195             timestamp=timestamp, source=source, **kwargs
196         )
197
198         self.type = 'leave'
199
200
201 class PostbackEvent(Event):
202     """Webhook PostbackEvent.
203
204     https://devdocs.line.me/en/#postback-event
205
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.
209     """
210
211     def __init__(self, timestamp=None, source=None, reply_token=None, postback=None, **kwargs):
212         """__init__ method.
213
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`
220         :param kwargs:
221         """
222         super(PostbackEvent, self).__init__(
223             timestamp=timestamp, source=source, **kwargs
224         )
225
226         self.type = 'postback'
227         self.reply_token = reply_token
228         self.postback = self.get_or_new_from_json_dict(
229             postback, Postback
230         )
231
232
233 class BeaconEvent(Event):
234     """Webhook BeaconEvent.
235
236     https://devdocs.line.me/en/#beacon-event
237
238     Event object for when a user detects a LINE Beacon. You can reply to beacon events.
239     """
240
241     def __init__(self, timestamp=None, source=None, reply_token=None,
242                  beacon=None, **kwargs):
243         """__init__ method.
244
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`
251         :param kwargs:
252         """
253         super(BeaconEvent, self).__init__(
254             timestamp=timestamp, source=source, **kwargs
255         )
256
257         self.type = 'beacon'
258         self.reply_token = reply_token
259         self.beacon = self.get_or_new_from_json_dict(
260             beacon, Beacon
261         )
262
263
264 class AccountLinkEvent(Event):
265     """Webhook AccountLinkEvent.
266
267     https://developers.line.me/en/docs/messaging-api/reference/#account-link-event
268
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.
273     """
274
275     def __init__(self, timestamp=None, source=None, reply_token=None, link=None, **kwargs):
276         """__init__ method.
277
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`
284         :param kwargs:
285         """
286         super(AccountLinkEvent, self).__init__(
287             timestamp=timestamp, source=source, **kwargs
288         )
289
290         self.type = 'accountLink'
291         self.reply_token = reply_token
292         self.link = self.get_or_new_from_json_dict(
293             link, Link
294         )
295
296
297 class Postback(Base):
298     """Postback.
299
300     https://devdocs.line.me/en/#postback-event
301     """
302
303     def __init__(self, data=None, params=None, **kwargs):
304         """__init__ method.
305
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.
310         :param kwargs:
311         """
312         super(Postback, self).__init__(**kwargs)
313
314         self.data = data
315         self.params = params
316
317
318 class Beacon(Base):
319     """Beacon.
320
321     https://devdocs.line.me/en/#beacon-event
322     """
323
324     def __init__(self, type=None, hwid=None, dm=None, **kwargs):
325         """__init__ method.
326
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
330         :param kwargs:
331         """
332         super(Beacon, self).__init__(**kwargs)
333
334         self.type = type
335         self.hwid = hwid
336         self.dm = dm
337
338     @property
339     def device_message(self):
340         """Get dm(device_message) as bytearray.
341
342         :rtype: bytearray
343         :return:
344         """
345         return bytearray.fromhex(self.dm) if self.dm is not None else None
346
347
348 class Link(Base):
349     """Link.
350
351     https://developers.line.me/en/docs/messaging-api/reference/#link-object
352     """
353
354     def __init__(self, result=None, nonce=None, **kwargs):
355         """__init__ method.
356
357         :param str result: Indicate whether the link was successful or not.
358         :param str nonce: Specified nonce when verifying the user ID.
359         """
360         super(Link, self).__init__(**kwargs)
361
362         self.result = result
363         self.nonce = nonce