This project implements a Telegram bot using Telethon and FastAPI that automatically forwards messages from specified source channels to a target channel.
The repository is structured as follows:
.
└── app
├── __init__.py
├── api
│ ├── __init__.py
│ ├── health_check.py
│ └── telegram.py
├── main.py
├── settings.py
└── telegram
├── __init__.py
├── client.py
└── instance.py
app/main.py
: The entry point of the FastAPI application.app/api/telegram.py
: Handles message forwarding logic.app/api/health_check.py
: Implements health check endpoint.app/telegram/client.py
: Contains the Telethon client implementation.app/telegram/instance.py
: Manages Telegram client instance.app/settings.py
: Stores configuration settings for the application.
- Ensure you have Python 3.11 or later installed.
- Clone the repository:
git clone <repository-url> cd <repository-name>
- Create and activate a virtual environment:
python -m venv .venv source .venv/bin/activate # On Windows, use `.venv\Scripts\activate`
- Install the required dependencies using uv:
uv pip install -r requirements.txt
-
Create a
.env
file in the root directory with the following variables:# Telegram API credentials TELEGRAM__API_ID=your_api_id TELEGRAM__API_HASH=your_api_hash # Channel configuration TELEGRAM__CHANNELS=channel1_id,channel2_id,channel3_id # Source channels to monitor TELEGRAM__TARGET_CHANNEL_ID=target_channel_id # Channel where messages will be forwarded
-
To obtain your API credentials:
- Go to https://my.telegram.org/auth
- Log in with your phone number
- Go to 'API development tools'
- Create a new application to get your API_ID and API_HASH
To start the FastAPI server:
uv run fastapi dev app/main.py
The server will start, and you can access the API documentation at http://localhost:8000/docs
.
- The bot monitors the specified source channels (TELEGRAM__CHANNELS)
- When a new message is posted in any of the source channels, the bot automatically forwards it to the target channel ( TELEGRAM__TARGET_CHANNEL_ID)
- The FastAPI application provides an interface to monitor the bot's status and operations
[Source Channels] -> [Telethon Client] -> [Target Channel]
livez
/redyz
: Health check endpoint to verify the service is runningtelegram/channels
: Channels CRUD
For deployment, consider using a production-grade ASGI server like Gunicorn with Uvicorn workers:
gunicorn app.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
Ensure to set up proper SSL/TLS for secure communication when deploying to production.
- Make sure your Telegram account has the necessary permissions to access the source channels and post in the target channel
- Be mindful of Telegram's rate limits when configuring multiple source channels
- Keep your API credentials secure and never commit them to version control