mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-29 13:27:47 +00:00
Added possibility to choose config file, implemented temporary sessions
This commit is contained in:
parent
155087e305
commit
fa99cbbaa2
@ -61,9 +61,13 @@ class Client:
|
|||||||
invoke every single Telegram API method available.
|
invoke every single Telegram API method available.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
session_name (:obj:`str`):
|
config_file (:obj:`str`, optional):
|
||||||
|
Path of the configuration file containing the api_id and api_hash parameters.
|
||||||
|
|
||||||
|
session_name (:obj:`str`, optional):
|
||||||
Name to uniquely identify an authorized session. It will be used
|
Name to uniquely identify an authorized session. It will be used
|
||||||
to save the session to a file named ``<session_name>.session``.
|
to save the session to a file named ``<session_name>.session``.
|
||||||
|
If left to None the session will be temporary and will not be saved.
|
||||||
|
|
||||||
test_mode (:obj:`bool`, optional):
|
test_mode (:obj:`bool`, optional):
|
||||||
Enable or disable log-in to testing servers. Defaults to False.
|
Enable or disable log-in to testing servers. Defaults to False.
|
||||||
@ -74,7 +78,8 @@ class Client:
|
|||||||
INVITE_LINK_RE = re.compile(r"^(?:https?:\/\/)?t\.me\/joinchat\/(.+)$")
|
INVITE_LINK_RE = re.compile(r"^(?:https?:\/\/)?t\.me\/joinchat\/(.+)$")
|
||||||
DIALOGS_AT_ONCE = 100
|
DIALOGS_AT_ONCE = 100
|
||||||
|
|
||||||
def __init__(self, session_name: str, test_mode: bool = False):
|
def __init__(self, config_file: str = "config.ini", session_name: str = None, test_mode: bool = False):
|
||||||
|
self.config_file = config_file
|
||||||
self.session_name = session_name
|
self.session_name = session_name
|
||||||
self.test_mode = test_mode
|
self.test_mode = test_mode
|
||||||
|
|
||||||
@ -98,7 +103,7 @@ class Client:
|
|||||||
def start(self):
|
def start(self):
|
||||||
"""Use this method to start the Client after creating it.
|
"""Use this method to start the Client after creating it.
|
||||||
Requires no parameters."""
|
Requires no parameters."""
|
||||||
self.load_config()
|
self.load_config(self.config_file)
|
||||||
self.load_session(self.session_name)
|
self.load_session(self.session_name)
|
||||||
|
|
||||||
self.session = Session(self.dc_id, self.test_mode, self.auth_key, self.config.api_id)
|
self.session = Session(self.dc_id, self.test_mode, self.auth_key, self.config.api_id)
|
||||||
@ -289,9 +294,9 @@ class Client:
|
|||||||
|
|
||||||
return r.user.id
|
return r.user.id
|
||||||
|
|
||||||
def load_config(self):
|
def load_config(self, config_file):
|
||||||
config = ConfigParser()
|
config = ConfigParser()
|
||||||
config.read("config.ini")
|
config.read(config_file)
|
||||||
|
|
||||||
self.config = Config(
|
self.config = Config(
|
||||||
int(config["pyrogram"]["api_id"]),
|
int(config["pyrogram"]["api_id"]),
|
||||||
@ -300,6 +305,9 @@ class Client:
|
|||||||
|
|
||||||
def load_session(self, session_name):
|
def load_session(self, session_name):
|
||||||
try:
|
try:
|
||||||
|
if session_name is None:
|
||||||
|
raise FileNotFoundError
|
||||||
|
else:
|
||||||
with open("{}.session".format(session_name)) as f:
|
with open("{}.session".format(session_name)) as f:
|
||||||
s = json.load(f)
|
s = json.load(f)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
@ -312,6 +320,7 @@ class Client:
|
|||||||
self.user_id = s["user_id"]
|
self.user_id = s["user_id"]
|
||||||
|
|
||||||
def save_session(self):
|
def save_session(self):
|
||||||
|
if session_name is not None:
|
||||||
auth_key = base64.b64encode(self.auth_key).decode()
|
auth_key = base64.b64encode(self.auth_key).decode()
|
||||||
auth_key = [auth_key[i: i + 43] for i in range(0, len(auth_key), 43)]
|
auth_key = [auth_key[i: i + 43] for i in range(0, len(auth_key), 43)]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user