TelegramClient¶
class TelegramClient¶
@extend_class
class TelegramClient(telethon.TelegramClient, BaseObject)
Extended version of telethon.TelegramClient
Methods:
- FromTDesktop():
Create an instance of TelegramClient from TDesktop. - ToTDesktop():
Convert this TelegramClient instance to TDesktop. - QRLoginToNewClient():
Return True if logged-in using an official API. - GetSessions():
Get all logged in sessions. - GetCurrentSession():
Get current logged-in session. - TerminateSession():
Terminate a specific session. - TerminateAllSessions():
Terminate all other sessions. - PrintSessions():
Pretty-print all logged-in sessions. - is_official_app():
Return True if logged-in using an official API.
TelegramClient()¶
@typing.overload
def __init__(session: Union[str, Session] = None, api: Union[Type[APIData], APIData] = API.TelegramDesktop)
Start TelegramClient with customized api.
Read more at opentele GitHub
Arguments:
Name | Type | Default | Description |
---|---|---|---|
session | str, Session | 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 method .log_out() when you're done. Read more here. |
|
api | API | TelegramDesktop | Which API to use. Read more here. |
GetSessions()¶
async def GetSessions() -> Optional[types.account.Authorizations]
Get all logged-in sessions.
Returns:
- Return an instance of Authorizations on success
GetCurrentSession()¶
async def GetCurrentSession() -> Optional[types.Authorization]
Get current logged-in session.
Returns:
Return telethon.types.Authorization on success.
Return None on failure.
TerminateSession()¶
async def TerminateSession(hash: int)
Terminate a specific session
Arguments:
Name | Type | Description |
---|---|---|
hash | int | The session's hash to terminate |
Raises:
FreshResetAuthorisationForbiddenError : You can't log out other sessions if less than 24 hours have passed since you logged on to the current session. HashInvalidError : The provided hash is invalid.
TerminateAllSessions()¶
async def TerminateAllSessions() -> bool
Terminate all other sessions.
PrintSessions()¶
async def PrintSessions(sessions: types.account.Authorizations = None)
Pretty-print all logged-in sessions.
Arguments:
Name | Type | Default | Description |
---|---|---|---|
sessions | Authorizations | None | Sessions that return by GetSessions(), if None then it will GetSessions() first. |
Returns:
On success, it should prints the sessions table as the code below.
|---------+-----------------------------+----------+----------------+--------+----------------------------+--------------|
| | Device | Platform | System | API_ID | App name | Official App |
|---------+-----------------------------+----------+----------------+--------+----------------------------+--------------|
| Current | MacBook Pro | macOS | 10.15.6 | 2834 | Telegram macOS 8.4 | ✔ |
|---------+-----------------------------+----------+----------------+--------+----------------------------+--------------|
| 1 | Chrome 96 | Windows | | 2496 | Telegram Web 1.28.3 Z | ✔ |
| 2 | iMac | macOS | 11.3.1 | 2834 | Telegram macOS 8.4 | ✔ |
| 3 | MacBook Pro | macOS | 10.12 | 2834 | Telegram macOS 8.4 | ✔ |
| 4 | Huawei Y360-U93 | Android | 7.1 N MR1 (25) | 21724 | Telegram Android X 8.4.1 | ✔ |
| 5 | Samsung Galaxy Spica | Android | 6.0 M (23) | 6 | Telegram Android 8.4.1 | ✔ |
| 6 | Xiaomi Redmi Note 8 | Android | 10 Q (29) | 6 | Telegram Android 8.4.1 | ✔ |
| 7 | Samsung Galaxy Tab A (2017) | Android | 7.0 N (24) | 6 | Telegram Android 8.4.1 | ✔ |
| 8 | Samsung Galaxy XCover Pro | Android | 8.0 O (26) | 6 | Telegram Android 8.4.1 | ✔ |
| 9 | iPhone X | iOS | 13.1.3 | 10840 | Telegram iOS 8.4 | ✔ |
| 10 | iPhone XS Max | iOS | 12.11.0 | 10840 | Telegram iOS 8.4 | ✔ |
| 11 | iPhone 11 Pro Max | iOS | 14.4.2 | 10840 | Telegram iOS 8.4 | ✔ |
|---------+-----------------------------+----------+----------------+--------+----------------------------+--------------|
is_official_app()¶
async def is_official_app() -> bool
Return True if this session was logged-in using an official app (API).
QRLoginToNewClient()¶
@typing.overload
async def QRLoginToNewClient(session: Union[str, Session] = None, api: Union[Type[APIData], APIData] = API.TelegramDesktop, password: str = None) -> TelegramClient
Create a new session using the current session.
Arguments:
Name | Type | Default | Description |
---|---|---|---|
session | str, Session | None | description |
api | API | TelegramDesktop | Which API to use. Read more here. |
password | str | None | Two-step verification password, set if needed. |
Raises:
-
NoPasswordProvided : The account's two-step verification is enabled and no password was provided. Please set the password parameters.
-
PasswordIncorrect : The two-step verification password is incorrect.
-
TimeoutError : Time out waiting for the client to be authorized.
Returns:
- Return an instance of TelegramClient on success.
Examples:
Use to current session to authorize a new session:
# Using the API that we've generated before. Please refer to method API.Generate() to learn more.
oldAPI = API.TelegramDesktop.Generate(system="windows", unique_id="old.session")
oldclient = TelegramClient("old.session", api=oldAPI)
await oldClient.connect()
# We can safely authorize the new client with a different API.
newAPI = API.TelegramAndroid.Generate(unique_id="new.session")
newClient = await client.QRLoginToNewClient(session="new.session", api=newAPI)
await newClient.connect()
await newClient.PrintSessions()
ToTDesktop()¶
async def ToTDesktop(flag: Type[LoginFlag] = CreateNewSession, api: Union[Type[APIData], APIData] = API.TelegramDesktop, password: str = None) -> td.TDesktop
Convert this instance of TelegramClient to TDesktop
Arguments:
Name | Type | Default | Description |
---|---|---|---|
flag | LoginFlag | CreateNewSession | The login flag. Read more here. |
api | API | TelegramDesktop | Which API to use. Read more here. |
password | str | None | Two-step verification password if needed. |
Returns:
- Return an instance of TDesktop on success
Examples:
Save a telethon session to tdata:
# Using the API that we've generated before. Please refer to method API.Generate() to learn more.
oldAPI = API.TelegramDesktop.Generate(system="windows", unique_id="old.session")
oldclient = TelegramClient("old.session", api=oldAPI)
await oldClient.connect()
# We can safely CreateNewSession with a different API.
# Be aware that you should not use UseCurrentSession with a different API than the one that first authorized it.
newAPI = API.TelegramAndroid.Generate(unique_id="new_tdata")
tdesk = await oldClient.ToTDesktop(flag=CreateNewSession, api=newAPI)
# Save the new session to a folder named "new_tdata"
tdesk.SaveTData("new_tdata")
FromTDesktop()¶
@typing.overload
@staticmethod
async def FromTDesktop(account: Union[td.TDesktop, td.Account], session: Union[str, Session] = None, flag: Type[LoginFlag] = CreateNewSession, api: Union[Type[APIData], APIData] = API.TelegramDesktop, password: str = None) -> TelegramClient
Arguments:
Name | Type | Default | Description |
---|---|---|---|
account | TDesktop, Account | The TDesktop or Account you want to convert from. | |
session | str, Session | None | The file name of the session file to be used, if None then the session will not be saved. Read more here. |
flag | LoginFlag | CreateNewSession | The login flag. Read more here. |
api | API | TelegramDesktop | Which API to use. Read more here. |
password | str | None | Two-step verification password if needed. |
Returns:
- Return an instance of TelegramClient on success
Examples:
Create a telethon session using tdata folder:
# Using the API that we've generated before. Please refer to method API.Generate() to learn more.
oldAPI = API.TelegramDesktop.Generate(system="windows", unique_id="old_tdata")
tdesk = TDesktop("old_tdata", api=oldAPI)
# We can safely authorize the new client with a different API.
newAPI = API.TelegramAndroid.Generate(unique_id="new.session")
client = await TelegramClient.FromTDesktop(tdesk, session="new.session", flag=CreateNewSession, api=newAPI)
await client.connect()
await client.PrintSessions()