Skip to Content
GuidesConsistency and sessions

Consistency and sessions

Validation checks the values a session is about to send. The consistency checks ask the server how it sees a live session: whether the requests an official client makes succeed, whether the session counts as an official app, and whether the layer and the lang pack fit. The same client then lists the account’s sessions, prints them as a table and ends the ones you no longer want.

Warning:

Every step here talks to Telegram as the logged-in account. Run it on accounts you own, and double-check a hash before terminating: an ended session cannot be restored, and the device using it is logged out.

Connect with the session’s profile

The checks compare the session with the profile it is used with, so connect exactly as the session normally runs: the opentele2 TelegramClient with its api= in Python (tdata to Telethon), an mtcute client with initConnectionOptions in Node (tdata to mtcute), a grammers client with connection_params in Rust (tdata to grammers, feature client).

Run the seven checks

They run in a fixed order and never stop at a failure:

CheckPasses when
get_confighelp.getConfig succeeds
nearest_dchelp.getNearestDc succeeds
current_sessionthe server marks the current session official_app
layer_matchthe library’s layer is within 15 of the advertised 230
lang_packlangpack.getLanguages accepts the session’s lang_pack; an empty pack passes without a request
app_updatehelp.getAppUpdate succeeds
terms_of_servicehelp.getTermsOfServiceUpdate succeeds
checks.py
import asyncio from opentele2.api import API from opentele2.tl import TelegramClient async def main(): client = TelegramClient("telethon.session", api=API.TelegramDesktop) await client.connect() report = await client.RunConsistencyChecks() # auto_warn=True warns when a check fails print(report.all_passed) print(report.summary) for check in report.checks: print(check.name, check.passed, check.detail) await client.disconnect() asyncio.run(main())

Read the report

The summary is the same text in every package: a count line, then one line per check.

output
Consistency: 7/7 checks passed [OK] get_config: ... [OK] nearest_dc: ... [OK] current_session: ...
Note:

current_session fails for any session whose api_id is not official: the server reports such sessions without official_app. With grammers, lang_pack asks about the profile’s pack while grammers 0.11 sends an empty one. A failing check is a signal to look at the profile, not an error of the library.

List the sessions

account.getAuthorizations returns every session of the account; the current one is flagged current.

list.py
# client: a connected opentele2 TelegramClient, inside a coroutine sessions = await client.GetSessions() for auth in sessions.authorizations: print(auth.hash, auth.current, auth.device_model, auth.app_name, auth.official_app) current = await client.GetCurrentSession() print(await client.is_official_app())
print.py
await client.PrintSessions() # fetches the sessions, or pass the GetSessions() result
output
|---------+---------+----------+----------------+--------+----------------------------+--------------| | | Device | Platform | System | API_ID | App name | Official App | |---------+---------+----------+----------------+--------+----------------------------+--------------| | Current | Desktop | Windows | Windows 11 x64 | 2040 | Telegram Desktop 7.3.1 x64 | ✔ | |---------+---------+----------+----------------+--------+----------------------------+--------------|

Terminate sessions

End one session by its hash, or every session except the current one. The examples end every unofficial session first, then show the call that ends all the others.

terminate.py
# client: a connected opentele2 TelegramClient, inside a coroutine sessions = await client.GetSessions() old = [auth for auth in sessions.authorizations if not auth.current and not auth.official_app] for auth in old: await client.TerminateSession(auth.hash) # Or end every session but the current one: # await client.TerminateAllSessions()

TerminateSession raises Telethon’s FreshResetAuthorisationForbiddenError or HashInvalidError with opentele2’s message.

Warning:

A session that logged in less than 24 hours ago cannot end other sessions: the server answers FRESH_RESET_AUTHORISATION_FORBIDDEN. A session just created by QR login has to wait before it cleans up.

Next

Last updated on