Adding decorators for send* functions. New decorator for authentication

This commit is contained in:
Leandro Toledo 2015-07-11 19:01:02 -03:00
parent 2fd3657ab2
commit 2d822b10fd

View file

@ -6,6 +6,7 @@
import json import json
import urllib import urllib
import urllib2 import urllib2
import functools
from telegram import (User, Message, Update, UserProfilePhotos, TelegramError, from telegram import (User, Message, Update, UserProfilePhotos, TelegramError,
ReplyMarkup, InputFile) ReplyMarkup, InputFile)
@ -71,6 +72,48 @@ class Bot(object):
return User.de_json(data) return User.de_json(data)
def message(func):
"""
Returns:
A telegram.Message instance representing the message posted.
"""
functools.wraps(func)
def wrap(self, *args, **kwargs):
url, data = func(self, *args, **kwargs)
if kwargs.get('reply_to_message_id'):
reply_to_message_id = kwargs.get('reply_to_message_id')
data['reply_to_message_id'] = reply_to_message_id
if kwargs.get('reply_markup'):
reply_markup = kwargs.get('reply_markup')
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data)
if data is True:
return data
return Message.de_json(data)
return wrap
def require_authentication(func):
functools.wraps(func)
def wrap(self, *args, **kwargs):
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
return func(self, *args, **kwargs)
return wrap
@message
@require_authentication
def sendMessage(self, def sendMessage(self,
chat_id, chat_id,
text, text,
@ -100,26 +143,16 @@ class Bot(object):
url = '%s/sendMessage' % (self.base_url) url = '%s/sendMessage' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {'chat_id': chat_id, data = {'chat_id': chat_id,
'text': text} 'text': text}
if disable_web_page_preview: if disable_web_page_preview:
data['disable_web_page_preview'] = disable_web_page_preview data['disable_web_page_preview'] = disable_web_page_preview
if reply_to_message_id:
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data) return (url, data)
data = self._parseAndCheckTelegram(json_data)
return Message.de_json(data)
@message
@require_authentication
def forwardMessage(self, def forwardMessage(self,
chat_id, chat_id,
from_chat_id, from_chat_id,
@ -141,9 +174,6 @@ class Bot(object):
url = '%s/forwardMessage' % (self.base_url) url = '%s/forwardMessage' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {} data = {}
if chat_id: if chat_id:
data['chat_id'] = chat_id data['chat_id'] = chat_id
@ -152,11 +182,10 @@ class Bot(object):
if message_id: if message_id:
data['message_id'] = message_id data['message_id'] = message_id
json_data = self._requestUrl(url, 'POST', data=data) return (url, data)
data = self._parseAndCheckTelegram(json_data)
return Message.de_json(data)
@message
@require_authentication
def sendPhoto(self, def sendPhoto(self,
chat_id, chat_id,
photo, photo,
@ -188,27 +217,16 @@ class Bot(object):
url = '%s/sendPhoto' % (self.base_url) url = '%s/sendPhoto' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {'chat_id': chat_id, data = {'chat_id': chat_id,
'photo': photo} 'photo': photo}
if caption: if caption:
data['caption'] = caption data['caption'] = caption
if reply_to_message_id:
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data) return (url, data)
data = self._parseAndCheckTelegram(json_data)
return Message.de_json(data)
@message
@require_authentication
def sendAudio(self, def sendAudio(self,
chat_id, chat_id,
audio, audio,
@ -239,25 +257,13 @@ class Bot(object):
url = '%s/sendAudio' % (self.base_url) url = '%s/sendAudio' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {'chat_id': chat_id, data = {'chat_id': chat_id,
'audio': audio} 'audio': audio}
if reply_to_message_id: return (url, data)
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data)
return Message.de_json(data)
@message
@require_authentication
def sendDocument(self, def sendDocument(self,
chat_id, chat_id,
document, document,
@ -285,25 +291,13 @@ class Bot(object):
url = '%s/sendDocument' % (self.base_url) url = '%s/sendDocument' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {'chat_id': chat_id, data = {'chat_id': chat_id,
'document': document} 'document': document}
if reply_to_message_id: return (url, data)
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data)
return Message.de_json(data)
@message
@require_authentication
def sendSticker(self, def sendSticker(self,
chat_id, chat_id,
sticker, sticker,
@ -331,25 +325,13 @@ class Bot(object):
url = '%s/sendSticker' % (self.base_url) url = '%s/sendSticker' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {'chat_id': chat_id, data = {'chat_id': chat_id,
'sticker': sticker} 'sticker': sticker}
if reply_to_message_id: return (url, data)
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data)
return Message.de_json(data)
@message
@require_authentication
def sendVideo(self, def sendVideo(self,
chat_id, chat_id,
video, video,
@ -378,25 +360,13 @@ class Bot(object):
url = '%s/sendVideo' % (self.base_url) url = '%s/sendVideo' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {'chat_id': chat_id, data = {'chat_id': chat_id,
'video': video} 'video': video}
if reply_to_message_id: return (url, data)
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data)
return Message.de_json(data)
@message
@require_authentication
def sendLocation(self, def sendLocation(self,
chat_id, chat_id,
latitude, latitude,
@ -425,26 +395,14 @@ class Bot(object):
url = '%s/sendLocation' % (self.base_url) url = '%s/sendLocation' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {'chat_id': chat_id, data = {'chat_id': chat_id,
'latitude': latitude, 'latitude': latitude,
'longitude': longitude} 'longitude': longitude}
if reply_to_message_id: return (url, data)
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data)
return Message.de_json(data)
@message
@require_authentication
def sendChatAction(self, def sendChatAction(self,
chat_id, chat_id,
action): action):
@ -469,14 +427,12 @@ class Bot(object):
url = '%s/sendChatAction' % (self.base_url) url = '%s/sendChatAction' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {'chat_id': chat_id, data = {'chat_id': chat_id,
'action': action} 'action': action}
self._requestUrl(url, 'POST', data=data) return (url, data)
@require_authentication
def getUserProfilePhotos(self, def getUserProfilePhotos(self,
user_id, user_id,
offset=None, offset=None,
@ -499,9 +455,6 @@ class Bot(object):
url = '%s/getUserProfilePhotos' % (self.base_url) url = '%s/getUserProfilePhotos' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {'user_id': user_id} data = {'user_id': user_id}
if offset: if offset:
@ -514,6 +467,7 @@ class Bot(object):
return UserProfilePhotos.de_json(data) return UserProfilePhotos.de_json(data)
@require_authentication
def getUpdates(self, def getUpdates(self,
offset=None, offset=None,
limit=100, limit=100,
@ -540,9 +494,6 @@ class Bot(object):
url = '%s/getUpdates' % (self.base_url) url = '%s/getUpdates' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {} data = {}
if offset: if offset:
data['offset'] = offset data['offset'] = offset
@ -556,7 +507,9 @@ class Bot(object):
return [Update.de_json(x) for x in data] return [Update.de_json(x) for x in data]
def setWebhook(self, webhook_url=""): @require_authentication
def setWebhook(self,
webhook_url):
"""Use this method to specify a url and receive incoming updates via an """Use this method to specify a url and receive incoming updates via an
outgoing webhook. Whenever there is an update for the bot, we will send outgoing webhook. Whenever there is an update for the bot, we will send
an HTTPS POST request to the specified url, containing a an HTTPS POST request to the specified url, containing a
@ -573,9 +526,6 @@ class Bot(object):
""" """
url = '%s/setWebhook' % (self.base_url) url = '%s/setWebhook' % (self.base_url)
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})
data = {'url': webhook_url} data = {'url': webhook_url}
json_data = self._requestUrl(url, 'POST', data=data) json_data = self._requestUrl(url, 'POST', data=data)
@ -648,7 +598,7 @@ class Bot(object):
""" """
try: try:
data = json.loads(json_data) data = json.loads(json_data.decode())
self._checkForTelegramError(data) self._checkForTelegramError(data)
except ValueError: except ValueError:
if '<title>403 Forbidden</title>' in json_data: if '<title>403 Forbidden</title>' in json_data: