An experimental full MTProto Python client library
Project description
TGLib
An experimental, full-featured MTProto Python client library for Telegram.
Built from scratch. Async-first. Lightweight. Telethon-style API.
โ ๏ธ Experimental
TGLib is currently in early development. APIs may change without notice. Use in production at your own risk. Contributions and feedback are welcome!
โจ Features
- ๐ Full MTProto implementation โ low-level Telegram protocol support
- โก Async-first โ built entirely on
asyncio - ๐ Sync & Async support โ use whichever style you prefer
- ๐๏ธ Session persistence โ via
aiosqlite - ๐ Encryption โ powered by
pyaesandpycryptodome - ๐ค Bot & User client โ supports both bot tokens and phone login
- ๐ชถ Lightweight โ minimal dependencies, maximum control
- ๐ Python 3.10+ โ uses modern Python features
๐ฆ Installation
From PyPI:
pip install tglib
From source:
git clone https://github.com/ankit-chaubey/TGLib.git
cd TGLib
pip install -e .
๐ Quick Start
Get your api_id and api_hash from my.telegram.org.
Never hardcode credentials โ use environment variables!
export API_ID=your_api_id
export API_HASH=your_api_hash
๐ Usage Styles
TGLib supports three usage styles, just like Telethon.
1. Async (recommended)
import asyncio
import os
from tglib import TelegramClient
client = TelegramClient(
session="my_session",
api_id=int(os.environ["API_ID"]),
api_hash=os.environ["API_HASH"],
)
async def main():
await client.start(phone="+1234567890")
me = await client.get_me()
print(f"Logged in as: {me.first_name}")
await client.disconnect()
asyncio.run(main())
2. Async Context Manager (cleanest)
import asyncio
import os
from tglib import TelegramClient
async def main():
async with TelegramClient(
session="my_session",
api_id=int(os.environ["API_ID"]),
api_hash=os.environ["API_HASH"],
) as client:
me = await client.get_me()
print(f"Logged in as: {me.first_name}")
asyncio.run(main())
3. Sync (beginner-friendly, no asyncio needed)
import os
from tglib import TelegramClient
with TelegramClient(
session="my_session",
api_id=int(os.environ["API_ID"]),
api_hash=os.environ["API_HASH"],
) as client:
me = client.run(client.get_me())
print(f"Logged in as: {me.first_name}")
๐ค Bot Example
import asyncio
import os
from tglib import TelegramClient
client = TelegramClient(
session="bot_session",
api_id=int(os.environ["API_ID"]),
api_hash=os.environ["API_HASH"],
)
@client.on(None) # handle all updates
async def handler(update):
print(update)
async def main():
await client.start(bot_token=os.environ["BOT_TOKEN"])
print("Bot is running...")
await client.run_until_disconnected()
asyncio.run(main())
๐ก Sending Messages
# Send a plain message
await client.send_message("username", "Hello from TGLib!")
# Send with markdown
await client.send_message("username", "**Bold** and *italic*", parse_mode="md")
# Send with HTML
await client.send_message("username", "<b>Bold</b> text", parse_mode="html")
# Reply to a message
await client.send_message("username", "Replying!", reply_to=message_id)
๐ฅ Receiving Messages
async def main():
await client.start(phone="+1234567890")
# Get recent messages
messages = await client.get_messages("username", limit=10)
for msg in messages.messages:
print(msg.message)
# Get dialogs
dialogs = await client.get_dialogs(limit=20)
asyncio.run(main())
๐ run_until_disconnected
Keep your client or bot alive until manually stopped (Ctrl+C):
# Async
await client.run_until_disconnected()
# Sync
client.run(client.run_until_disconnected())
๐ง Dependencies
| Package | Purpose |
|---|---|
pyaes |
AES encryption for MTProto |
pycryptodome |
RSA and additional crypto |
aiosqlite |
Async session storage |
๐ Project Structure
TGLib/
โโโ tglib/
โ โโโ __init__.py # Entry point
โ โโโ client.py # Main TelegramClient
โ โโโ crypto/ # Encryption & MTProto crypto
โ โโโ network/ # TCP transport & MTProto sender
โ โโโ sessions/ # SQLite & Memory sessions
โ โโโ tl/ # TL schema types & functions
โ โ โโโ types/ # Telegram object types
โ โ โโโ functions/ # Telegram API functions
โ โโโ errors/ # RPC & custom errors
โ โโโ helpers.py # Utility functions
โโโ setup.py
โโโ README.md
๐ค Contributing
Contributions are very welcome! Here's how to get started:
# Fork the repo, then:
git clone https://github.com/YOUR_USERNAME/TGLib.git
cd TGLib
pip install -e .
- Create a new branch:
git checkout -b feature/your-feature - Make your changes
- Push and open a Pull Request
๐ Issues & Feedback
Found a bug or have a suggestion?
๐ Open an issue
๐ License
This project is licensed under the MIT License โ see the LICENSE file for details.
๐ค Author
Ankit Chaubey
๐ง ankitchaubey.dev@gmail.com
๐ github.com/ankit-chaubey
Made with โค๏ธ and Python
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tglib-0.1.3.tar.gz.
File metadata
- Download URL: tglib-0.1.3.tar.gz
- Upload date:
- Size: 418.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
966f72a06b89a00d4d3492d2aa798db398de1fcc6422572b46557850910f84ed
|
|
| MD5 |
30b6ded3269c16099ac93edba0a221a2
|
|
| BLAKE2b-256 |
40ae0277cbe5acaf7a2e97ace7dd8fc644d5be97156e38bcaf9703b936b3ac22
|
File details
Details for the file tglib-0.1.3-py3-none-any.whl.
File metadata
- Download URL: tglib-0.1.3-py3-none-any.whl
- Upload date:
- Size: 464.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71e4c17c80791f9fcd8302cedcfc2ec76a9167f37eb5c3eb15d30661ae3d68d5
|
|
| MD5 |
a79438aa38d2d35b04d198e689a9c521
|
|
| BLAKE2b-256 |
38ade512e18fc7a11df43a71c5cd96e70bcd6b7f414c03dd196cf9354880c216
|