2017-06-08 14:12:57 +03:00
|
|
|
import logging
|
2018-03-02 22:05:09 +03:00
|
|
|
import platform
|
2017-09-29 21:50:27 +03:00
|
|
|
from datetime import timedelta, datetime
|
2018-06-08 22:52:59 +03:00
|
|
|
|
2018-01-19 13:47:45 +03:00
|
|
|
from . import version, utils
|
2018-01-18 15:55:03 +03:00
|
|
|
from .crypto import rsa
|
2018-06-08 22:52:59 +03:00
|
|
|
from .extensions import markdown
|
|
|
|
from .network import MTProtoSender, ConnectionTcpFull
|
2018-03-02 00:34:32 +03:00
|
|
|
from .sessions import Session, SQLiteSession
|
2018-01-20 00:55:28 +03:00
|
|
|
from .tl import TLObject
|
2017-09-17 17:17:55 +03:00
|
|
|
from .tl.all_tlobjects import LAYER
|
2017-08-24 14:02:48 +03:00
|
|
|
from .tl.functions import (
|
2018-06-08 22:52:59 +03:00
|
|
|
InitConnectionRequest, InvokeWithLayerRequest
|
2017-08-24 14:02:48 +03:00
|
|
|
)
|
2017-07-04 11:21:15 +03:00
|
|
|
from .tl.functions.auth import (
|
|
|
|
ImportAuthorizationRequest, ExportAuthorizationRequest
|
|
|
|
)
|
2017-09-04 18:18:33 +03:00
|
|
|
from .tl.functions.help import (
|
|
|
|
GetCdnConfigRequest, GetConfigRequest
|
|
|
|
)
|
2017-10-24 16:40:51 +03:00
|
|
|
from .tl.types.auth import ExportedAuthorization
|
2017-09-07 19:49:08 +03:00
|
|
|
from .update_state import UpdateState
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-12-28 03:04:11 +03:00
|
|
|
DEFAULT_DC_ID = 4
|
2017-11-16 15:30:18 +03:00
|
|
|
DEFAULT_IPV4_IP = '149.154.167.51'
|
|
|
|
DEFAULT_IPV6_IP = '[2001:67c:4e8:f002::a]'
|
|
|
|
DEFAULT_PORT = 443
|
|
|
|
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__ = logging.getLogger(__name__)
|
|
|
|
|
2017-11-16 15:30:18 +03:00
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
# TODO Do we need this class?
|
2017-06-08 14:12:57 +03:00
|
|
|
class TelegramBareClient:
|
2018-06-08 22:52:59 +03:00
|
|
|
"""
|
|
|
|
A bare Telegram client that somewhat eases the usage of the
|
|
|
|
``MTProtoSender``.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session (`str` | `telethon.sessions.abstract.Session`, `None`):
|
|
|
|
The file name of the session file to be used if a string is
|
|
|
|
given (it may be a full path), or the Session instance to be
|
|
|
|
used otherwise. If it's ``None``, the session will not be saved,
|
|
|
|
and you should call :meth:`.log_out()` when you're done.
|
|
|
|
|
|
|
|
Note that if you pass a string it will be a file in the current
|
|
|
|
working directory, although you can also pass absolute paths.
|
|
|
|
|
|
|
|
The session file contains enough information for you to login
|
|
|
|
without re-sending the code, so if you have to enter the code
|
|
|
|
more than once, maybe you're changing the working directory,
|
|
|
|
renaming or removing the file, or using random names.
|
|
|
|
|
|
|
|
api_id (`int` | `str`):
|
|
|
|
The API ID you obtained from https://my.telegram.org.
|
|
|
|
|
|
|
|
api_hash (`str`):
|
|
|
|
The API ID you obtained from https://my.telegram.org.
|
|
|
|
|
|
|
|
connection (`telethon.network.connection.common.Connection`, optional):
|
|
|
|
The connection instance to be used when creating a new connection
|
|
|
|
to the servers. If it's a type, the `proxy` argument will be used.
|
|
|
|
|
|
|
|
Defaults to `telethon.network.connection.tcpfull.ConnectionTcpFull`.
|
|
|
|
|
|
|
|
use_ipv6 (`bool`, optional):
|
|
|
|
Whether to connect to the servers through IPv6 or not.
|
|
|
|
By default this is ``False`` as IPv6 support is not
|
|
|
|
too widespread yet.
|
|
|
|
|
|
|
|
proxy (`tuple` | `dict`, optional):
|
|
|
|
A tuple consisting of ``(socks.SOCKS5, 'host', port)``.
|
|
|
|
See https://github.com/Anorov/PySocks#usage-1 for more.
|
|
|
|
|
|
|
|
update_workers (`int`, optional):
|
|
|
|
If specified, represents how many extra threads should
|
|
|
|
be spawned to handle incoming updates, and updates will
|
|
|
|
be kept in memory until they are processed. Note that
|
|
|
|
you must set this to at least ``0`` if you want to be
|
|
|
|
able to process updates through :meth:`updates.poll()`.
|
|
|
|
|
|
|
|
timeout (`int` | `float` | `timedelta`, optional):
|
|
|
|
The timeout to be used when receiving responses from
|
|
|
|
the network. Defaults to 5 seconds.
|
|
|
|
|
|
|
|
spawn_read_thread (`bool`, optional):
|
|
|
|
Whether to use an extra background thread or not. Defaults
|
|
|
|
to ``True`` so receiving items from the network happens
|
|
|
|
instantly, as soon as they arrive. Can still be disabled
|
|
|
|
if you want to run the library without any additional thread.
|
|
|
|
|
|
|
|
report_errors (`bool`, optional):
|
|
|
|
Whether to report RPC errors or not. Defaults to ``True``,
|
|
|
|
see :ref:`api-status` for more information.
|
|
|
|
|
|
|
|
device_model (`str`, optional):
|
|
|
|
"Device model" to be sent when creating the initial connection.
|
|
|
|
Defaults to ``platform.node()``.
|
|
|
|
|
|
|
|
system_version (`str`, optional):
|
|
|
|
"System version" to be sent when creating the initial connection.
|
|
|
|
Defaults to ``platform.system()``.
|
|
|
|
|
|
|
|
app_version (`str`, optional):
|
|
|
|
"App version" to be sent when creating the initial connection.
|
|
|
|
Defaults to `telethon.version.__version__`.
|
|
|
|
|
|
|
|
lang_code (`str`, optional):
|
|
|
|
"Language code" to be sent when creating the initial connection.
|
|
|
|
Defaults to ``'en'``.
|
|
|
|
|
|
|
|
system_lang_code (`str`, optional):
|
|
|
|
"System lang code" to be sent when creating the initial connection.
|
|
|
|
Defaults to `lang_code`.
|
2017-06-08 14:12:57 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Current TelegramClient version
|
2017-10-28 13:21:07 +03:00
|
|
|
__version__ = version.__version__
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
# Server configuration (with .dc_options)
|
|
|
|
_config = None
|
2017-09-17 15:30:23 +03:00
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
# region Initialization
|
|
|
|
|
2017-06-22 12:43:42 +03:00
|
|
|
def __init__(self, session, api_id, api_hash,
|
2018-05-10 15:22:19 +03:00
|
|
|
*,
|
|
|
|
connection=ConnectionTcpFull,
|
2017-11-16 15:30:18 +03:00
|
|
|
use_ipv6=False,
|
2017-09-07 19:49:08 +03:00
|
|
|
proxy=None,
|
2017-09-29 21:50:27 +03:00
|
|
|
timeout=timedelta(seconds=5),
|
2018-03-15 12:22:21 +03:00
|
|
|
report_errors=True,
|
2018-03-02 22:05:09 +03:00
|
|
|
device_model=None,
|
|
|
|
system_version=None,
|
|
|
|
app_version=None,
|
|
|
|
lang_code='en',
|
|
|
|
system_lang_code='en'):
|
2017-09-29 21:50:27 +03:00
|
|
|
"""Refer to TelegramClient.__init__ for docs on this method"""
|
|
|
|
if not api_id or not api_hash:
|
2017-12-28 02:22:28 +03:00
|
|
|
raise ValueError(
|
2017-09-29 21:50:27 +03:00
|
|
|
"Your API ID or Hash cannot be empty or None. "
|
2018-01-08 16:04:04 +03:00
|
|
|
"Refer to telethon.rtfd.io for more information.")
|
2017-09-29 21:50:27 +03:00
|
|
|
|
2017-11-16 15:30:18 +03:00
|
|
|
self._use_ipv6 = use_ipv6
|
2018-03-02 00:34:32 +03:00
|
|
|
|
2017-09-29 21:50:27 +03:00
|
|
|
# Determine what session object we have
|
|
|
|
if isinstance(session, str) or session is None:
|
2018-03-02 00:34:32 +03:00
|
|
|
session = SQLiteSession(session)
|
2017-09-29 21:50:27 +03:00
|
|
|
elif not isinstance(session, Session):
|
2017-12-28 02:22:28 +03:00
|
|
|
raise TypeError(
|
2017-09-29 21:50:27 +03:00
|
|
|
'The given session must be a str or a Session instance.'
|
|
|
|
)
|
|
|
|
|
2017-11-16 15:40:25 +03:00
|
|
|
# ':' in session.server_address is True if it's an IPv6 address
|
|
|
|
if (not session.server_address or
|
|
|
|
(':' in session.server_address) != use_ipv6):
|
2017-12-28 03:04:11 +03:00
|
|
|
session.set_dc(
|
|
|
|
DEFAULT_DC_ID,
|
|
|
|
DEFAULT_IPV6_IP if self._use_ipv6 else DEFAULT_IPV4_IP,
|
|
|
|
DEFAULT_PORT
|
|
|
|
)
|
2017-11-16 15:30:18 +03:00
|
|
|
|
2018-03-15 12:22:21 +03:00
|
|
|
session.report_errors = report_errors
|
2017-06-08 14:12:57 +03:00
|
|
|
self.session = session
|
2017-06-11 23:42:04 +03:00
|
|
|
self.api_id = int(api_id)
|
2017-06-08 14:12:57 +03:00
|
|
|
self.api_hash = api_hash
|
2017-09-21 14:43:33 +03:00
|
|
|
|
2017-09-30 12:45:35 +03:00
|
|
|
# This is the main sender, which will be used from the thread
|
|
|
|
# that calls .connect(). Every other thread will spawn a new
|
|
|
|
# temporary connection. The connection on this one is always
|
|
|
|
# kept open so Telegram can send us updates.
|
2018-05-10 15:22:19 +03:00
|
|
|
if isinstance(connection, type):
|
|
|
|
connection = connection(proxy=proxy, timeout=timeout)
|
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
self._sender = MTProtoSender(self.session, connection)
|
2017-09-22 13:20:38 +03:00
|
|
|
|
2017-09-30 17:32:10 +03:00
|
|
|
# Cache "exported" sessions as 'dc_id: Session' not to recreate
|
|
|
|
# them all the time since generating a new key is a relatively
|
|
|
|
# expensive operation.
|
|
|
|
self._exported_sessions = {}
|
2017-07-04 11:21:15 +03:00
|
|
|
|
2017-09-07 19:49:08 +03:00
|
|
|
# This member will process updates if enabled.
|
|
|
|
# One may change self.updates.enabled at any later point.
|
2018-06-08 22:52:59 +03:00
|
|
|
# TODO Stop using that 1
|
|
|
|
self.updates = UpdateState(1)
|
2017-09-02 22:45:27 +03:00
|
|
|
|
2017-09-29 21:50:27 +03:00
|
|
|
# Used on connection - the user may modify these and reconnect
|
2018-03-02 22:05:09 +03:00
|
|
|
system = platform.uname()
|
|
|
|
self.device_model = device_model or system.system or 'Unknown'
|
|
|
|
self.system_version = system_version or system.release or '1.0'
|
|
|
|
self.app_version = app_version or self.__version__
|
|
|
|
self.lang_code = lang_code
|
|
|
|
self.system_lang_code = system_lang_code
|
2017-09-29 21:50:27 +03:00
|
|
|
|
|
|
|
# Save whether the user is authorized here (a.k.a. logged in)
|
2017-10-18 13:17:13 +03:00
|
|
|
self._authorized = None # None = We don't know yet
|
2017-09-29 21:50:27 +03:00
|
|
|
|
2017-12-28 03:13:24 +03:00
|
|
|
# The first request must be in invokeWithLayer(initConnection(X)).
|
|
|
|
# See https://core.telegram.org/api/invoking#saving-client-info.
|
|
|
|
self._first_request = True
|
|
|
|
|
2017-09-29 21:50:27 +03:00
|
|
|
# Default PingRequest delay
|
|
|
|
self._last_ping = datetime.now()
|
|
|
|
self._ping_delay = timedelta(minutes=1)
|
|
|
|
|
2018-02-15 13:41:32 +03:00
|
|
|
# Also have another delay for GetStateRequest.
|
|
|
|
#
|
|
|
|
# If the connection is kept alive for long without invoking any
|
|
|
|
# high level request the server simply stops sending updates.
|
|
|
|
# TODO maybe we can have ._last_request instead if any req works?
|
|
|
|
self._last_state = datetime.now()
|
|
|
|
self._state_delay = timedelta(hours=1)
|
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
# Some further state for subclasses
|
|
|
|
self._event_builders = []
|
|
|
|
self._events_pending_resolve = []
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
# Default parse mode
|
|
|
|
self._parse_mode = markdown
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
# Some fields to easy signing in. Let {phone: hash} be
|
|
|
|
# a dictionary because the user may change their mind.
|
|
|
|
self._phone_code_hash = {}
|
|
|
|
self._phone = None
|
|
|
|
self._tos = None
|
2017-06-09 11:35:19 +03:00
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
# Sometimes we need to know who we are, cache the self peer
|
|
|
|
self._self_input_peer = None
|
2017-09-30 17:32:10 +03:00
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
# endregion
|
2017-12-20 14:47:10 +03:00
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
# region Connecting
|
2017-09-29 21:50:27 +03:00
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
async def connect(self, _sync_updates=True):
|
|
|
|
"""
|
|
|
|
Connects to Telegram.
|
|
|
|
"""
|
|
|
|
# TODO Maybe we should rethink what the session does if the sender
|
|
|
|
# needs a session but it might connect to arbitrary IPs?
|
|
|
|
#
|
|
|
|
# TODO sync updates/connected and authorized if no UnauthorizedError?
|
|
|
|
await self._sender.connect(
|
|
|
|
self.session.server_address, self.session.port)
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-09-17 17:39:29 +03:00
|
|
|
def is_connected(self):
|
2018-06-08 22:52:59 +03:00
|
|
|
"""
|
|
|
|
Returns ``True`` if the user has connected.
|
|
|
|
"""
|
2017-09-21 14:43:33 +03:00
|
|
|
return self._sender.is_connected()
|
2017-09-17 17:39:29 +03:00
|
|
|
|
2017-10-24 16:40:51 +03:00
|
|
|
def _wrap_init_connection(self, query):
|
2018-06-08 22:52:59 +03:00
|
|
|
"""
|
|
|
|
Wraps `query` around
|
|
|
|
``InvokeWithLayerRequest(InitConnectionRequest(...))``.
|
|
|
|
"""
|
2017-10-24 16:40:51 +03:00
|
|
|
return InvokeWithLayerRequest(LAYER, InitConnectionRequest(
|
2017-09-17 15:25:53 +03:00
|
|
|
api_id=self.api_id,
|
2018-03-02 22:05:09 +03:00
|
|
|
device_model=self.device_model,
|
|
|
|
system_version=self.system_version,
|
|
|
|
app_version=self.app_version,
|
|
|
|
lang_code=self.lang_code,
|
|
|
|
system_lang_code=self.system_lang_code,
|
2017-09-17 15:25:53 +03:00
|
|
|
lang_pack='', # "langPacks are for official apps only"
|
|
|
|
query=query
|
2017-10-24 16:40:51 +03:00
|
|
|
))
|
2017-09-17 15:25:53 +03:00
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
async def disconnect(self):
|
|
|
|
"""
|
|
|
|
Disconnects from Telegram.
|
|
|
|
"""
|
|
|
|
await self._sender.disconnect()
|
|
|
|
# TODO What to do with the update state? Does it belong here?
|
|
|
|
# self.session.set_update_state(0, self.updates.get_update_state(0))
|
2018-01-26 11:59:49 +03:00
|
|
|
self.session.close()
|
2017-09-29 21:50:27 +03:00
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
def _switch_dc(self, new_dc):
|
2017-06-08 17:51:20 +03:00
|
|
|
"""
|
2018-06-08 22:52:59 +03:00
|
|
|
Switches the current connection to the new data center.
|
|
|
|
"""
|
|
|
|
# TODO Implement
|
|
|
|
raise NotImplementedError
|
|
|
|
dc = self._get_dc(new_dc)
|
|
|
|
__log__.info('Reconnecting to new data center %s', dc)
|
|
|
|
|
|
|
|
self.session.set_dc(dc.id, dc.ip_address, dc.port)
|
|
|
|
# auth_key's are associated with a server, which has now changed
|
|
|
|
# so it's not valid anymore. Set to None to force recreating it.
|
|
|
|
self.session.auth_key = None
|
|
|
|
self.session.save()
|
|
|
|
self.disconnect()
|
|
|
|
return self.connect()
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-12-28 02:09:29 +03:00
|
|
|
def set_proxy(self, proxy):
|
2017-12-28 00:50:49 +03:00
|
|
|
"""Change the proxy used by the connections.
|
|
|
|
"""
|
|
|
|
if self.is_connected():
|
|
|
|
raise RuntimeError("You can't change the proxy while connected.")
|
2018-06-08 22:52:59 +03:00
|
|
|
|
|
|
|
# TODO Should we tell the user to create a new client?
|
|
|
|
# Can this be done more cleanly? Similar to `switch_dc`
|
|
|
|
self._sender._connection.conn.proxy = proxy
|
2017-12-28 00:50:49 +03:00
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
# endregion
|
|
|
|
|
2017-09-29 21:50:27 +03:00
|
|
|
# region Working with different connections/Data Centers
|
|
|
|
|
2017-11-16 15:30:18 +03:00
|
|
|
def _get_dc(self, dc_id, cdn=False):
|
2017-06-08 14:12:57 +03:00
|
|
|
"""Gets the Data Center (DC) associated to 'dc_id'"""
|
2017-10-24 16:40:51 +03:00
|
|
|
if not TelegramBareClient._config:
|
|
|
|
TelegramBareClient._config = self(GetConfigRequest())
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-08-24 14:02:48 +03:00
|
|
|
try:
|
2017-09-05 17:11:02 +03:00
|
|
|
if cdn:
|
|
|
|
# Ensure we have the latest keys for the CDNs
|
|
|
|
for pk in self(GetCdnConfigRequest()).public_keys:
|
|
|
|
rsa.add_key(pk.public_key)
|
|
|
|
|
2017-08-29 14:49:41 +03:00
|
|
|
return next(
|
2017-10-24 16:40:51 +03:00
|
|
|
dc for dc in TelegramBareClient._config.dc_options
|
2017-11-16 15:30:18 +03:00
|
|
|
if dc.id == dc_id and bool(dc.ipv6) == self._use_ipv6 and bool(dc.cdn) == cdn
|
2017-08-29 14:49:41 +03:00
|
|
|
)
|
2017-08-24 14:02:48 +03:00
|
|
|
except StopIteration:
|
|
|
|
if not cdn:
|
|
|
|
raise
|
|
|
|
|
2017-09-05 17:11:02 +03:00
|
|
|
# New configuration, perhaps a new CDN was added?
|
2017-10-24 16:40:51 +03:00
|
|
|
TelegramBareClient._config = self(GetConfigRequest())
|
2017-11-16 15:30:18 +03:00
|
|
|
return self._get_dc(dc_id, cdn=cdn)
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2017-09-30 17:32:10 +03:00
|
|
|
def _get_exported_client(self, dc_id):
|
|
|
|
"""Creates and connects a new TelegramBareClient for the desired DC.
|
2017-07-04 11:21:15 +03:00
|
|
|
|
2017-09-30 17:32:10 +03:00
|
|
|
If it's the first time calling the method with a given dc_id,
|
|
|
|
a new session will be first created, and its auth key generated.
|
|
|
|
Exporting/Importing the authorization will also be done so that
|
|
|
|
the auth is bound with the key.
|
2017-07-04 11:21:15 +03:00
|
|
|
"""
|
|
|
|
# Thanks badoualy/kotlogram on /telegram/api/DefaultTelegramClient.kt
|
|
|
|
# for clearly showing how to export the authorization! ^^
|
2017-09-30 17:32:10 +03:00
|
|
|
session = self._exported_sessions.get(dc_id)
|
|
|
|
if session:
|
|
|
|
export_auth = None # Already bound with the auth key
|
2017-07-04 11:21:15 +03:00
|
|
|
else:
|
2017-09-30 17:32:10 +03:00
|
|
|
# TODO Add a lock, don't allow two threads to create an auth key
|
2017-09-30 18:51:07 +03:00
|
|
|
# (when calling .connect() if there wasn't a previous session).
|
2017-09-30 17:32:10 +03:00
|
|
|
# for the same data center.
|
2017-07-04 11:21:15 +03:00
|
|
|
dc = self._get_dc(dc_id)
|
|
|
|
|
|
|
|
# Export the current authorization to the new DC.
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.info('Exporting authorization for data center %s', dc)
|
2017-07-04 11:21:15 +03:00
|
|
|
export_auth = self(ExportAuthorizationRequest(dc_id))
|
|
|
|
|
|
|
|
# Create a temporary session for this IP address, which needs
|
|
|
|
# to be different because each auth_key is unique per DC.
|
|
|
|
#
|
|
|
|
# Construct this session with the connection parameters
|
|
|
|
# (system version, device model...) from the current one.
|
2018-03-02 00:34:32 +03:00
|
|
|
session = self.session.clone()
|
2017-12-28 03:04:11 +03:00
|
|
|
session.set_dc(dc.id, dc.ip_address, dc.port)
|
2017-09-30 17:32:10 +03:00
|
|
|
self._exported_sessions[dc_id] = session
|
2017-07-04 11:21:15 +03:00
|
|
|
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.info('Creating exported new client')
|
2017-09-30 17:32:10 +03:00
|
|
|
client = TelegramBareClient(
|
|
|
|
session, self.api_id, self.api_hash,
|
|
|
|
proxy=self._sender.connection.conn.proxy,
|
|
|
|
timeout=self._sender.connection.get_timeout()
|
|
|
|
)
|
2017-10-24 16:40:51 +03:00
|
|
|
client.connect(_sync_updates=False)
|
|
|
|
if isinstance(export_auth, ExportedAuthorization):
|
|
|
|
client(ImportAuthorizationRequest(
|
|
|
|
id=export_auth.id, bytes=export_auth.bytes
|
|
|
|
))
|
|
|
|
elif export_auth is not None:
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.warning('Unknown export auth type %s', export_auth)
|
2017-10-24 16:40:51 +03:00
|
|
|
|
2017-09-30 19:33:34 +03:00
|
|
|
client._authorized = True # We exported the auth, so we got auth
|
2017-09-30 17:32:10 +03:00
|
|
|
return client
|
2017-07-04 11:21:15 +03:00
|
|
|
|
2017-09-30 18:51:07 +03:00
|
|
|
def _get_cdn_client(self, cdn_redirect):
|
|
|
|
"""Similar to ._get_exported_client, but for CDNs"""
|
|
|
|
session = self._exported_sessions.get(cdn_redirect.dc_id)
|
|
|
|
if not session:
|
|
|
|
dc = self._get_dc(cdn_redirect.dc_id, cdn=True)
|
2018-03-02 00:34:32 +03:00
|
|
|
session = self.session.clone()
|
2017-12-28 03:04:11 +03:00
|
|
|
session.set_dc(dc.id, dc.ip_address, dc.port)
|
2017-09-30 18:51:07 +03:00
|
|
|
self._exported_sessions[cdn_redirect.dc_id] = session
|
|
|
|
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.info('Creating new CDN client')
|
2017-09-30 18:51:07 +03:00
|
|
|
client = TelegramBareClient(
|
|
|
|
session, self.api_id, self.api_hash,
|
|
|
|
proxy=self._sender.connection.conn.proxy,
|
|
|
|
timeout=self._sender.connection.get_timeout()
|
|
|
|
)
|
|
|
|
|
|
|
|
# This will make use of the new RSA keys for this specific CDN.
|
|
|
|
#
|
2017-10-24 16:40:51 +03:00
|
|
|
# We won't be calling GetConfigRequest because it's only called
|
|
|
|
# when needed by ._get_dc, and also it's static so it's likely
|
|
|
|
# set already. Avoid invoking non-CDN methods by not syncing updates.
|
|
|
|
client.connect(_sync_updates=False)
|
2017-09-30 19:33:34 +03:00
|
|
|
client._authorized = self._authorized
|
2017-09-30 18:51:07 +03:00
|
|
|
return client
|
|
|
|
|
2017-06-08 14:12:57 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Invoking Telegram requests
|
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
async def __call__(self, request, ordered=False):
|
2018-05-09 11:19:45 +03:00
|
|
|
"""
|
|
|
|
Invokes (sends) one or more MTProtoRequests and returns (receives)
|
|
|
|
their result.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (`TLObject` | `list`):
|
|
|
|
The request or requests to be invoked.
|
|
|
|
|
|
|
|
retries (`bool`, optional):
|
|
|
|
How many times the request should be retried automatically
|
|
|
|
in case it fails with a non-RPC error.
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2018-05-09 11:19:45 +03:00
|
|
|
The invoke will be retried up to 'retries' times before raising
|
|
|
|
``RuntimeError``.
|
2018-05-09 10:46:07 +03:00
|
|
|
|
2018-05-09 11:19:45 +03:00
|
|
|
ordered (`bool`, optional):
|
|
|
|
Whether the requests (if more than one was given) should be
|
|
|
|
executed sequentially on the server. They run in arbitrary
|
|
|
|
order by default.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The result of the request (often a `TLObject`) or a list of
|
|
|
|
results if more than one request was given.
|
2017-06-08 14:12:57 +03:00
|
|
|
"""
|
2018-06-08 22:52:59 +03:00
|
|
|
requests = (request,) if not utils.is_list_like(request) else request
|
2017-09-25 21:52:27 +03:00
|
|
|
if not all(isinstance(x, TLObject) and
|
2018-06-08 22:52:59 +03:00
|
|
|
x.content_related for x in requests):
|
2017-12-28 02:22:28 +03:00
|
|
|
raise TypeError('You can only invoke requests, not types!')
|
2017-06-08 14:12:57 +03:00
|
|
|
|
2018-06-08 22:52:59 +03:00
|
|
|
# TODO Resolve requests, should be done by TelegramClient
|
|
|
|
# for r in requests:
|
|
|
|
# await r.resolve(self, utils)
|
|
|
|
|
|
|
|
# TODO InvokeWithLayer if no authkey, maybe done in MTProtoSender?
|
|
|
|
# TODO Handle PhoneMigrateError, NetworkMigrateError, UserMigrateError
|
|
|
|
# ^ by switching DC
|
|
|
|
# TODO Retry on ServerError, RpcCallFailError
|
|
|
|
# TODO Auto-sleep on some FloodWaitError, FloodTestPhoneWaitError
|
|
|
|
future = await self._sender.send(request, ordered=ordered)
|
|
|
|
if isinstance(future, list):
|
|
|
|
results = []
|
|
|
|
for f in future:
|
|
|
|
results.append(await future)
|
|
|
|
return results
|
2017-12-20 14:47:10 +03:00
|
|
|
else:
|
2018-06-08 22:52:59 +03:00
|
|
|
return await future
|
2017-09-30 12:45:35 +03:00
|
|
|
|
2017-10-01 17:04:14 +03:00
|
|
|
# Let people use client.invoke(SomeRequest()) instead client(...)
|
|
|
|
invoke = __call__
|
|
|
|
|
2017-09-29 21:50:27 +03:00
|
|
|
# endregion
|