6
6
import json
7
7
import urllib
8
8
import urllib2
9
+ import functools
9
10
10
11
from telegram import (User , Message , Update , UserProfilePhotos , TelegramError ,
11
12
ReplyMarkup , InputFile )
@@ -71,6 +72,48 @@ def getMe(self):
71
72
72
73
return User .de_json (data )
73
74
75
+ def message (func ):
76
+ """
77
+ Returns:
78
+ A telegram.Message instance representing the message posted.
79
+ """
80
+ functools .wraps (func )
81
+
82
+ def wrap (self , * args , ** kwargs ):
83
+ url , data = func (self , * args , ** kwargs )
84
+
85
+ if kwargs .get ('reply_to_message_id' ):
86
+ reply_to_message_id = kwargs .get ('reply_to_message_id' )
87
+ data ['reply_to_message_id' ] = reply_to_message_id
88
+
89
+ if kwargs .get ('reply_markup' ):
90
+ reply_markup = kwargs .get ('reply_markup' )
91
+ if isinstance (reply_markup , ReplyMarkup ):
92
+ data ['reply_markup' ] = reply_markup .to_json ()
93
+ else :
94
+ data ['reply_markup' ] = reply_markup
95
+
96
+ json_data = self ._requestUrl (url , 'POST' , data = data )
97
+ data = self ._parseAndCheckTelegram (json_data )
98
+
99
+ if data is True :
100
+ return data
101
+
102
+ return Message .de_json (data )
103
+ return wrap
104
+
105
+ def require_authentication (func ):
106
+ functools .wraps (func )
107
+
108
+ def wrap (self , * args , ** kwargs ):
109
+ if not self .__auth :
110
+ raise TelegramError ({'message' : "API must be authenticated." })
111
+
112
+ return func (self , * args , ** kwargs )
113
+ return wrap
114
+
115
+ @message
116
+ @require_authentication
74
117
def sendMessage (self ,
75
118
chat_id ,
76
119
text ,
@@ -100,26 +143,16 @@ def sendMessage(self,
100
143
101
144
url = '%s/sendMessage' % (self .base_url )
102
145
103
- if not self .__auth :
104
- raise TelegramError ({'message' : "API must be authenticated." })
105
-
106
146
data = {'chat_id' : chat_id ,
107
147
'text' : text }
148
+
108
149
if disable_web_page_preview :
109
150
data ['disable_web_page_preview' ] = disable_web_page_preview
110
- if reply_to_message_id :
111
- data ['reply_to_message_id' ] = reply_to_message_id
112
- if reply_markup :
113
- if isinstance (reply_markup , ReplyMarkup ):
114
- data ['reply_markup' ] = reply_markup .to_json ()
115
- else :
116
- data ['reply_markup' ] = reply_markup
117
-
118
- json_data = self ._requestUrl (url , 'POST' , data = data )
119
- data = self ._parseAndCheckTelegram (json_data )
120
151
121
- return Message . de_json ( data )
152
+ return ( url , data )
122
153
154
+ @message
155
+ @require_authentication
123
156
def forwardMessage (self ,
124
157
chat_id ,
125
158
from_chat_id ,
@@ -141,9 +174,6 @@ def forwardMessage(self,
141
174
142
175
url = '%s/forwardMessage' % (self .base_url )
143
176
144
- if not self .__auth :
145
- raise TelegramError ({'message' : "API must be authenticated." })
146
-
147
177
data = {}
148
178
if chat_id :
149
179
data ['chat_id' ] = chat_id
@@ -152,11 +182,10 @@ def forwardMessage(self,
152
182
if message_id :
153
183
data ['message_id' ] = message_id
154
184
155
- json_data = self ._requestUrl (url , 'POST' , data = data )
156
- data = self ._parseAndCheckTelegram (json_data )
157
-
158
- return Message .de_json (data )
185
+ return (url , data )
159
186
187
+ @message
188
+ @require_authentication
160
189
def sendPhoto (self ,
161
190
chat_id ,
162
191
photo ,
@@ -188,27 +217,16 @@ def sendPhoto(self,
188
217
189
218
url = '%s/sendPhoto' % (self .base_url )
190
219
191
- if not self .__auth :
192
- raise TelegramError ({'message' : "API must be authenticated." })
193
-
194
220
data = {'chat_id' : chat_id ,
195
221
'photo' : photo }
196
222
197
223
if caption :
198
224
data ['caption' ] = caption
199
- if reply_to_message_id :
200
- data ['reply_to_message_id' ] = reply_to_message_id
201
- if reply_markup :
202
- if isinstance (reply_markup , ReplyMarkup ):
203
- data ['reply_markup' ] = reply_markup .to_json ()
204
- else :
205
- data ['reply_markup' ] = reply_markup
206
-
207
- json_data = self ._requestUrl (url , 'POST' , data = data )
208
- data = self ._parseAndCheckTelegram (json_data )
209
225
210
- return Message . de_json ( data )
226
+ return ( url , data )
211
227
228
+ @message
229
+ @require_authentication
212
230
def sendAudio (self ,
213
231
chat_id ,
214
232
audio ,
@@ -239,25 +257,13 @@ def sendAudio(self,
239
257
240
258
url = '%s/sendAudio' % (self .base_url )
241
259
242
- if not self .__auth :
243
- raise TelegramError ({'message' : "API must be authenticated." })
244
-
245
260
data = {'chat_id' : chat_id ,
246
261
'audio' : audio }
247
262
248
- if reply_to_message_id :
249
- data ['reply_to_message_id' ] = reply_to_message_id
250
- if reply_markup :
251
- if isinstance (reply_markup , ReplyMarkup ):
252
- data ['reply_markup' ] = reply_markup .to_json ()
253
- else :
254
- data ['reply_markup' ] = reply_markup
255
-
256
- json_data = self ._requestUrl (url , 'POST' , data = data )
257
- data = self ._parseAndCheckTelegram (json_data )
258
-
259
- return Message .de_json (data )
263
+ return (url , data )
260
264
265
+ @message
266
+ @require_authentication
261
267
def sendDocument (self ,
262
268
chat_id ,
263
269
document ,
@@ -285,25 +291,13 @@ def sendDocument(self,
285
291
286
292
url = '%s/sendDocument' % (self .base_url )
287
293
288
- if not self .__auth :
289
- raise TelegramError ({'message' : "API must be authenticated." })
290
-
291
294
data = {'chat_id' : chat_id ,
292
295
'document' : document }
293
296
294
- if reply_to_message_id :
295
- data ['reply_to_message_id' ] = reply_to_message_id
296
- if reply_markup :
297
- if isinstance (reply_markup , ReplyMarkup ):
298
- data ['reply_markup' ] = reply_markup .to_json ()
299
- else :
300
- data ['reply_markup' ] = reply_markup
301
-
302
- json_data = self ._requestUrl (url , 'POST' , data = data )
303
- data = self ._parseAndCheckTelegram (json_data )
304
-
305
- return Message .de_json (data )
297
+ return (url , data )
306
298
299
+ @message
300
+ @require_authentication
307
301
def sendSticker (self ,
308
302
chat_id ,
309
303
sticker ,
@@ -331,25 +325,13 @@ def sendSticker(self,
331
325
332
326
url = '%s/sendSticker' % (self .base_url )
333
327
334
- if not self .__auth :
335
- raise TelegramError ({'message' : "API must be authenticated." })
336
-
337
328
data = {'chat_id' : chat_id ,
338
329
'sticker' : sticker }
339
330
340
- if reply_to_message_id :
341
- data ['reply_to_message_id' ] = reply_to_message_id
342
- if reply_markup :
343
- if isinstance (reply_markup , ReplyMarkup ):
344
- data ['reply_markup' ] = reply_markup .to_json ()
345
- else :
346
- data ['reply_markup' ] = reply_markup
347
-
348
- json_data = self ._requestUrl (url , 'POST' , data = data )
349
- data = self ._parseAndCheckTelegram (json_data )
350
-
351
- return Message .de_json (data )
331
+ return (url , data )
352
332
333
+ @message
334
+ @require_authentication
353
335
def sendVideo (self ,
354
336
chat_id ,
355
337
video ,
@@ -378,25 +360,13 @@ def sendVideo(self,
378
360
379
361
url = '%s/sendVideo' % (self .base_url )
380
362
381
- if not self .__auth :
382
- raise TelegramError ({'message' : "API must be authenticated." })
383
-
384
363
data = {'chat_id' : chat_id ,
385
364
'video' : video }
386
365
387
- if reply_to_message_id :
388
- data ['reply_to_message_id' ] = reply_to_message_id
389
- if reply_markup :
390
- if isinstance (reply_markup , ReplyMarkup ):
391
- data ['reply_markup' ] = reply_markup .to_json ()
392
- else :
393
- data ['reply_markup' ] = reply_markup
394
-
395
- json_data = self ._requestUrl (url , 'POST' , data = data )
396
- data = self ._parseAndCheckTelegram (json_data )
397
-
398
- return Message .de_json (data )
366
+ return (url , data )
399
367
368
+ @message
369
+ @require_authentication
400
370
def sendLocation (self ,
401
371
chat_id ,
402
372
latitude ,
@@ -425,26 +395,14 @@ def sendLocation(self,
425
395
426
396
url = '%s/sendLocation' % (self .base_url )
427
397
428
- if not self .__auth :
429
- raise TelegramError ({'message' : "API must be authenticated." })
430
-
431
398
data = {'chat_id' : chat_id ,
432
399
'latitude' : latitude ,
433
400
'longitude' : longitude }
434
401
435
- if reply_to_message_id :
436
- data ['reply_to_message_id' ] = reply_to_message_id
437
- if reply_markup :
438
- if isinstance (reply_markup , ReplyMarkup ):
439
- data ['reply_markup' ] = reply_markup .to_json ()
440
- else :
441
- data ['reply_markup' ] = reply_markup
442
-
443
- json_data = self ._requestUrl (url , 'POST' , data = data )
444
- data = self ._parseAndCheckTelegram (json_data )
445
-
446
- return Message .de_json (data )
402
+ return (url , data )
447
403
404
+ @message
405
+ @require_authentication
448
406
def sendChatAction (self ,
449
407
chat_id ,
450
408
action ):
@@ -469,14 +427,12 @@ def sendChatAction(self,
469
427
470
428
url = '%s/sendChatAction' % (self .base_url )
471
429
472
- if not self .__auth :
473
- raise TelegramError ({'message' : "API must be authenticated." })
474
-
475
430
data = {'chat_id' : chat_id ,
476
431
'action' : action }
477
432
478
- self . _requestUrl (url , 'POST' , data = data )
433
+ return (url , data )
479
434
435
+ @require_authentication
480
436
def getUserProfilePhotos (self ,
481
437
user_id ,
482
438
offset = None ,
@@ -499,9 +455,6 @@ def getUserProfilePhotos(self,
499
455
500
456
url = '%s/getUserProfilePhotos' % (self .base_url )
501
457
502
- if not self .__auth :
503
- raise TelegramError ({'message' : "API must be authenticated." })
504
-
505
458
data = {'user_id' : user_id }
506
459
507
460
if offset :
@@ -514,6 +467,7 @@ def getUserProfilePhotos(self,
514
467
515
468
return UserProfilePhotos .de_json (data )
516
469
470
+ @require_authentication
517
471
def getUpdates (self ,
518
472
offset = None ,
519
473
limit = 100 ,
@@ -540,9 +494,6 @@ def getUpdates(self,
540
494
541
495
url = '%s/getUpdates' % (self .base_url )
542
496
543
- if not self .__auth :
544
- raise TelegramError ({'message' : "API must be authenticated." })
545
-
546
497
data = {}
547
498
if offset :
548
499
data ['offset' ] = offset
@@ -556,7 +507,9 @@ def getUpdates(self,
556
507
557
508
return [Update .de_json (x ) for x in data ]
558
509
559
- def setWebhook (self , webhook_url = "" ):
510
+ @require_authentication
511
+ def setWebhook (self ,
512
+ webhook_url ):
560
513
"""Use this method to specify a url and receive incoming updates via an
561
514
outgoing webhook. Whenever there is an update for the bot, we will send
562
515
an HTTPS POST request to the specified url, containing a
@@ -573,9 +526,6 @@ def setWebhook(self, webhook_url=""):
573
526
"""
574
527
url = '%s/setWebhook' % (self .base_url )
575
528
576
- if not self .__auth :
577
- raise TelegramError ({'message' : "API must be authenticated." })
578
-
579
529
data = {'url' : webhook_url }
580
530
581
531
json_data = self ._requestUrl (url , 'POST' , data = data )
@@ -648,7 +598,7 @@ def _parseAndCheckTelegram(self,
648
598
"""
649
599
650
600
try :
651
- data = json .loads (json_data )
601
+ data = json .loads (json_data . decode () )
652
602
self ._checkForTelegramError (data )
653
603
except ValueError :
654
604
if '<title>403 Forbidden</title>' in json_data :
0 commit comments