Skip to content

Commit 2d822b1

Browse files
committed
Adding decorators for send* functions. New decorator for authentication
1 parent 2fd3657 commit 2d822b1

File tree

1 file changed

+75
-125
lines changed

1 file changed

+75
-125
lines changed

telegram/bot.py

Lines changed: 75 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import urllib
88
import urllib2
9+
import functools
910

1011
from telegram import (User, Message, Update, UserProfilePhotos, TelegramError,
1112
ReplyMarkup, InputFile)
@@ -71,6 +72,48 @@ def getMe(self):
7172

7273
return User.de_json(data)
7374

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
74117
def sendMessage(self,
75118
chat_id,
76119
text,
@@ -100,26 +143,16 @@ def sendMessage(self,
100143

101144
url = '%s/sendMessage' % (self.base_url)
102145

103-
if not self.__auth:
104-
raise TelegramError({'message': "API must be authenticated."})
105-
106146
data = {'chat_id': chat_id,
107147
'text': text}
148+
108149
if disable_web_page_preview:
109150
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)
120151

121-
return Message.de_json(data)
152+
return (url, data)
122153

154+
@message
155+
@require_authentication
123156
def forwardMessage(self,
124157
chat_id,
125158
from_chat_id,
@@ -141,9 +174,6 @@ def forwardMessage(self,
141174

142175
url = '%s/forwardMessage' % (self.base_url)
143176

144-
if not self.__auth:
145-
raise TelegramError({'message': "API must be authenticated."})
146-
147177
data = {}
148178
if chat_id:
149179
data['chat_id'] = chat_id
@@ -152,11 +182,10 @@ def forwardMessage(self,
152182
if message_id:
153183
data['message_id'] = message_id
154184

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)
159186

187+
@message
188+
@require_authentication
160189
def sendPhoto(self,
161190
chat_id,
162191
photo,
@@ -188,27 +217,16 @@ def sendPhoto(self,
188217

189218
url = '%s/sendPhoto' % (self.base_url)
190219

191-
if not self.__auth:
192-
raise TelegramError({'message': "API must be authenticated."})
193-
194220
data = {'chat_id': chat_id,
195221
'photo': photo}
196222

197223
if caption:
198224
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)
209225

210-
return Message.de_json(data)
226+
return (url, data)
211227

228+
@message
229+
@require_authentication
212230
def sendAudio(self,
213231
chat_id,
214232
audio,
@@ -239,25 +257,13 @@ def sendAudio(self,
239257

240258
url = '%s/sendAudio' % (self.base_url)
241259

242-
if not self.__auth:
243-
raise TelegramError({'message': "API must be authenticated."})
244-
245260
data = {'chat_id': chat_id,
246261
'audio': audio}
247262

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)
260264

265+
@message
266+
@require_authentication
261267
def sendDocument(self,
262268
chat_id,
263269
document,
@@ -285,25 +291,13 @@ def sendDocument(self,
285291

286292
url = '%s/sendDocument' % (self.base_url)
287293

288-
if not self.__auth:
289-
raise TelegramError({'message': "API must be authenticated."})
290-
291294
data = {'chat_id': chat_id,
292295
'document': document}
293296

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)
306298

299+
@message
300+
@require_authentication
307301
def sendSticker(self,
308302
chat_id,
309303
sticker,
@@ -331,25 +325,13 @@ def sendSticker(self,
331325

332326
url = '%s/sendSticker' % (self.base_url)
333327

334-
if not self.__auth:
335-
raise TelegramError({'message': "API must be authenticated."})
336-
337328
data = {'chat_id': chat_id,
338329
'sticker': sticker}
339330

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)
352332

333+
@message
334+
@require_authentication
353335
def sendVideo(self,
354336
chat_id,
355337
video,
@@ -378,25 +360,13 @@ def sendVideo(self,
378360

379361
url = '%s/sendVideo' % (self.base_url)
380362

381-
if not self.__auth:
382-
raise TelegramError({'message': "API must be authenticated."})
383-
384363
data = {'chat_id': chat_id,
385364
'video': video}
386365

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)
399367

368+
@message
369+
@require_authentication
400370
def sendLocation(self,
401371
chat_id,
402372
latitude,
@@ -425,26 +395,14 @@ def sendLocation(self,
425395

426396
url = '%s/sendLocation' % (self.base_url)
427397

428-
if not self.__auth:
429-
raise TelegramError({'message': "API must be authenticated."})
430-
431398
data = {'chat_id': chat_id,
432399
'latitude': latitude,
433400
'longitude': longitude}
434401

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)
447403

404+
@message
405+
@require_authentication
448406
def sendChatAction(self,
449407
chat_id,
450408
action):
@@ -469,14 +427,12 @@ def sendChatAction(self,
469427

470428
url = '%s/sendChatAction' % (self.base_url)
471429

472-
if not self.__auth:
473-
raise TelegramError({'message': "API must be authenticated."})
474-
475430
data = {'chat_id': chat_id,
476431
'action': action}
477432

478-
self._requestUrl(url, 'POST', data=data)
433+
return (url, data)
479434

435+
@require_authentication
480436
def getUserProfilePhotos(self,
481437
user_id,
482438
offset=None,
@@ -499,9 +455,6 @@ def getUserProfilePhotos(self,
499455

500456
url = '%s/getUserProfilePhotos' % (self.base_url)
501457

502-
if not self.__auth:
503-
raise TelegramError({'message': "API must be authenticated."})
504-
505458
data = {'user_id': user_id}
506459

507460
if offset:
@@ -514,6 +467,7 @@ def getUserProfilePhotos(self,
514467

515468
return UserProfilePhotos.de_json(data)
516469

470+
@require_authentication
517471
def getUpdates(self,
518472
offset=None,
519473
limit=100,
@@ -540,9 +494,6 @@ def getUpdates(self,
540494

541495
url = '%s/getUpdates' % (self.base_url)
542496

543-
if not self.__auth:
544-
raise TelegramError({'message': "API must be authenticated."})
545-
546497
data = {}
547498
if offset:
548499
data['offset'] = offset
@@ -556,7 +507,9 @@ def getUpdates(self,
556507

557508
return [Update.de_json(x) for x in data]
558509

559-
def setWebhook(self, webhook_url=""):
510+
@require_authentication
511+
def setWebhook(self,
512+
webhook_url):
560513
"""Use this method to specify a url and receive incoming updates via an
561514
outgoing webhook. Whenever there is an update for the bot, we will send
562515
an HTTPS POST request to the specified url, containing a
@@ -573,9 +526,6 @@ def setWebhook(self, webhook_url=""):
573526
"""
574527
url = '%s/setWebhook' % (self.base_url)
575528

576-
if not self.__auth:
577-
raise TelegramError({'message': "API must be authenticated."})
578-
579529
data = {'url': webhook_url}
580530

581531
json_data = self._requestUrl(url, 'POST', data=data)
@@ -648,7 +598,7 @@ def _parseAndCheckTelegram(self,
648598
"""
649599

650600
try:
651-
data = json.loads(json_data)
601+
data = json.loads(json_data.decode())
652602
self._checkForTelegramError(data)
653603
except ValueError:
654604
if '<title>403 Forbidden</title>' in json_data:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy