### Preparing Account for Start (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telegram-desktop/account.md This method prepares the account for initialization and subsequent use. It requires a `localKey` of type `td.AuthKey` for encryption and returns a `td.MTP.Config` object, which contains configuration details necessary for starting the account's operations. ```Python def prepareToStart(localKey: td.AuthKey) -> td.MTP.Config ``` -------------------------------- ### Preparing Account for Start in Python Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telegram-desktop/account.md This method prepares the account before it can be started. It requires a `localKey` of type `td.AuthKey` for authorization and returns a `td.MTP.Config` object, which contains configuration details for the MTP protocol. ```python def prepareToStart(localKey: td.AuthKey) -> td.MTP.Config ``` -------------------------------- ### Example: Creating TelegramClient with Generated APIData - Python Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/authorization/api.md This example demonstrates how to use the APIData.Generate method to obtain an APIData object with a unique ID, ensuring consistent device data. This generated API configuration is then passed to the TelegramClient constructor, allowing the client to start with a specific session and device profile. ```python api = API.TelegramIOS.Generate(unique_id="new.session") client = TelegramClient(session="new.session" api=api) client.start() ``` -------------------------------- ### Example: Adding and Saving TDesktop Account in Python Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telegram-desktop/account.md This example demonstrates how to add an existing Telethon client account to a `TDesktop` instance and then save the `TDesktop` data to a folder named 'new_tdata'. It illustrates the integration of Telethon with OpenTele's TDesktop for account management. ```python telethonClient = TelegramClient("sessionFile", API_ID, API_HASH) td = TDesktop("new_tdata") account = Account.FromTelethon(telethonClient, owner=td) # add this account to td td.SaveTData() ``` -------------------------------- ### Complete Example: TDesktop to Telethon Conversion (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/examples/convert-tdata-to-telethon.md This comprehensive example demonstrates the full workflow of converting a `tdata` folder session to a `Telethon` client. It includes importing necessary modules, loading the `TDesktop` client, converting it to `Telethon` using the current session, connecting the client, and printing all associated sessions. This snippet provides a runnable example of the entire process. ```python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession import asyncio async def main(): # Load TDesktop client from tdata folder tdataFolder = r"C:\Users\\AppData\Roaming\Telegram Desktop\tdata" tdesk = TDesktop(tdataFolder) # Check if we have loaded any accounts assert tdesk.isLoaded() # flag=UseCurrentSession # # Convert TDesktop to Telethon using the current session. client = await tdesk.ToTelethon(session="telethon.session", flag=UseCurrentSession) # Connect and print all logged-in sessions of this client. # Telethon will save the session to telethon.session on creation. await client.connect() await client.PrintSessions() asyncio.run(main()) ``` -------------------------------- ### Complete Example: Initializing TelegramClient with Randomized API (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/examples/using-official-apis.md This complete example demonstrates the full flow of initializing a TelegramClient using a randomized Telegram Android API. It imports necessary modules, defines an asynchronous main function, generates a randomized API, creates the client, and connects to Telegram. ```python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession import asyncio async def main(): # Randomize the device data api = API.TelegramAndroid.Generate() client = TelegramClient("telethon.session", api=api) await client.connect() asyncio.run(main()) ``` -------------------------------- ### Example: Creating TelegramClient from TDesktop TData (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telethon/telegramclient.md This example demonstrates how to initialize a `TelegramClient` session from an existing tdata folder using the `FromTDesktop` method. It illustrates generating an API for the tdata, converting it to a `TelegramClient` with a new session file, connecting to Telegram, and printing active sessions. This allows seamless integration of Telegram Desktop sessions into Telethon applications. ```python # 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() ``` -------------------------------- ### Complete Example: Initializing TelegramClient with Random API (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/examples/using-official-apis.md This complete example demonstrates the full flow of initializing a TelegramClient using a randomized Telegram Android API. It imports necessary modules, defines an asynchronous main function, generates a unique API, and connects the client to Telegram. ```python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession import asyncio async def main(): # Randomize the device data api = API.TelegramAndroid.Generate() client = TelegramClient("telethon.session", api=api) await client.connect() asyncio.run(main()) ``` -------------------------------- ### Installing opentele with pip Source: https://github.com/thedemons/opentele/blob/main/docs-github/README.md This snippet demonstrates how to install or upgrade the opentele library using pip, the Python package installer. It ensures you have the latest version of the library available for use in your projects, which is a prerequisite for using its functionalities. ```pip pip install --upgrade opentele ``` -------------------------------- ### Installing opentele Python Library Source: https://github.com/thedemons/opentele/blob/main/docs/README.md This snippet provides the command to install or upgrade the 'opentele' library using pip, the Python package installer. It ensures that you have the latest version of the library available for use in your projects. ```pip pip install --upgrade opentele ``` -------------------------------- ### Complete Example: Connecting TelegramClient with Random API (Python) Source: https://github.com/thedemons/opentele/blob/main/examples/using-official-apis.md This complete example combines the previous steps, demonstrating how to import necessary modules, define an asynchronous main function, generate a randomized API instance, create a TelegramClient from a session file using that API, and connect to Telegram. It provides a runnable boilerplate for basic usage. ```python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession import asyncio async def main(): # Randomize the device data api = API.TelegramAndroid.Generate() client = TelegramClient("telethon.session", api=api) await client.connect() asyncio.run(main()) ``` -------------------------------- ### Creating TelegramClient with Generated APIData in Python Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/authorization/api.md This example demonstrates how to use the `Generate` method to obtain an `APIData` object with a unique ID, then use this API configuration to initialize a `TelegramClient`. The `unique_id` ensures that the generated device data is consistent for the 'new.session'. Finally, the client is started, establishing a connection to Telegram. ```python api = API.TelegramIOS.Generate(unique_id="new.session") client = TelegramClient(session="new.session" api=api) client.start() ``` -------------------------------- ### Initializing the Account Class in Python Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telegram-desktop/account.md This constructor initializes an `Account` object, linking it to a `TDesktop` client and configuring its base path, API, key file, and index. While direct instantiation is possible, it's generally recommended to use `TDesktop()` or `TDesktop.FromTelethon()` for proper setup. The `prepareToStart()` method must be called after initialization. ```python def __init__(owner: td.TDesktop, basePath: str = None, api: Union[Type[APIData], APIData] = API.TelegramDesktop, keyFile: str = None, index: int = 0) -> None ``` -------------------------------- ### Complete Example: TDesktop to Telethon Conversion (Python) Source: https://github.com/thedemons/opentele/blob/main/examples/convert-tdata-to-telethon.md This comprehensive example demonstrates the full workflow of converting a `tdata` folder to a `Telethon` client. It includes importing necessary modules, loading the `TDesktop` client, converting it using the current session, connecting to Telegram, and printing active sessions. ```Python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession import asyncio async def main(): # Load TDesktop client from tdata folder tdataFolder = r"C:\\Users\\\\AppData\\Roaming\\Telegram Desktop\\tdata" tdesk = TDesktop(tdataFolder) # Check if we have loaded any accounts assert tdesk.isLoaded() # flag=UseCurrentSession # # Convert TDesktop to Telethon using the current session. client = await tdesk.ToTelethon(session="telethon.session", flag=UseCurrentSession) # Connect and print all logged-in sessions of this client. # Telethon will save the session to telethon.session on creation. await client.connect() await client.PrintSessions() asyncio.run(main()) ``` -------------------------------- ### Installing opentele with pip Source: https://github.com/thedemons/opentele/blob/main/README.md This command installs or upgrades the opentele library using pip, the Python package installer. It ensures you have the latest version of the library available for use in your projects, which is a prerequisite for utilizing its features. ```pip pip install --upgrade opentele ``` -------------------------------- ### Creating TelegramClient from TDesktop Folder - Python Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telethon/telegramclient.md This example demonstrates how to convert a Telegram Desktop (tdata) folder into a `TelegramClient` instance using the `FromTDesktop` method. It shows how to generate different API configurations for the old TDesktop and the new client, connect the client, and print its active sessions. This allows for seamless migration and continued use of a Telegram account with a new client configuration. ```python # 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() ``` -------------------------------- ### TDesktop Client Session Management Example (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telegram-desktop/tdesktop.md This example demonstrates how to migrate an existing Telethon session to a TDesktop session using TDesktop.FromTelethon with a new API, and then save the new TDesktop session to a specified folder. It highlights the use of CreateNewSession flag and API.Generate for different API configurations. ```python # 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 use 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("new_tdata") tdesk = await TDesktop.FromTelethon(oldclient, flag=CreateNewSession, api=newAPI) # Save the new session to a folder named "new_tdata" tdesk.SaveTData("new_tdata") ``` -------------------------------- ### Complete Telethon to TDesktop Conversion Example (Python) Source: https://github.com/thedemons/opentele/blob/main/examples/convert-telethon-to-tdata.md This comprehensive snippet combines all previous steps into a single executable example. It demonstrates importing necessary modules, loading a `TelegramClient` session, converting it to a `TDesktop` object using the current session, and finally saving the `TDesktop` session to a 'tdata' folder for use with Telegram Desktop. ```Python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession import asyncio async def main(): # Load the client from telethon.session file # We don't need to specify api, api_id or api_hash, it will use TelegramDesktop API by default. client = TelegramClient("telethon.session") # flag=UseCurrentSession # # Convert Telethon to TDesktop using the current session. tdesk = await client.ToTDesktop(flag=UseCurrentSession) # Save the session to a folder named "tdata" tdesk.SaveTData("tdata") asyncio.run(main()) ``` -------------------------------- ### Complete Example: TDesktop to Telethon Conversion (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/examples/convert-tdata-to-telethon.md This comprehensive example demonstrates the full workflow of converting a `tdata` folder to a Telethon `TelegramClient`. It includes importing necessary modules, loading the `TDesktop` client, converting it using the current session, connecting to Telegram, and printing active sessions. ```python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession import asyncio async def main(): # Load TDesktop client from tdata folder tdataFolder = r"C:\Users\\AppData\Roaming\Telegram Desktop\tdata" tdesk = TDesktop(tdataFolder) # Check if we have loaded any accounts assert tdesk.isLoaded() # flag=UseCurrentSession # # Convert TDesktop to Telethon using the current session. client = await tdesk.ToTelethon(session="telethon.session", flag=UseCurrentSession) # Connect and print all logged-in sessions of this client. # Telethon will save the session to telethon.session on creation. await client.connect() await client.PrintSessions() asyncio.run(main()) ``` -------------------------------- ### Complete example for Telethon to TDesktop conversion - Python Source: https://github.com/thedemons/opentele/blob/main/docs/examples/convert-telethon-to-tdata.md This comprehensive example combines all previous steps into a single executable script. It imports necessary modules, loads a `TelegramClient` session, converts it to a `TDesktop` object using the current session, and then saves the `TDesktop` session to a 'tdata' folder. This script provides a full, runnable demonstration of the conversion process. ```python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession import asyncio async def main(): # Load the client from telethon.session file # We don't need to specify api, api_id or api_hash, it will use TelegramDesktop API by default. client = TelegramClient("telethon.session") # flag=UseCurrentSession # # Convert Telethon to TDesktop using the current session. tdesk = await client.ToTDesktop(flag=UseCurrentSession) # Save the session to a folder named "tdata" tdesk.SaveTData("tdata") asyncio.run(main()) ``` -------------------------------- ### Example: Converting TelegramClient to TDesktop and Saving TData (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telethon/telegramclient.md This example demonstrates converting an existing `TelegramClient` session to a `TDesktop` session and saving it as tdata. It showcases how to use different API configurations for the source and target sessions, emphasizing the `CreateNewSession` flag for safe migration. The resulting `TDesktop` object is then saved to a specified folder. ```python # 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") ``` -------------------------------- ### Complete Example: Convert Telethon to TDesktop and Save (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/examples/convert-telethon-to-tdata.md This comprehensive example demonstrates the full workflow of converting a Telethon `TelegramClient` session to a Telegram Desktop `tdata` folder. It includes importing necessary modules, loading an existing Telethon session, converting it to a `TDesktop` object using the `UseCurrentSession` flag, and finally saving the `TDesktop` session to a 'tdata' directory, all within an asynchronous execution context. ```python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession import asyncio async def main(): # Load the client from telethon.session file # We don't need to specify api, api_id or api_hash, it will use TelegramDesktop API by default. client = TelegramClient("telethon.session") # flag=UseCurrentSession # # Convert Telethon to TDesktop using the current session. tdesk = await client.ToTDesktop(flag=UseCurrentSession) # Save the session to a folder named "tdata" tdesk.SaveTData("tdata") asyncio.run(main()) ``` -------------------------------- ### Converting TDesktop Session to Telethon Client in Python Source: https://github.com/thedemons/opentele/blob/main/docs/README.md This Python example demonstrates how to load a Telegram Desktop (tdata) session, convert it into a Telethon client, and then connect to Telegram. It utilizes an official iOS API with randomized device info and shows how to authorize a new client using an existing session via 'Login via QR code', followed by printing all logged-in devices. ```Python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, CreateNewSession, UseCurrentSession import asyncio async def main(): # Load TDesktop client from tdata folder tdataFolder = r"C:\Users\\AppData\Roaming\Telegram Desktop\tdata" tdesk = TDesktop(tdataFolder) # Using official iOS API with randomly generated device info # print(api) to see more api = API.TelegramIOS.Generate() # Convert TDesktop session to telethon client # CreateNewSession flag will use the current existing session to # authorize the new client by `Login via QR code`. client = await tdesk.ToTelethon("newSession.session", CreateNewSession, api) # Although Telegram Desktop doesn't let you authorize other # sessions via QR Code (or it doesn't have that feature), # it is still available across all platforms (APIs). # Connect and print all logged in devices await client.connect() await client.PrintSessions() asyncio.run(main()) ``` -------------------------------- ### Saving TDesktop Account Data in Python Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telegram-desktop/account.md This example demonstrates how to add a `TelethonClient` account to a `TDesktop` instance and then save the `TDesktop` data to a folder named 'new_tdata'. It shows the typical workflow for integrating `Telethon` with `TDesktop` and persisting the account information. ```python telethonClient = TelegramClient("sessionFile", API_ID, API_HASH) td = TDesktop("new_tdata") account = Account.FromTelethon(telethonClient, owner=td) # add this account to td td.SaveTData() ``` -------------------------------- ### Example: Saving Telethon Session to TData with Generated API in Python Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/authorization/api.md This example demonstrates how to use the `Generate` method to create `APIData` for Telethon sessions. It shows how to generate an `oldAPI` for a Windows session with a unique ID, connect a client, then generate a `newAPI` for a macOS session, and finally convert the old client to a new TDesktop session and save it to a folder. ```Python # unique_id will ensure that this data will always be the same (per unique_id). # You can use the session file name, or user_id as a unique_id. # If unique_id isn't specify, the device data will be randomized each time we runs it. 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. # You can print(newAPI) to see what it had generated. newAPI = API.TelegramDesktop.Generate("macos", "new_tdata") tdesk = oldclient.ToTDesktop(oldclient, flag=CreateNewSession, api=newAPI) # Save the new session to a folder named "new_tdata" tdesk.SaveTData("new_tdata") ``` -------------------------------- ### Converting TDesktop to Telethon Client (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telegram-desktop/tdesktop.md This example illustrates how to convert an existing TDesktop session into a Telethon `TelegramClient` instance. It shows initializing a `TDesktop` object from a TData folder and then using the `ToTelethon` method to create a new `TelegramClient` with a specified session file, login flag, and API configuration. The resulting client is then connected and its sessions are printed for verification. ```Python # 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 tdesk.ToTelethon(session="new.session", flag=CreateNewSession, api=newAPI) await client.connect() await client.PrintSessions() ``` -------------------------------- ### Importing Dependencies and Defining Async Main Function (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/examples/convert-tdata-to-telethon.md This snippet imports necessary classes from `opentele` for TDesktop and Telethon integration, along with `asyncio` for asynchronous operations. It also sets up the basic asynchronous `main` function structure required for running the examples. ```python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession, CreateNewSession import asyncio ``` ```python async def main(): # PUT EXAMPLE CODE HERE asyncio.run(main()) ``` -------------------------------- ### Defining Main Asynchronous Function (Python) Source: https://github.com/thedemons/opentele/blob/main/examples/convert-telethon-to-tdata.md This snippet defines the basic asynchronous structure for the application, `async def main():`, and then runs it using `asyncio.run(main())`. All subsequent example code will be placed inside this `main` function to ensure proper asynchronous execution. ```Python async def main(): # PUT EXAMPLE CODE HERE asyncio.run(main()) ``` -------------------------------- ### Retrieving Application Version in TDesktop (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telegram-desktop/tdesktop.md This property returns the application version of the TDesktop client as an optional integer. It allows programmatic retrieval of the client's installed version. ```Python @property def AppVersion() -> Optional[int] ``` -------------------------------- ### Creating TDesktop from Telethon Client (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telegram-desktop/tdesktop.md This snippet demonstrates how to create a `TDesktop` instance directly from an active Telethon `TelegramClient`. It highlights the use of the static method `FromTelethon`, passing the existing `TelegramClient` along with desired login flags and API configuration. This conversion is a prerequisite for operations like saving the session in TDesktop format, as shown in the `SaveTData` example. ```Python # Assume oldclient is an existing and connected TelegramClient instance # oldclient = TelegramClient("old.session", api=oldAPI) # await oldclient.connect() # Define the new API configuration for the TDesktop session newAPI = API.TelegramAndroid.Generate("new_tdata") # Create a TDesktop instance from the Telethon client tdesk = await TDesktop.FromTelethon(oldclient, flag=CreateNewSession, api=newAPI) # The 'tdesk' object can now be used, e.g., to save the session # tdesk.SaveTData("new_tdata") ``` -------------------------------- ### Converting Telethon Client to TDesktop Session in Python Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telegram-desktop/tdesktop.md This method creates a TDesktop instance from an existing TelethonClient. It allows specifying a login flag (e.g., CreateNewSession), the API to use (e.g., API.TelegramDesktop), and a two-step verification password if required. The example demonstrates converting a Telethon session to a new TDesktop session with a different API and saving it. ```Python @staticmethod async def FromTelethon(telethonClient: tl.TelegramClient, flag: Type[LoginFlag] = CreateNewSession, api: Union[Type[APIData], APIData] = API.TelegramDesktop, password: str = None) -> TDesktop ``` ```Python # 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("new_tdata") tdesk = await TDesktop.FromTelethon(oldclient, flag=CreateNewSession, api=newAPI) # Save the new session to a folder named "new_tdata" tdesk.SaveTData("new_tdata") ``` -------------------------------- ### Converting TData to Telethon Session in Python Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telegram-desktop/tdesktop.md This example illustrates how to convert a `tdata` folder into a Telethon `TelegramClient` session. It involves initializing a `TDesktop` instance from an existing `tdata` folder, then using `ToTelethon` to create a new `TelegramClient` session with a different API, connecting to it, and printing its sessions. ```Python # 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 tdesk.ToTelethon(session="new.session", flag=CreateNewSession, api=newAPI) await client.connect() await client.PrintSessions() ``` -------------------------------- ### Saving Telethon Session to TData with Generated API Data in Python Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/authorization/api.md This example demonstrates how to generate Telegram Desktop device data using `API.TelegramDesktop.Generate` and integrate it with a Telethon client. It shows how to create a new session with specific device data and then save it to a TData folder, ensuring consistent device parameters for the session. ```python # unique_id will ensure that this data will always be the same (per unique_id). # You can use the session file name, or user_id as a unique_id. # If unique_id isn't specify, the device data will be randomized each time we runs it. 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. # You can print(newAPI) to see what it had generated. newAPI = API.TelegramDesktop.Generate("macos", "new_tdata") tdesk = oldclient.ToTDesktop(oldclient, flag=CreateNewSession, api=newAPI) # Save the new session to a folder named "new_tdata" tdesk.SaveTData("new_tdata") ``` -------------------------------- ### Initializing Account Object in Python Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telegram-desktop/account.md This is the constructor for the `Account` class. It initializes an `Account` object with its `TDesktop` owner, an optional base path for tdata, the API configuration to use, a key file, and an index. Direct instantiation is not recommended; instead, use `TDesktop()` or `TDesktop.FromTelethon()`. ```python def __init__(owner: td.TDesktop, basePath: str = None, api: Union[Type[APIData], APIData] = API.TelegramDesktop, keyFile: str = None, index: int = 0) -> None ``` -------------------------------- ### Saving Telethon Session to TData in Python Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telegram-desktop/tdesktop.md This example demonstrates how to convert an existing Telethon `TelegramClient` session into a `tdata` folder format and save it. It involves generating different APIs for old and new sessions, connecting the old client, converting it to a `TDesktop` instance using `FromTelethon` with the `CreateNewSession` flag, and finally saving the `TDesktop` instance to a new `tdata` folder. ```Python # 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("new_tdata") tdesk = await TDesktop.FromTelethon(oldclient, flag=CreateNewSession, api=newAPI) # Save the new session to a folder named "new_tdata" tdesk.SaveTData("new_tdata") ``` -------------------------------- ### Initializing TDesktop Client (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telegram-desktop/tdesktop.md Initializes a TDesktop client instance. It allows specifying the path to the tdata folder, the Telegram API to use, an optional passcode for encrypted tdata, and a key file. If the tdata path is invalid or corrupted, a new instance will be created. ```python def __init__(basePath: str = None, api: Union[Type[APIData], APIData] = API.TelegramDesktop, passcode: str = None, keyFile: str = None) -> None ``` -------------------------------- ### Initializing APIData with Custom Parameters in Python Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/authorization/api.md This snippet shows the overloaded constructor for the `APIData` class, allowing users to create a customized API configuration. It requires `api_id` and `api_hash` as mandatory parameters, with other device and language-related attributes being optional. A warning is provided regarding the risk of account bans if incorrect API configurations are used. ```python @typing.overload def __init__(api_id: int, api_hash: str, device_model: str = None, system_version: str = None, app_version: str = None, lang_code: str = None, system_lang_code: str = None, lang_pack: str = None) -> None ``` -------------------------------- ### Loading TDesktop Client from tdata Folder (Python) Source: https://github.com/thedemons/opentele/blob/main/examples/convert-tdata-to-telethon.md This code initializes a `TDesktop` object by loading the specified `tdata` folder. It then asserts that the `TDesktop` object has successfully loaded at least one account, which is a prerequisite for further operations. ```Python # Load TDesktop client from tdata folder tdataFolder = r"C:\\Users\\\\AppData\\Roaming\\Telegram Desktop\\tdata" tdesk = TDesktop(tdataFolder) # Check if we have loaded any accounts assert tdesk.isLoaded() ``` -------------------------------- ### Creating TelegramClient from TDesktop Folder (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/examples/using-official-apis.md This snippet demonstrates creating a TelegramClient from a Telegram Desktop tdata folder. It uses TDesktop to load the folder, asserts its validity, and then initializes the TelegramClient using FromTDesktop with the UseCurrentSession flag and the specified API. ```python tdataFolder = r"C:\Users\\AppData\Roaming\Telegram Desktop\tdata" tdesk = TDesktop(tdataFolder) assert tdesk.isLoaded() client = TelegramClient.FromTDesktop(tdesk, session="telethon.session", flag=UseCurrentSession, api=api) await client.connect() ``` -------------------------------- ### Loading TDesktop Client from tdata Folder (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/examples/convert-tdata-to-telethon.md This code initializes a `TDesktop` object by loading the specified `tdata` folder. It then asserts that the `TDesktop` object has successfully loaded at least one account, which is a prerequisite for further operations. ```python # Load TDesktop client from tdata folder tdataFolder = r"C:\Users\\AppData\Roaming\Telegram Desktop\tdata" tdesk = TDesktop(tdataFolder) # Check if we have loaded any accounts assert tdesk.isLoaded() ``` -------------------------------- ### Retrieving Key File Path for TDesktop (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telegram-desktop/account.md This property provides the path to the key file associated with the account, referencing the `TDesktop.keyFile` for more details on its purpose and usage. ```Python @property def keyFile() -> str ``` -------------------------------- ### Initializing TDesktop Client in Python Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telegram-desktop/tdesktop.md This method initializes a TDesktop client instance. It allows specifying the basePath to the tdata folder, the API to use (defaulting to API.TelegramDesktop), a passcode for tdata encryption, and a keyFile. If basePath is invalid or corrupted, a new instance will be created. ```python def __init__(basePath: str = None, api: Union[Type[APIData], APIData] = API.TelegramDesktop, passcode: str = None, keyFile: str = None) -> None ``` -------------------------------- ### Loading TDesktop Client from tdata Folder (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/examples/convert-tdata-to-telethon.md This code initializes a `TDesktop` object by loading the specified `tdata` folder. It then asserts that the `TDesktop` object has successfully loaded at least one account, which is a prerequisite for further operations. ```python # Load TDesktop client from tdata folder tdataFolder = r"C:\Users\\AppData\Roaming\Telegram Desktop\tdata" tdesk = TDesktop(tdataFolder) # Check if we have loaded any accounts assert tdesk.isLoaded() ``` -------------------------------- ### Initializing TelegramClient with Custom API in Python Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telethon/telegramclient.md This method initializes a `TelegramClient` instance, allowing customization of the session file and the Telegram API to be used. The `session` parameter can be a file name or a `Session` object, while the `api` parameter specifies which Telegram API (e.g., Telegram Desktop) to connect to. ```python @typing.overload def __init__(session: Union[str, Session] = None, api: Union[Type[APIData], APIData] = API.TelegramDesktop) ``` -------------------------------- ### Getting Account User ID (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telegram-desktop/account.md This property returns the unique integer identifier for the user associated with this account. It is essential for identifying the account within the Telegram system. ```Python @property def UserId() -> int ``` -------------------------------- ### Getting Account Count in TDesktop (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telegram-desktop/tdesktop.md This property returns the total number of accounts associated with the TDesktop client. It provides an integer indicating how many accounts are managed by this client instance. ```Python @property def accountsCount() -> int ``` -------------------------------- ### Getting Base Path for TData Storage (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telegram-desktop/account.md This property retrieves the file system path where the tdata for the account is stored. It provides a string representing the base directory for account-specific data. ```Python @property def basePath() -> str ``` -------------------------------- ### Creating TelegramClient from Existing Session (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/examples/using-official-apis.md This snippet shows how to initialize a TelegramClient instance from an existing session file. It connects to the Telegram servers using the specified session and the previously configured API object. ```python client = TelegramClient("telethon.session", api=api) await client.connect() ``` -------------------------------- ### Getting Local Encryption Key in TDesktop (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telegram-desktop/tdesktop.md This property retrieves the local encryption/decryption key used by the TDesktop client. It returns an optional `td.AuthKey` object, which is essential for handling encrypted data within the client. ```Python @property def localKey() -> Optional[td.AuthKey] ``` -------------------------------- ### Creating API Instance with Default Template (Python) Source: https://github.com/thedemons/opentele/blob/main/examples/using-official-apis.md This snippet demonstrates how to create an API instance using a built-in template, specifically API.TelegramAndroid. This provides a predefined set of API credentials and device data for connecting to Telegram. ```python api = API.TelegramAndroid ``` -------------------------------- ### Get Current Session - Telethon Client (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telethon/telegramclient.md Retrieves the current logged-in session. This function returns a `telethon.types.Authorization` object on success, providing details about the active session. If no session is found or an error occurs, it returns `None`. ```Python async def GetCurrentSession() -> Optional[types.Authorization] ``` -------------------------------- ### Initializing APIData with Custom Parameters - Python Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/authorization/api.md This __init__ method allows for the creation of a customized APIData instance by providing specific API credentials and device details. It accepts api_id and api_hash as mandatory parameters, along with optional fields for device, system, app versions, and language codes. A critical warning advises against mixing API types to prevent account bans. ```python @typing.overload def __init__(api_id: int, api_hash: str, device_model: str = None, system_version: str = None, app_version: str = None, lang_code: str = None, system_lang_code: str = None, lang_pack: str = None) -> None ``` -------------------------------- ### Handling Invalid TData Magic Errors in OpenTele Python Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/exceptions/README.md This exception indicates that a TData file's initial 4-byte 'magic' signature is incorrect, suggesting the file is either corrupted or not a valid TData format. This check is crucial for verifying file integrity at the start of processing. ```Python class TDataInvalidMagic(OpenTeleException) ``` -------------------------------- ### Displaying Source Repository Link Source: https://github.com/thedemons/opentele/blob/main/theme/partials/header.html This snippet conditionally includes a partial for displaying a link to the project's source repository, only if `config.repo_url` is defined in the MkDocs configuration. ```Jinja2 {% if config.repo_url %} {% include "partials/source.html" %} {% endif %} ``` -------------------------------- ### Creating TelegramClient from TDesktop Folder (Python) Source: https://github.com/thedemons/opentele/blob/main/examples/using-official-apis.md This snippet demonstrates creating a TelegramClient from a Telegram Desktop tdata folder. It loads the tdata using TDesktop, asserts its validity, and then initializes the TelegramClient using FromTDesktop, specifying a session name, UseCurrentSession flag, and the API object, followed by connecting to Telegram. ```python tdataFolder = r"C:\Users\\AppData\Roaming\Telegram Desktop\tdata" tdesk = TDesktop(tdataFolder) assert tdesk.isLoaded() client = TelegramClient.FromTDesktop(tdesk, session="telethon.session", flag=UseCurrentSession, api=api) await client.connect() ``` -------------------------------- ### Defining Asynchronous Main Function (Python) Source: https://github.com/thedemons/opentele/blob/main/examples/using-official-apis.md This snippet defines an asynchronous main function, which is the entry point for the application's asynchronous code. It also includes asyncio.run(main()) to execute the main function, as all Telethon and OpenTele operations are asynchronous. ```python async def main(): # PUT EXAMPLE CODE HERE asyncio.run(main()) ``` -------------------------------- ### Initializing TelegramClient with Custom API in Python Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telethon/telegramclient.md This `__init__` method allows initializing the `TelegramClient` with a custom session and API. The `session` parameter can be a file name or a `Session` instance, while the `api` parameter specifies which Telegram API to use, defaulting to `API.TelegramDesktop`. ```Python @typing.overload def __init__(session: Union[str, Session] = None, api: Union[Type[APIData], APIData] = API.TelegramDesktop) ``` -------------------------------- ### Creating TelegramClient from TData Folder (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/examples/using-official-apis.md This snippet demonstrates how to create a TelegramClient from a Telegram Desktop tdata folder. It uses TDesktop to load the session data and then initializes the TelegramClient using FromTDesktop, connecting to Telegram. ```python tdataFolder = r"C:\Users\\AppData\Roaming\Telegram Desktop\tdata" tdesk = TDesktop(tdataFolder) assert tdesk.isLoaded() client = TelegramClient.FromTDesktop(tdesk, session="telethon.session", flag=UseCurrentSession, api=api) await client.connect() ``` -------------------------------- ### Importing Dependencies and Defining Async Main Function (Python) Source: https://github.com/thedemons/opentele/blob/main/examples/convert-tdata-to-telethon.md This snippet imports necessary classes from `opentele` for TDesktop and TelegramClient operations, along with `asyncio` for asynchronous execution. It also sets up the basic `async def main():` structure required for running asynchronous code. ```Python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession, CreateNewSession import asyncio ``` ```Python async def main(): # PUT EXAMPLE CODE HERE asyncio.run(main()) ``` -------------------------------- ### Defining TelegramDesktop API Template Class (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/authorization/api.md This snippet defines the `TelegramDesktop` class, a specialized API template within `opentele` that inherits from `APIData`. It provides specific API credentials and device parameters (like `api_id`, `api_hash`, `device_model`, `system_version`, `app_version`, `lang_code`, `system_lang_code`, and `lang_pack`) to accurately emulate the official Telegram Desktop client. ```python class TelegramDesktop(APIData) ``` -------------------------------- ### Defining Main Asynchronous Function (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/examples/using-official-apis.md This code defines an asynchronous main function, which serves as the entry point for the application's asynchronous logic. It's executed using asyncio.run(main()), ensuring that all asynchronous operations within main are properly managed. ```python async def main(): # PUT EXAMPLE CODE HERE asyncio.run(main()) ``` -------------------------------- ### Defining FromTDesktop Method for TDesktop to TelegramClient Conversion (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/documentation/telethon/telegramclient.md The `FromTDesktop` static method facilitates the conversion of a `TDesktop` or `Account` instance back into a `TelegramClient` object. It supports specifying a session file name for persistence, a `LoginFlag`, an `APIData` configuration, and an optional two-step verification password. This function returns a `TelegramClient` instance upon successful conversion. ```python @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 ``` -------------------------------- ### Handling TDesktop Instances Without Accounts in Python Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/exceptions/README.md This exception indicates that a TDesktop instance exists but does not have an active account loaded. Operations requiring an authenticated account will fail if this condition is met. ```Python class TDesktopHasNoAccount(OpenTeleException) ``` -------------------------------- ### Authorizing New Telethon Client via QR Login (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/documentation/telethon/telegramclient.md This asynchronous method creates and authorizes a new `TelegramClient` session using the current session via QR code login. It allows specifying a new session name, a different API configuration, and a two-step verification password if required. This is useful for managing multiple client sessions or migrating sessions. ```Python @typing.overload async def QRLoginToNewClient(session: Union[str, Session] = None, api: Union[Type[APIData], APIData] = API.TelegramDesktop, password: str = None) -> TelegramClient ``` ```Python # 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() ``` -------------------------------- ### Defining Asynchronous Main Function (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/examples/convert-telethon-to-tdata.md This snippet defines the basic asynchronous structure for the application using `asyncio`. The `main` function serves as the entry point for the asynchronous code, and `asyncio.run(main())` is used to execute it, ensuring that all `await` calls within the `main` function are properly handled. ```python async def main(): # PUT EXAMPLE CODE HERE asyncio.run(main()) ``` -------------------------------- ### Listing All Built-in API Templates (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/examples/using-official-apis.md This snippet showcases the various built-in API templates available within the opentele.api.API class. These templates provide predefined configurations for different Telegram clients, such as Desktop, Android, iOS, and Web versions. ```python api = API.TelegramDesktop api = API.TelegramAndroid api = API.TelegramAndroidX api = API.TelegramIOS api = API.TelegramMacOS api = API.TelegramWeb_Z api = API.TelegramWeb_K api = API.Webogram ``` -------------------------------- ### Creating API with Default Telegram Android Template (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/examples/using-official-apis.md This snippet demonstrates how to initialize an API object using the default built-in template for Telegram Android. This provides a standard set of device data for API interactions. ```python api = API.TelegramAndroid ``` -------------------------------- ### Converting TDesktop to Telethon Session using opentele Source: https://github.com/thedemons/opentele/blob/main/docs-github/README.md This Python snippet demonstrates how to load a Telegram Desktop (tdata) session, convert it into a Telethon client, and connect to Telegram using an official iOS API. It highlights the use of `CreateNewSession` to authorize the new client via QR code and then prints all logged-in devices, showcasing the session conversion and management capabilities. ```python from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, CreateNewSession, UseCurrentSession import asyncio async def main(): # Load TDesktop client from tdata folder tdataFolder = r"C:\Users\\AppData\Roaming\Telegram Desktop\tdata" tdesk = TDesktop(tdataFolder) # Using official iOS API with randomly generated device info # print(api) to see more api = API.TelegramIOS.Generate() # Convert TDesktop session to telethon client # CreateNewSession flag will use the current existing session to # authorize the new client by `Login via QR code`. client = await tdesk.ToTelethon("newSession.session", CreateNewSession, api) # Although Telegram Desktop doesn't let you authorize other # sessions via QR Code (or it doesn't have that feature), # it is still available across all platforms (APIs). # Connect and print all logged in devices await client.connect() await client.PrintSessions() asyncio.run(main()) ``` -------------------------------- ### Displaying Site Logo and Name Link Source: https://github.com/thedemons/opentele/blob/main/theme/partials/header.html This snippet includes a logo partial and wraps it in an anchor tag that links to the `site_url`. The link's title attribute is set to the escaped `config.site_name` for accessibility. ```Jinja2 [{% include "partials/logo.html" %}]({{ site_url }} "{{ config.site_name | e }}") {% include ".icons/material/menu" ~ ".svg" %} ``` -------------------------------- ### Integrating Search Functionality Source: https://github.com/thedemons/opentele/blob/main/theme/partials/header.html This snippet conditionally includes search-related elements. If the 'search' plugin is enabled in the MkDocs configuration, it includes a magnify icon and the `search.html` partial for the search interface. ```Jinja2 {% if "search" in config["plugins"] %} {% include ".icons/material/magnify.svg" %} {% include "partials/search.html" %} {% endif %} ``` -------------------------------- ### Listing All Built-in API Templates (Python) Source: https://github.com/thedemons/opentele/blob/main/docs-github/examples/using-official-apis.md This snippet provides a list of all available built-in API templates within the OpenTele library. These templates represent different Telegram client types (e.g., Desktop, Android, iOS, Web) and can be used to configure the API object. ```python api = API.TelegramDesktop api = API.TelegramAndroid api = API.TelegramAndroidX api = API.TelegramIOS api = API.TelegramMacOS api = API.TelegramWeb_Z api = API.TelegramWeb_K api = API.Webogram ``` -------------------------------- ### Creating TelegramClient from Existing Session (Python) Source: https://github.com/thedemons/opentele/blob/main/docs/examples/using-official-apis.md This code initializes a TelegramClient from an existing session file, specified by its name ('telethon.session'). It connects the client to Telegram using the provided api configuration, allowing reuse of an authenticated session. ```python client = TelegramClient("telethon.session", api=api) await client.connect() ```