mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 09:26:37 +03:00
Improve .is_user_authorized() and add a .get_me() method
This commit is contained in:
parent
7acfd0c6c9
commit
cf6f300df1
|
@ -14,31 +14,44 @@ from .errors import (RPCError, InvalidDCError, FloodWaitError,
|
||||||
|
|
||||||
from .network import authenticator, MtProtoSender, TcpTransport
|
from .network import authenticator, MtProtoSender, TcpTransport
|
||||||
from .parser.markdown_parser import parse_message_entities
|
from .parser.markdown_parser import parse_message_entities
|
||||||
|
|
||||||
# For sending and receiving requests
|
# For sending and receiving requests
|
||||||
from .tl import MTProtoRequest, Session
|
from .tl import MTProtoRequest, Session
|
||||||
from .tl.all_tlobjects import layer
|
from .tl.all_tlobjects import layer
|
||||||
from .tl.functions import (InitConnectionRequest, InvokeWithLayerRequest,
|
from .tl.functions import (InitConnectionRequest, InvokeWithLayerRequest,
|
||||||
PingRequest)
|
PingRequest)
|
||||||
|
|
||||||
# The following is required to get the password salt
|
# Required to get the password salt
|
||||||
from .tl.functions.account import GetPasswordRequest
|
from .tl.functions.account import GetPasswordRequest
|
||||||
|
|
||||||
|
# Logging in and out
|
||||||
from .tl.functions.auth import (CheckPasswordRequest, LogOutRequest,
|
from .tl.functions.auth import (CheckPasswordRequest, LogOutRequest,
|
||||||
SendCodeRequest, SignInRequest,
|
SendCodeRequest, SignInRequest,
|
||||||
SignUpRequest, ImportBotAuthorizationRequest)
|
SignUpRequest, ImportBotAuthorizationRequest)
|
||||||
|
|
||||||
|
# Initial request
|
||||||
from .tl.functions.help import GetConfigRequest
|
from .tl.functions.help import GetConfigRequest
|
||||||
|
|
||||||
|
# Easier access to common methods
|
||||||
from .tl.functions.messages import (
|
from .tl.functions.messages import (
|
||||||
GetDialogsRequest, GetHistoryRequest, ReadHistoryRequest, SendMediaRequest,
|
GetDialogsRequest, GetHistoryRequest, ReadHistoryRequest, SendMediaRequest,
|
||||||
SendMessageRequest)
|
SendMessageRequest)
|
||||||
# The Requests and types that we'll be using
|
|
||||||
|
# For .get_me() and ensuring we're authorized
|
||||||
|
from telethon.tl.functions.users import GetUsersRequest
|
||||||
|
|
||||||
|
# Easier access for working with media, too
|
||||||
from .tl.functions.upload import (
|
from .tl.functions.upload import (
|
||||||
GetFileRequest, SaveBigFilePartRequest, SaveFilePartRequest)
|
GetFileRequest, SaveBigFilePartRequest, SaveFilePartRequest)
|
||||||
|
|
||||||
# All the types we need to work with
|
# All the types we need to work with
|
||||||
from .tl.types import (
|
from .tl.types import (
|
||||||
ChatPhotoEmpty, DocumentAttributeAudio, DocumentAttributeFilename,
|
ChatPhotoEmpty, DocumentAttributeAudio, DocumentAttributeFilename,
|
||||||
InputDocumentFileLocation, InputFile, InputFileBig, InputFileLocation,
|
InputDocumentFileLocation, InputFile, InputFileBig, InputFileLocation,
|
||||||
InputMediaUploadedDocument, InputMediaUploadedPhoto, InputPeerEmpty,
|
InputMediaUploadedDocument, InputMediaUploadedPhoto, InputPeerEmpty,
|
||||||
MessageMediaContact, MessageMediaDocument, MessageMediaPhoto,
|
MessageMediaContact, MessageMediaDocument, MessageMediaPhoto,
|
||||||
UserProfilePhotoEmpty)
|
UserProfilePhotoEmpty, InputUserSelf)
|
||||||
|
|
||||||
from .utils import (find_user_or_chat, get_input_peer,
|
from .utils import (find_user_or_chat, get_input_peer,
|
||||||
get_appropriated_part_size, get_extension)
|
get_appropriated_part_size, get_extension)
|
||||||
|
|
||||||
|
@ -371,11 +384,9 @@ class TelegramClient:
|
||||||
# region Authorization requests
|
# region Authorization requests
|
||||||
|
|
||||||
def is_user_authorized(self):
|
def is_user_authorized(self):
|
||||||
"""Has the user been authorized yet (code request sent and confirmed)?
|
"""Has the user been authorized yet
|
||||||
Note that this will NOT yield the correct result if the session was revoked by another client!"""
|
(code request sent and confirmed)?"""
|
||||||
if self.session and self.session.user is not None:
|
return self.session and self.get_me() is not None
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def send_code_request(self, phone_number):
|
def send_code_request(self, phone_number):
|
||||||
"""Sends a code request to the specified phone number"""
|
"""Sends a code request to the specified phone number"""
|
||||||
|
@ -421,10 +432,8 @@ class TelegramClient:
|
||||||
'You must provide a phone_number and a code for the first time, '
|
'You must provide a phone_number and a code for the first time, '
|
||||||
'and a password only if an RPCError was raised before.')
|
'and a password only if an RPCError was raised before.')
|
||||||
|
|
||||||
# Result is an Auth.Authorization TLObject
|
# Ignore 'result.user', we don't need it
|
||||||
self.session.user = result.user
|
#
|
||||||
self.session.save()
|
|
||||||
|
|
||||||
# If we want the connection to stay alive for a long time, we need
|
# If we want the connection to stay alive for a long time, we need
|
||||||
# to start the pings thread once we're already authorized and not
|
# to start the pings thread once we're already authorized and not
|
||||||
# before to avoid the updates thread trying to read anything while
|
# before to avoid the updates thread trying to read anything while
|
||||||
|
@ -463,6 +472,17 @@ class TelegramClient:
|
||||||
self.sender.logging_out = False
|
self.sender.logging_out = False
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def get_me(self):
|
||||||
|
"""Gets "me" (the self user) which is currently authenticated,
|
||||||
|
or None if the request fails (hence, not authenticated)."""
|
||||||
|
try:
|
||||||
|
return self.invoke(GetUsersRequest([InputUserSelf()]))[0]
|
||||||
|
except RPCError as e:
|
||||||
|
if e.code == 401: # 401 UNAUTHORIZED
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_sessions():
|
def list_sessions():
|
||||||
"""Lists all the sessions of the users who have ever connected
|
"""Lists all the sessions of the users who have ever connected
|
||||||
|
|
|
@ -18,6 +18,7 @@ class Session:
|
||||||
self.salt = 0 # Unsigned long
|
self.salt = 0 # Unsigned long
|
||||||
self.time_offset = 0
|
self.time_offset = 0
|
||||||
self.last_message_id = 0 # Long
|
self.last_message_id = 0 # Long
|
||||||
|
# TODO Remove this now unused members, left so unpickling is happy
|
||||||
self.user = None
|
self.user = None
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user