2929import tempfile
3030import threading
3131import time
32+ import warnings
3233from configparser import ConfigParser
3334from datetime import datetime
3435from hashlib import sha256 , md5
@@ -67,10 +68,10 @@ class Client(Methods, BaseClient):
6768
6869 Args:
6970 session_name (``str``):
70- Name to uniquely identify a session of either a User or a Bot.
71- For Users: pass a string of your choice, e.g.: "my_main_account" .
72- For Bots: pass your Bot API token, e.g.: "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
73- Note: as long as a valid User session file exists, Pyrogram won't ask you again to input your phone number .
71+ Name to uniquely identify a session of either a User or a Bot, e.g.: "my_account". This name will be used
72+ to save a file to disk that stores details needed for reconnecting without asking again for credentials .
73+ Note for bots: You can pass a bot token here, but this usage will be deprecated in next releases.
74+ Use *bot_token* instead .
7475
7576 api_id (``int``, *optional*):
7677 The *api_id* part of your Telegram API Key, as integer. E.g.: 12345
@@ -144,6 +145,10 @@ class Client(Methods, BaseClient):
144145 a new Telegram account in case the phone number you passed is not registered yet.
145146 Only applicable for new sessions.
146147
148+ bot_token (``str``, *optional*):
149+ Pass your Bot API token to create a bot session, e.g.: "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
150+ Only applicable for new sessions.
151+
147152 last_name (``str``, *optional*):
148153 Same purpose as *first_name*; pass a Last Name to avoid entering it manually. It can
149154 be an empty string: "". Only applicable for new sessions.
@@ -192,6 +197,7 @@ def __init__(self,
192197 password : str = None ,
193198 recovery_code : callable = None ,
194199 force_sms : bool = False ,
200+ bot_token : str = None ,
195201 first_name : str = None ,
196202 last_name : str = None ,
197203 workers : int = BaseClient .WORKERS ,
@@ -218,6 +224,7 @@ def __init__(self,
218224 self .password = password
219225 self .recovery_code = recovery_code
220226 self .force_sms = force_sms
227+ self .bot_token = bot_token
221228 self .first_name = first_name
222229 self .last_name = last_name
223230 self .workers = workers
@@ -263,8 +270,13 @@ def start(self):
263270 raise ConnectionError ("Client has already been started" )
264271
265272 if self .BOT_TOKEN_RE .match (self .session_name ):
273+ self .is_bot = True
266274 self .bot_token = self .session_name
267275 self .session_name = self .session_name .split (":" )[0 ]
276+ warnings .warn ('\n You are using a bot token as session name.\n '
277+ 'It will be deprecated in next update, please use session file name to load '
278+ 'existing sessions and bot_token argument to create new sessions.' ,
279+ DeprecationWarning , stacklevel = 2 )
268280
269281 self .load_config ()
270282 self .load_session ()
@@ -282,13 +294,15 @@ def start(self):
282294 try :
283295 if self .user_id is None :
284296 if self .bot_token is None :
297+ self .is_bot = False
285298 self .authorize_user ()
286299 else :
300+ self .is_bot = True
287301 self .authorize_bot ()
288302
289303 self .save_session ()
290304
291- if self .bot_token is None :
305+ if not self .is_bot :
292306 if self .takeout :
293307 self .takeout_id = self .send (functions .account .InitTakeoutSession ()).id
294308 log .warning ("Takeout session {} initiated" .format (self .takeout_id ))
@@ -1113,6 +1127,8 @@ def load_session(self):
11131127 self .auth_key = base64 .b64decode ("" .join (s ["auth_key" ]))
11141128 self .user_id = s ["user_id" ]
11151129 self .date = s .get ("date" , 0 )
1130+ # TODO: replace default with False once token session name will be deprecated
1131+ self .is_bot = s .get ("is_bot" , self .is_bot )
11161132
11171133 for k , v in s .get ("peers_by_id" , {}).items ():
11181134 self .peers_by_id [int (k )] = utils .get_input_peer (int (k ), v )
@@ -1246,7 +1262,8 @@ def save_session(self):
12461262 test_mode = self .test_mode ,
12471263 auth_key = auth_key ,
12481264 user_id = self .user_id ,
1249- date = self .date
1265+ date = self .date ,
1266+ is_bot = self .is_bot ,
12501267 ),
12511268 f ,
12521269 indent = 4
0 commit comments