This bot forwards photos and videos from a source Telegram group to one or more target groups, translating captions into English (or another specified language) via Google Translate. Built with Python and Telethon, it supports media albums, handles rate limits, and excludes GIFs for efficient media sharing.
- Message Forwarding: Forwards media messages (photos and videos) from a source group to target groups.
- Caption Translation: Translates captions to English (or another specified language) using Google Translate.
- Album Support: Ensures that grouped media (albums) are forwarded in the correct order and as a cohesive unit.
- Flood Wait Handling: Manages Telegram’s rate limits by respecting flood wait errors with automatic retries.
- GIF Filtering: Excludes GIFs to focus on photos and videos only.
- Python 3.7+
- A Telegram account with API credentials (API ID and API hash)
- Bot token from BotFather
-
Clone the Repository:
git clone https://github.com/yourusername/telegram-forwarding-bot.git cd telegram-forwarding-bot
-
Install Dependencies:
Use
pip
to install the required Python libraries:pip install -r requirements.txt
-
Configure API Credentials and Group IDs:
The repository includes a
config.py
file in the root directory as a template for setting up your bot's credentials and group IDs. Simply openconfig.py
and replace the placeholder values with your actual Telegram API credentials and group IDs as shown below:# config.py # Telegram API credentials api_id = 1234567 # Replace with your actual API_ID from my.telegram.org api_hash = 'your_api_hash_here' # Replace with your actual API_HASH from my.telegram.org bot_token = 'your_bot_token_here' # Replace with your bot's token from BotFather # Group IDs source_group = -1000000000000 # Replace with the source group ID where messages will be monitored target_group_1 = -1000000000001 # Replace with the first target group ID where messages will be forwarded target_group_2 = -1000000000002 # Replace with the second target group ID where messages will be forwarded
Security Note: After filling in
config.py
with your actual API credentials and group IDs, make sure to keep your repository private if you’re pushing it to GitHub or another remote host. This will help prevent accidental exposure of sensitive information like your API tokens and group IDs. -
Run the Bot:
Start the bot by running the main script:
python telegram-forwarding-bot.py
The bot automatically listens for new messages in the source group. When it detects a message containing a photo or video:
- If the message includes a caption, the bot translates it to English.
- It then forwards the media and the translated caption to the target groups.
- The bot uses a retry mechanism for sending messages with incremental delays.
- It manages rate limits by handling
FloodWaitError
exceptions, waiting the required time before retrying.
-
Translation Language: By default, captions are translated to English. To change the target language, update the
dest
parameter in the translation function within the code. -
Additional Target Groups: You can add more target groups by including their IDs in
config.py
and adjusting thetarget_groups
list in the code accordingly.
-
API Credentials: Keep
config.py
private and secure. Do not commit it to public repositories. Addconfig.py
to.gitignore
if using Git.echo "config.py" >> .gitignore
-
Rate Limits: Be mindful of Telegram’s rate limits. The bot includes flood wait handling, but excessive requests may lead to temporary restrictions.
-
Environment Variables (Optional): For enhanced security, consider using environment variables for sensitive information instead of
config.py
.
You can easily deploy this bot on Fly.io to keep it running 24/7 without idle timeouts. Fly.io supports GitHub integration, allowing you to automate deployments directly from your GitHub repository.
-
Sign Up on Fly.io and Link Your GitHub Repository:
- Go to Fly.io and sign up or log in.
- In the Fly.io dashboard, select Create New App and choose Deploy from GitHub.
- Connect your GitHub account and select the repository for this bot.
- Choose your Fly.io app name and preferred region for deployment (e.g.,
ams
for Amsterdam,iad
for Virginia).
-
Customize
fly.toml
with Your App Name:-
The repository includes a
fly.toml
file and aDockerfile
configured for Fly.io deployment. -
Open
fly.toml
and updateapp = "your-fly-app-name"
to match the app name you chose on Fly.io:app = "your-fly-app-name" # Replace with your chosen Fly.io app name
-
-
Set Port Configuration to 0:
- During deployment, select Customize Deploy and change the default port setting from
8080
to0
. This ensures the bot functions correctly without a web server requirement.
- During deployment, select Customize Deploy and change the default port setting from
-
Trigger Deployment:
- After setting up GitHub integration, Fly.io will automatically deploy your bot each time you push changes to the repository.
- You can monitor deployment logs and manage the app through the Fly.io dashboard.
- Dockerfile: This repository includes a
Dockerfile
that specifies a lightweight Python environment for deploying the bot. Fly.io uses this file to create a Docker container for the app. - fly.toml: This file configures Fly.io’s deployment settings. Be sure to update the
app
field infly.toml
with the name of your Fly.io app to ensure it matches the setup in your Fly.io dashboard.
Contributions are welcome! Feel free to submit pull requests or open issues for suggestions and bug reports.
This project is licensed under the MIT License. See the LICENSE file for details.
- Dependencies: Ensure that all required dependencies are listed in
requirements.txt
. - Testing: Test the bot thoroughly before deploying it to production, especially if modifying the code to handle additional message types or languages.