-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy path__init__.py
More file actions
408 lines (315 loc) · 9.06 KB
/
__init__.py
File metadata and controls
408 lines (315 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""
botogram.objects
Representation of the different upstream API objects
Copyright (c) 2015 Pietro Albini <pietro@pietroalbini.io>
Released under the MIT license
"""
from .base import BaseObject, multiple, _itself
from . import mixins
from .. import utils
class User(BaseObject, mixins.ChatMixin):
"""Telegram API representation of an user
https://core.telegram.org/bots/api#user
"""
required = {
"id": int,
"first_name": str,
}
optional = {
"last_name": str,
"username": str,
}
@property
def name(self):
"""Get the full name of the user"""
result = self.first_name
if self.last_name is not None:
result += " " + self.last_name
return result
@property
@mixins._require_api
def avatar(self):
"""Get the avatar of the user"""
# This is lazy loaded and cached, so it won't affect performances if
# you don't need avatars
if not hasattr(self, "_avatar"):
avatars = self._api.call("getUserProfilePhotos", {
"user_id": self.id,
"limit": 1,
}, expect=UserProfilePhotos)
# If the user has no avatars just use None
self._avatar = None
if len(avatars.photos):
self._avatar = avatars.photos[0] # Take the most recent one
return self._avatar
@mixins._require_api
def avatar_history(self):
"""Get all the avatars of the user"""
avatars = []
while True:
chunk = self._api.call("getUserProfilePhotos", {
"user_id": self.id,
"offset": len(avatars),
}, expect=UserProfilePhotos)
avatars += chunk.photos
if len(avatars) >= chunk.total_count:
break
return avatars
class Chat(BaseObject, mixins.ChatMixin):
"""Telegram API representation of a chat
https://core.telegram.org/bots/api#chat
"""
required = {
"id": int,
"type": str,
}
optional = {
"title": str,
"username": str,
"first_name": str,
"last_name": str,
}
@property
def name(self):
"""Get the full name of the chat"""
result = None
if self.title is not None:
result = self.title
elif self.first_name is not None:
result = self.first_name
if self.last_name is not None:
result += " " + self.last_name
return result
class PhotoSize(BaseObject, mixins.FileMixin):
"""Telegram API representation of a photo size
https://core.telegram.org/bots/api#photosize
"""
required = {
"file_id": str,
"width": int,
"height": int,
}
optional = {
"file_size": int,
}
class Photo(mixins.FileMixin):
"""Custom representation of a photo
The current API provided by Telegram for photos sucks, so this tries to
provide a better one.
"""
def __init__(self, data, api=None):
self._api = api
# Accept only lists of PhotoSize
if not isinstance(data, list):
raise ValueError("You must provide a list of PhotoSize")
# A photo without sizes is nothing
if len(data) == 0:
raise ValueError("No sizes passed...")
# Put all the sizes in an array
self.sizes = []
for size in data:
self.sizes.append(PhotoSize(size, api))
# Calculate the smaller and the biggest sizes
with_size = {}
for size in self.sizes:
with_size[size.height * size.width] = size
self.smallest = with_size[min(with_size.keys())]
self.biggest = with_size[max(with_size.keys())]
# Publish all the attributes of the biggest-size photo
attrs = list(PhotoSize.required.keys()) + list(PhotoSize.optional.keys())
for attr in attrs:
setattr(self, attr, getattr(self.biggest, attr))
def set_api(self, api):
"""Change the API instance"""
self._api = api
# Set the API on all the photo sizes
for size in self.sizes:
size.set_api(api)
def serialize(self):
"""Serialize this object"""
result = []
for size in self.sizes:
result.append(size.serialize())
return result
class Audio(BaseObject, mixins.FileMixin):
"""Telegram API representation of an audio track
https://core.telegram.org/bots/api#audio
"""
required = {
"file_id": str,
"duration": int,
}
optional = {
"performer": str,
"title": str,
"mime_type": str,
"file_size": int,
}
class Voice(BaseObject, mixins.FileMixin):
"""Telegram API representation of a voice message
https://core.telegram.org/bots/api#voice
"""
required = {
"file_id": str,
"duration": int,
}
optional = {
"mime_type": str,
"file_size": int,
}
class Document(BaseObject, mixins.FileMixin):
"""Telegram API representation of a document
https://core.telegram.org/bots/api#document
"""
required = {
"file_id": str,
}
optional = {
"thumb": PhotoSize,
"file_name": str,
"mime_type": str,
"file_size": int,
}
class Sticker(BaseObject, mixins.FileMixin):
"""Telegram API representation of a sticker
https://core.telegram.org/bots/api#sticker
"""
required = {
"file_id": str,
"width": int,
"height": int,
}
optional = {
"thumb": PhotoSize,
"file_size": int,
}
class Video(BaseObject, mixins.FileMixin):
"""Telegram API representation of a video
https://core.telegram.org/bots/api#video
"""
required = {
"file_id": str,
"width": int,
"height": int,
"duration": int,
}
optional = {
"thumb": PhotoSize,
"mime_type": str,
"file_size": int,
}
class Contact(BaseObject):
"""Telegram API representation of a contact
https://core.telegram.org/bots/api#contact
"""
required = {
"phone_number": str,
"first_name": str,
}
optional = {
"last_name": str,
"user_id": int,
}
class Location(BaseObject):
"""Telegram API representation of a location mark
https://core.telegram.org/bots/api#location
"""
required = {
"longitude": float,
"latitude": float,
}
class UserProfilePhotos(BaseObject):
"""Telegram API representation of an user's profile photos
https://core.telegram.org/bots/api#userprofilephotos
"""
required = {
"total_count": int,
"photos": multiple(Photo),
}
class Message(BaseObject, mixins.MessageMixin):
"""Telegram API representation of a message
https://core.telegram.org/bots/api#message
"""
@property
@utils.deprecated("Message.from_", "1.0",
"Rename property to Message.sender")
def from_(self):
return self.sender
required = {
"message_id": int,
"from": User,
"date": int,
"chat": Chat,
}
optional = {
"forward_from": User,
"forward_date": int,
"reply_to_message": _itself,
"text": str,
"audio": Audio,
"voice": Voice,
"document": Document,
"photo": Photo,
"sticker": Sticker,
"video": Video,
"caption": str,
"contact": Contact,
"location": Location,
"new_chat_participant": User,
"left_chat_participant": User,
"new_chat_title": str,
"new_chat_photo": Photo,
"delete_chat_photo": bool,
"group_chat_created": bool,
"supergroup_chat_created": bool,
"channel_chat_created": bool,
"migrate_to_chat_id": int,
"migrate_from_chat_id": int,
}
replace_keys = {
"from": "sender",
}
class ReplyKeyboardMarkup(BaseObject):
"""Telegram API representation of a custom keyboard
https://core.telegram.org/bots/api#replykeyboardmarkup
"""
required = {
"keyboard": multiple(multiple(str)),
}
optional = {
"resize_keyboard": bool,
"one_time_keyboard": bool,
"selective": bool,
}
class ReplyKeyboardHide(BaseObject):
"""Telegram API special object which hides a custom keyboard
https://core.telegram.org/bots/api#replykeyboardhide
"""
required = {
"hide_keyboard": bool,
}
optional = {
"selective": bool,
}
class ForceReply(BaseObject):
"""Telegram API special object which forces the user to reply to a message
https://core.telegram.org/bots/api#forcereply
"""
required = {
"force_reply": bool,
}
optional = {
"selective": bool,
}
class Update(BaseObject):
"""Telegram API representation of an update
https://core.telegram.org/bots/api#update
"""
required = {
"update_id": int,
}
optional = {
"message": Message
}
# Shortcut for the Updates type
Updates = multiple(Update)