Telethon is a full-featured, asynchronous Python 3 client library for the Telegram API. It allows interaction with Telegram's MTProto API as a user or through a bot account. Currently at version 1.43.0, the library receives regular updates, including new API layers and bug fixes.
pip install telethonVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize and connect a Telethon client. It logs in the user (or registers if it's the first run) using the `api_id` and `api_hash` provided via environment variables. The session is saved to a `.session` file for persistence. The client runs asynchronously until manually disconnected.
Review the official 'Migrating from v1 to v2' documentation for detailed instructions on adapting your codebase to the new API and structure.
Ensure all Telethon API calls within an `async` function are `await`ed. If integrating with synchronous code, use `asyncio.run()` for your main async function and avoid blocking the event loop.
Always retrieve your unique `api_id` and `api_hash` from https://my.telegram.org. Store them securely, preferably in environment variables, and never hardcode them directly into public repositories.
Treat session files as highly sensitive. Use a unique session name for each client instance, especially if running multiple scripts. If an error occurs, ensure no other process is holding the session file lock.
Implement proper delays and back-off strategies in your code, especially when performing bulk operations. Use the `FloodWaitError.seconds` attribute to dynamically adjust wait times. Monitor Telegram's official API guidelines for rate limits.
Install `cryptg` using `pip install cryptg`. Verify its usage by enabling logging at INFO level before importing Telethon.
Install Telethon using pip: `pip install telethon`.
Import 'InputPeerChannel' from the correct module: `from telethon.tl.types import InputPeerChannel`.
Implement error handling to catch 'FloodWaitError' and pause execution for the specified duration before retrying.
Verify that the phone number is correctly formatted and valid before attempting to authenticate.
Ensure the user has the necessary admin rights in the chat before performing the operation.