mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-28 09:17:29 +01:00
clearCredentians and require_authentication are now decapred, bot properties will be called when needed only #33
This commit is contained in:
parent
fda1843593
commit
59ff1b68b5
1 changed files with 30 additions and 39 deletions
|
@ -48,21 +48,39 @@ class Bot(TelegramObject):
|
||||||
else:
|
else:
|
||||||
self.base_url = base_url + self.token
|
self.base_url = base_url + self.token
|
||||||
|
|
||||||
|
self.bot = None
|
||||||
|
|
||||||
self.log = logging.getLogger(__name__)
|
self.log = logging.getLogger(__name__)
|
||||||
|
|
||||||
try:
|
def info(func):
|
||||||
bot = self.getMe()
|
@functools.wraps(func)
|
||||||
|
def decorator(self, *args, **kwargs):
|
||||||
|
if not self.bot:
|
||||||
|
self.getMe()
|
||||||
|
|
||||||
self.id = bot.id
|
result = func(self, *args, **kwargs)
|
||||||
self.first_name = bot.first_name
|
return result
|
||||||
self.last_name = bot.last_name
|
return decorator
|
||||||
self.username = bot.username
|
|
||||||
|
|
||||||
self.__auth = True
|
@property
|
||||||
|
@info
|
||||||
|
def id(self):
|
||||||
|
return self.bot.id
|
||||||
|
|
||||||
self.log.info('Starting bot %s' % self.name)
|
@property
|
||||||
except TelegramError:
|
@info
|
||||||
raise TelegramError({'message': 'Bad token'})
|
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
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -109,22 +127,6 @@ class Bot(TelegramObject):
|
||||||
return Message.de_json(data)
|
return Message.de_json(data)
|
||||||
return decorator
|
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
|
@log
|
||||||
def getMe(self):
|
def getMe(self):
|
||||||
"""A simple method for testing your bot's auth token.
|
"""A simple method for testing your bot's auth token.
|
||||||
|
@ -138,11 +140,11 @@ class Bot(TelegramObject):
|
||||||
json_data = self._requestUrl(url, 'GET')
|
json_data = self._requestUrl(url, 'GET')
|
||||||
data = self._parseAndCheckTelegram(json_data)
|
data = self._parseAndCheckTelegram(json_data)
|
||||||
|
|
||||||
return User.de_json(data)
|
self.bot = User.de_json(data)
|
||||||
|
return self.bot
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@message
|
@message
|
||||||
@require_authentication
|
|
||||||
def sendMessage(self,
|
def sendMessage(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
text,
|
text,
|
||||||
|
@ -182,7 +184,6 @@ class Bot(TelegramObject):
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@message
|
@message
|
||||||
@require_authentication
|
|
||||||
def forwardMessage(self,
|
def forwardMessage(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
from_chat_id,
|
from_chat_id,
|
||||||
|
@ -216,7 +217,6 @@ class Bot(TelegramObject):
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@message
|
@message
|
||||||
@require_authentication
|
|
||||||
def sendPhoto(self,
|
def sendPhoto(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
photo,
|
photo,
|
||||||
|
@ -258,7 +258,6 @@ class Bot(TelegramObject):
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@message
|
@message
|
||||||
@require_authentication
|
|
||||||
def sendAudio(self,
|
def sendAudio(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
audio,
|
audio,
|
||||||
|
@ -296,7 +295,6 @@ class Bot(TelegramObject):
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@message
|
@message
|
||||||
@require_authentication
|
|
||||||
def sendDocument(self,
|
def sendDocument(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
document,
|
document,
|
||||||
|
@ -331,7 +329,6 @@ class Bot(TelegramObject):
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@message
|
@message
|
||||||
@require_authentication
|
|
||||||
def sendSticker(self,
|
def sendSticker(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
sticker,
|
sticker,
|
||||||
|
@ -366,7 +363,6 @@ class Bot(TelegramObject):
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@message
|
@message
|
||||||
@require_authentication
|
|
||||||
def sendVideo(self,
|
def sendVideo(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
video,
|
video,
|
||||||
|
@ -414,7 +410,6 @@ class Bot(TelegramObject):
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@message
|
@message
|
||||||
@require_authentication
|
|
||||||
def sendLocation(self,
|
def sendLocation(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
latitude,
|
latitude,
|
||||||
|
@ -451,7 +446,6 @@ class Bot(TelegramObject):
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@message
|
@message
|
||||||
@require_authentication
|
|
||||||
def sendChatAction(self,
|
def sendChatAction(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
action):
|
action):
|
||||||
|
@ -482,7 +476,6 @@ class Bot(TelegramObject):
|
||||||
return url, data
|
return url, data
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@require_authentication
|
|
||||||
def getUserProfilePhotos(self,
|
def getUserProfilePhotos(self,
|
||||||
user_id,
|
user_id,
|
||||||
offset=None,
|
offset=None,
|
||||||
|
@ -518,7 +511,6 @@ class Bot(TelegramObject):
|
||||||
return UserProfilePhotos.de_json(data)
|
return UserProfilePhotos.de_json(data)
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@require_authentication
|
|
||||||
def getUpdates(self,
|
def getUpdates(self,
|
||||||
offset=None,
|
offset=None,
|
||||||
limit=100,
|
limit=100,
|
||||||
|
@ -565,7 +557,6 @@ class Bot(TelegramObject):
|
||||||
return [Update.de_json(x) for x in data]
|
return [Update.de_json(x) for x in data]
|
||||||
|
|
||||||
@log
|
@log
|
||||||
@require_authentication
|
|
||||||
def setWebhook(self,
|
def setWebhook(self,
|
||||||
webhook_url):
|
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
|
||||||
|
|
Loading…
Add table
Reference in a new issue