Skip to content

Commit

Permalink
clearCredentians and require_authentication are now decapred, bot pro…
Browse files Browse the repository at this point in the history
…perties will be called when needed only #33
  • Loading branch information
leandrotoledo committed Aug 14, 2015
1 parent fda1843 commit 59ff1b6
Showing 1 changed file with 30 additions and 39 deletions.
69 changes: 30 additions & 39 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,39 @@ def __init__(self,
else:
self.base_url = base_url + self.token

self.bot = None

self.log = logging.getLogger(__name__)

try:
bot = self.getMe()
def info(func):
@functools.wraps(func)
def decorator(self, *args, **kwargs):
if not self.bot:
self.getMe()

self.id = bot.id
self.first_name = bot.first_name
self.last_name = bot.last_name
self.username = bot.username
result = func(self, *args, **kwargs)
return result
return decorator

self.__auth = True
@property
@info
def id(self):
return self.bot.id

self.log.info('Starting bot %s' % self.name)
except TelegramError:
raise TelegramError({'message': 'Bad token'})
@property
@info
def first_name(self):
return self.bot.first_name

@property
@info
def last_name(self):
return self.bot.last_name

@property
@info
def username(self):
return self.bot.username

@property
def name(self):
Expand Down Expand Up @@ -109,22 +127,6 @@ def decorator(self, *args, **kwargs):
return Message.de_json(data)
return decorator

def require_authentication(func):
@functools.wraps(func)
def decorator(self, *args, **kwargs):
if not self.__auth:
raise TelegramError({'message': "API must be authenticated."})

return func(self, *args, **kwargs)
return decorator

@log
@require_authentication
def clearCredentials(self):
"""Clear any credentials for this instance.
"""
self.__auth = False

@log
def getMe(self):
"""A simple method for testing your bot's auth token.
Expand All @@ -138,11 +140,11 @@ def getMe(self):
json_data = self._requestUrl(url, 'GET')
data = self._parseAndCheckTelegram(json_data)

return User.de_json(data)
self.bot = User.de_json(data)
return self.bot

@log
@message
@require_authentication
def sendMessage(self,
chat_id,
text,
Expand Down Expand Up @@ -182,7 +184,6 @@ def sendMessage(self,

@log
@message
@require_authentication
def forwardMessage(self,
chat_id,
from_chat_id,
Expand Down Expand Up @@ -216,7 +217,6 @@ def forwardMessage(self,

@log
@message
@require_authentication
def sendPhoto(self,
chat_id,
photo,
Expand Down Expand Up @@ -258,7 +258,6 @@ def sendPhoto(self,

@log
@message
@require_authentication
def sendAudio(self,
chat_id,
audio,
Expand Down Expand Up @@ -296,7 +295,6 @@ def sendAudio(self,

@log
@message
@require_authentication
def sendDocument(self,
chat_id,
document,
Expand Down Expand Up @@ -331,7 +329,6 @@ def sendDocument(self,

@log
@message
@require_authentication
def sendSticker(self,
chat_id,
sticker,
Expand Down Expand Up @@ -366,7 +363,6 @@ def sendSticker(self,

@log
@message
@require_authentication
def sendVideo(self,
chat_id,
video,
Expand Down Expand Up @@ -414,7 +410,6 @@ def sendVideo(self,

@log
@message
@require_authentication
def sendLocation(self,
chat_id,
latitude,
Expand Down Expand Up @@ -451,7 +446,6 @@ def sendLocation(self,

@log
@message
@require_authentication
def sendChatAction(self,
chat_id,
action):
Expand Down Expand Up @@ -482,7 +476,6 @@ def sendChatAction(self,
return url, data

@log
@require_authentication
def getUserProfilePhotos(self,
user_id,
offset=None,
Expand Down Expand Up @@ -518,7 +511,6 @@ def getUserProfilePhotos(self,
return UserProfilePhotos.de_json(data)

@log
@require_authentication
def getUpdates(self,
offset=None,
limit=100,
Expand Down Expand Up @@ -565,7 +557,6 @@ def getUpdates(self,
return [Update.de_json(x) for x in data]

@log
@require_authentication
def setWebhook(self,
webhook_url):
"""Use this method to specify a url and receive incoming updates via an
Expand Down

0 comments on commit 59ff1b6

Please sign in to comment.