Skip to content

Commit f84f9ec

Browse files
bakatroubledelivrance
authored andcommitted
Add bot_token argument (pyrogram#221)
* Add bot_token argument (closes #123) * Make session_name docs more readable and detailed * Explicitely set is_bot=False
1 parent 87c4d08 commit f84f9ec

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

pyrogram/client/client.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import tempfile
3030
import threading
3131
import time
32+
import warnings
3233
from configparser import ConfigParser
3334
from datetime import datetime
3435
from 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('\nYou 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

pyrogram/client/ext/base_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class StopTransmission(StopIteration):
6868
}
6969

7070
def __init__(self):
71-
self.bot_token = None
71+
self.is_bot = None
7272
self.dc_id = None
7373
self.auth_key = None
7474
self.user_id = None

pyrogram/client/ext/syncer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def sync(cls, client):
9494
auth_key=auth_key,
9595
user_id=client.user_id,
9696
date=int(time.time()),
97+
is_bot=client.is_bot,
9798
peers_by_id={
9899
k: getattr(v, "access_hash", None)
99100
for k, v in client.peers_by_id.copy().items()

0 commit comments

Comments
 (0)