-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsend_code.py
More file actions
65 lines (52 loc) · 2.1 KB
/
Copy pathsend_code.py
File metadata and controls
65 lines (52 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import annotations
import logging
import pyrogram
from pyrogram import raw, types
from pyrogram.errors import NetworkMigrate, PhoneMigrate
from pyrogram.session import Auth, Session
log = logging.getLogger(__name__)
class SendCode:
async def send_code(self: pyrogram.Client, phone_number: str) -> types.SentCode:
"""Send the confirmation code to the given phone number.
.. include:: /_includes/usable-by/users.rst
Parameters:
phone_number (``str``):
Phone number in international format (includes the country prefix).
Returns:
:obj:`~pyrogram.types.SentCode`: On success, an object containing information on the sent confirmation code
is returned.
Raises:
BadRequest: In case the phone number is invalid.
"""
phone_number = phone_number.strip(" +")
while True:
if not self.api_id or not self.api_hash:
raise ValueError("api_id and api_hash are required")
try:
r = await self.invoke(
raw.functions.auth.SendCode(
phone_number=phone_number,
api_id=self.api_id,
api_hash=self.api_hash,
settings=raw.types.CodeSettings(),
),
)
except (PhoneMigrate, NetworkMigrate) as e:
await self.session.stop()
await self.storage.dc_id(e.value)
await self.storage.auth_key(
await Auth(
self,
await self.storage.dc_id(),
await self.storage.test_mode(),
).create(),
)
self.session = Session(
self,
await self.storage.dc_id(),
await self.storage.auth_key(),
await self.storage.test_mode(),
)
await self.session.start()
else:
return types.SentCode._parse(r)