Clarify documentation on connection and receiving updates

This commit is contained in:
Lonami Exo 2019-05-23 12:11:58 +02:00
parent 6e5f90730e
commit 4ebf825c43
2 changed files with 31 additions and 8 deletions

View File

@ -385,6 +385,16 @@ class TelegramBaseClient(abc.ABC):
""" """
Connects to Telegram. Connects to Telegram.
.. note::
Connect means connect and nothing else, and only one low-level
request is made to notify Telegram about which layer we will be
using.
Before Telegram sends you updates, you need to make a high-level
request, like `client.get_me() <telethon.client.users.UserMethods.get_me>`,
as described in https://core.telegram.org/api/updates.
Example Example
.. code-block:: python .. code-block:: python

View File

@ -19,7 +19,9 @@ class UpdateMethods(UserMethods):
async def _run_until_disconnected(self: 'TelegramClient'): async def _run_until_disconnected(self: 'TelegramClient'):
try: try:
await self.disconnected # Make a high-level request to notify that we want updates
await self(functions.updates.GetStateRequest())
return await self.disconnected
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally:
@ -29,17 +31,28 @@ class UpdateMethods(UserMethods):
""" """
Runs the event loop until the library is disconnected. Runs the event loop until the library is disconnected.
Disconnections can be manual (by calling `disconnect() It also notifies Telegram that we want to receive updates
<telethon.client.telegrambaseclient.TelegramBaseClient.disconnect>`) as described in https://core.telegram.org/api/updates.
or not (if the library fails to reconnect automatically).
If a disconnection error occurs (i.e. not manual disconnect), Manual disconnections can be made by calling `disconnect()
said error will be raised through here, so you have a chance <telethon.client.telegrambaseclient.TelegramBaseClient.disconnect>`
to ``except`` it on your own code. or sending a ``KeyboardInterrupt`` (e.g. by pressing ``Ctrl+C`` on
the console window running the script).
If a disconnection error occurs (i.e. the library fails to reconnect
automatically), said error will be raised through here, so you have a
chance to ``except`` it on your own code.
If the loop is already running, this method returns a coroutine If the loop is already running, this method returns a coroutine
that you should await on your own code. that you should await on your own code.
.. note::
If you want to handle ``KeyboardInterrupt`` in your code,
simply run the event loop in your code too in any way, such as
``loop.run_forever()`` or ``await client.disconnected`` (e.g.
``loop.run_until_complete(client.disconnected)``).
Example Example
.. code-block:: python .. code-block:: python
@ -52,7 +65,7 @@ class UpdateMethods(UserMethods):
if self.loop.is_running(): if self.loop.is_running():
return self._run_until_disconnected() return self._run_until_disconnected()
try: try:
return self.loop.run_until_complete(self.disconnected) return self.loop.run_until_complete(self._run_until_disconnected())
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally: