Telethon/client/doc/concepts/sessions.rst

59 lines
2.9 KiB
ReStructuredText
Raw Permalink Normal View History

2023-09-13 20:01:16 +03:00
Sessions
========
.. currentmodule:: telethon
In Telethon, the word :term:`session` is used to refer to the set of data needed to connect to Telegram.
This includes the server address of your home data center, as well as the authorization key bound to an account.
2023-09-13 20:01:16 +03:00
When you first connect to Telegram, an authorization key is generated to encrypt all communication.
After login, Telegram remembers this authorization key as logged-in, so you don't need to login again.
.. important::
**Do not leak the session file!**
Anyone with that file can login to the account stored in it.
If you believe someone else has obtained this file, immediately revoke all active sessions from an official client.
Some auxiliary information such as the user ID of the logged-in user is also kept.
The update state, which can change every time an update is received from Telegram, is also stored in the session.
Telethon needs this information to catch up on all missed updates while your code was not running.
This is why it's important to call :meth:`Client.disconnect`.
Doing so flushes all the update state to the session and saves it.
Session files
-------------
Telethon defaults to using SQLite to store the session state.
The session state is written to ``.session`` files, so make sure your VCS ignores them!
To make sure the ``.session`` file is saved, you should call :meth:`Client.disconnect` before exiting the program.
The first parameter in the :class:`Client` constructor is the session to use.
You can use a `str`, a :class:`pathlib.Path` or a :class:`session.Storage`.
The string or path are relative to the Current Working Directory.
You can use absolute paths or relative paths to folders elsewhere.
The ``.session`` extension is automatically added if the path has no extension.
Session storages
----------------
The :class:`session.Storage` abstract base class defines the required methods to create custom storages.
Telethon comes with two built-in storages:
* :class:`~session.SqliteSession`. This is used by default when a string or path is used.
2023-10-28 21:05:17 +03:00
* :class:`~session.MemorySession`. This is used by default when the path is :data:`None`.
2023-09-13 20:01:16 +03:00
You can also use it directly when you have a :class:`~session.Session` instance.
It's useful when you don't have file-system access.
If you would like to store the session state in a different way, you can subclass :class:`session.Storage`.
You may also find `custom third-party session storages in Telethon's wiki <https://github.com/LonamiWebs/Telethon/wiki/Session-Storages>`_.
Be careful with any third-party code you install, as they could steal the login credentials.
Only use session storages you trust, and pin the specific versions you have audited.
2023-09-13 20:01:16 +03:00
Some Python installations do not have the ``sqlite3`` module.
In this case, attempting to use the default :class:`~session.SqliteSession` will fail.
If this happens, you can try reinstalling Python.
If you still don't have the ``sqlite3`` module, you should use a different storage.