A modular Telethon based bot core.
Project description
Telethon Core
A modular Telegram bot framework built on Telethon with structured logging, environment-based config, and auto-discovery of handlers.
Warning: outdated README.
Features
- Modular handlers - drop a
.pyfile inhandlers/and it's automatically loaded - Multi-sink logging - independent log levels for stdout, file, and Telegram channel via loguru
- Environment-based config - all settings via
.env, no hardcoded values - Graceful lifecycle — SIGINT/SIGTERM handling for clean shutdowns;
restart()viaos.execv - Auto command registration - handlers register their own commands via
core.registerCommand(), registered with Telegram on startup - Docker ready - includes
Dockerfileanddocker-compose.yml
Requirements
- Python 3.11+
- Telegram API credentials from my.telegram.org
- Bot token from @BotFather
Quick Start
Docker (recommended)
git clone https://github.com/theahadev/telethon-core.git
cd telethon-core
cp .env.example .env
# fill in your credentials in .env
docker compose up -d
Manual
git clone https://github.com/theahadev/telethon-core.git
cd telethon-core
pip install -r requirements.txt
# or: uv sync
cp .env.example .env
# fill in your credentials in .env
python main.py
Configuration
Copy .env.example to .env and fill in your values. Required variables are marked below.
Core
| Variable | Required | Description |
|---|---|---|
API_ID |
✅ | Telegram API ID from my.telegram.org |
API_HASH |
✅ | Telegram API Hash |
BOT_TOKEN |
✅ | Bot token from @BotFather |
DATA_FOLDER |
✅ | Directory for session file and persistent data |
Logging
| Variable | Description | Default |
|---|---|---|
LOG_LEVEL_STDOUT |
Console log level | INFO |
LOG_LEVEL_FILE |
File log level | INFO |
LOG_FILE_PATH |
Path to log file (enables file logging) | — |
LOG_ROTATION |
Log rotation policy (e.g. 500 MB, 1 week) |
— |
LOG_RETENTION |
Log retention policy (e.g. 1 month) |
— |
LOG_COMPRESSION |
Compression for rotated logs (gz, zip, etc.) |
— |
LOG_CHANNEL |
Telegram channel ID for log forwarding (enables Telegram logging) | — |
LOG_LEVEL_TELEGRAM |
Telegram log level | INFO |
Note: Errors and above always go to stderr regardless of
LOG_LEVEL_STDOUT. Telegram logging requiresLOG_CHANNELto be set.
Project Structure
telethon-core/
├── src/
│ ├── main.py # Entry point — loads config, sets up client, imports handlers
│ ├── core.py # Shared state, event wrappers, logging setup, lifecycle
│ └── handlers/
│ ├── __init__.py # Auto-discovery and loading of handler modules
│ ├── start.py # /start handler
│ ├── help.py # /help handler
│ └── addchat.py # Bot added to chat handler
├── pyproject.toml # Project metadata
├── Dockerfile
├── docker-compose.yml
└── .env.example # Environment variable template
Writing Handlers
Create any .py file in handlers/ - it's auto-loaded on startup. Register events at module level using the helpers from core:
from typing import Any
from loguru import logger
import core
async def my_handler(event: Any) -> None:
await event.reply("hello!")
core.onMessage(my_handler, pattern=r"^/hello(\s|$)")
core.registerCommand("hello", "Say hello") # optional — queued and sent to Telegram on startup
Event Wrappers
| Wrapper | pattern arg |
Trigger condition |
|---|---|---|
onMessage(func, pattern) |
✅ | A new message is received |
onEdit(func, pattern) |
✅ | A message is edited |
onDelete(func) |
❌ | A message is deleted - only deleted_id/deleted_ids available, no content |
onRead(func) |
❌ | Messages are marked as read |
onCallback(func, pattern) |
✅ | An inline keyboard button is pressed |
onInline(func, pattern) |
✅ | An inline query is sent (@yourbot something) |
onChatAction(func) |
❌ | A chat action occurs (join, leave, title change, pin, etc.) |
onUserUpdate(func) |
❌ | A user update occurs (typing indicator, online status, etc.) |
onRaw(func) |
❌ | Any raw Telegram Update object - unabstracted, last resort |
Bot Commands
Call core.registerCommand(command, description) at module level in your handler, right next to the event registration:
core.onMessage(start_handler, pattern=r"^/start(\s|$)")
core.registerCommand("start", "Start the bot")
Commands are queued as handlers are loaded, then registered with Telegram in bulk right before the main loop starts.
License
MIT - see LICENSE.
Project details
Release history Release notifications | RSS feed
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 telethon_core-0.5.2.tar.gz.
File metadata
- Download URL: telethon_core-0.5.2.tar.gz
- Upload date:
- Size: 22.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abdb5cd6b0ab438ed8ff9cdf12bf71f49f00cbb12f65d7bbc2968ddf5780728b
|
|
| MD5 |
22be3f54634e16d5c64643c7c445758a
|
|
| BLAKE2b-256 |
e5901c783c2da0b9f2a886b75e07ac7621108ef13830fa31e946465b9326bca8
|
Provenance
The following attestation bundles were made for telethon_core-0.5.2.tar.gz:
Publisher:
publish.yml on theahadev/telethon-core
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
telethon_core-0.5.2.tar.gz -
Subject digest:
abdb5cd6b0ab438ed8ff9cdf12bf71f49f00cbb12f65d7bbc2968ddf5780728b - Sigstore transparency entry: 1748085294
- Sigstore integration time:
-
Permalink:
theahadev/telethon-core@67c4493d0d8db073966242bd38cf3519d315d1b1 -
Branch / Tag:
refs/tags/v0.5.2 - Owner: https://github.com/theahadev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@67c4493d0d8db073966242bd38cf3519d315d1b1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file telethon_core-0.5.2-py3-none-any.whl.
File metadata
- Download URL: telethon_core-0.5.2-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
132498b237862ac10fcb578dc368c613b5473b384e6b30ea624984dfa0637946
|
|
| MD5 |
722f72975b7bace42cb955a9227805a6
|
|
| BLAKE2b-256 |
d2af61e2e11497c66dcf2feaa0b0b7fc3a949deb8cec1b70b122e8c063706cba
|
Provenance
The following attestation bundles were made for telethon_core-0.5.2-py3-none-any.whl:
Publisher:
publish.yml on theahadev/telethon-core
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
telethon_core-0.5.2-py3-none-any.whl -
Subject digest:
132498b237862ac10fcb578dc368c613b5473b384e6b30ea624984dfa0637946 - Sigstore transparency entry: 1748085425
- Sigstore integration time:
-
Permalink:
theahadev/telethon-core@67c4493d0d8db073966242bd38cf3519d315d1b1 -
Branch / Tag:
refs/tags/v0.5.2 - Owner: https://github.com/theahadev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@67c4493d0d8db073966242bd38cf3519d315d1b1 -
Trigger Event:
push
-
Statement type: