Registry / communication / telethon

telethon

JSON →
library1.43.0pypypi✓ verified 35d ago

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.

communicationhttp-networkingweb-framework
Install & Compatibility
Where this runs
tested against v1.43.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 1.247s · 30.2MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 3.5s · import 1.185s · 31MB
30MB installed
● package 30MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

from telethon import TelegramClient
from telethon import events
from telethon import errors

While 'import telethon.sync' was a common pattern in v1.x to run async code synchronously, it is removed in v2.x and generally discouraged in favor of proper asyncio usage.

import telethon.sync

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.

import os import asyncio from telethon import TelegramClient # Get API ID and API Hash from environment variables for security # You can obtain these from https://my.telegram.org api_id = int(os.environ.get('TG_API_ID', 0)) api_hash = os.environ.get('TG_API_HASH', '') # 'session_name' will create 'session_name.session' file # This file stores your login information and should be kept private. client = TelegramClient('session_name', api_id, api_hash) async def main(): if not api_id or not api_hash: print("Please set TG_API_ID and TG_API_HASH environment variables.") return print("Connecting to Telegram...") await client.start() print("Client Connected!") # Get information about yourself me = await client.get_me() print(f"Logged in as: {me.first_name} (@{me.username})") # Example: Send a message to yourself # await client.send_message('me', 'Hello from Telethon!') # print("Message sent to self!") # Keep the client running until disconnected (e.g., by Ctrl+C) await client.run_until_disconnected() if __name__ == '__main__': asyncio.run(main())
Debug
Known footguns
breakingTelethon v2.x is a complete rewrite and introduces significant breaking changes from v1.x (including API changes, renamed classes like TelegramClient to Client, and removal of telethon.sync). Code written for v1.x will likely not run on v2.x without substantial modification. Always consult the migration guide when upgrading to v2.x.
gotchaTelethon is an asynchronous library. Incorrectly mixing asynchronous operations (like network requests) with synchronous code without proper `await` calls or an active `asyncio` event loop is a common mistake that can lead to unresponsive handlers, runtime errors, or unexpected behavior.
gotchaThe `api_id` and `api_hash` are critical for client creation and must be obtained from your Telegram API development tools page (my.telegram.org). Using incorrect, placeholder, or expired values will prevent the client from connecting to Telegram.
gotchaSession files (`.session`) contain your encrypted authentication keys and sensitive login information. They are essential for persistent logins and should be protected, excluded from version control (e.g., via `.gitignore`), and never shared. Using the same session file for multiple concurrently running Telethon clients will result in `sqlite3.OperationalError: database is locked` or similar connection issues.
gotchaTelegram API has rate limits. Sending too many requests in a short period can trigger `FloodWaitError`, requiring the client to wait for a specified duration before making further requests. While Telethon can automatically handle some `FloodWaitError` conditions, excessive flooding can lead to account bans.
gotchaFor optimal performance, particularly concerning file downloads/uploads and update handling, it is highly recommended to install the `cryptg` package. Without it, Telethon defaults to a slower pure Python implementation for encryption/decryption, leading to reduced speed.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
122 hits · last 30 days
ahrefsbot
4
gptbot
3
chatgpt-user
3
googlebot
2
oai-searchbot
2
script
1
deepseek
1
commoncrawl
1
Resources