This repository contains a Telegram bot built using Python and the Telethon library. The bot is designed to fetch messages from source channels, filter and translate them, and then post them to designated target channels. This README provides instructions on how to set up, configure, and run the bot.
- 📥 Fetch messages from multiple Telegram channels.
- 🔍 Filter out specific words, URLs, and usernames.
- 🌐 Translate messages to a specified language using Google Translate.
- 📤 Post the translated messages to target Telegram channels.
- 📊 Track and display statistics on the number of messages processed.
Before running the bot, you need to have the following:
- Python 3.7 or higher 🐍
- Telethon library: Used to interact with Telegram's API.
- Googletrans library: For message translation.
- Telegram API credentials:
api_id
,api_hash
, andphone_number
.
-
Clone the repository:
git clone <repository_url> cd <repository_name>
-
Install required dependencies:
pip install -r requirements.txt
-
Create a
config.py
file with your Telegram credentials:# config.py api_id = 'YOUR_API_ID' api_hash = 'YOUR_API_HASH' phone_number = 'YOUR_PHONE_NUMBER'
-
Create the following files (if needed):
filtered_words.txt
: A text file containing words to be filtered out from the messages. Each word should be on a separate line.target_language.txt
: A text file containing the target language code (e.g.,en
for English,es
for Spanish). If not specified, the default is English.
-
Run the bot:
python Translate-bot-v1.py
When the bot starts, it will listen to the configured source channels and will handle new messages accordingly.
- Source Channels: The channels from which messages are fetched are defined in the
channel_groups
dictionary. Each group is represented as a key (e.g.,group1
) and the list of source channels for that group.channel_groups = { 'group1': ['source_channel_1', 'source_channel_2'], 'group2': ['source_channel_3', 'source_channel_4'], 'group3': ['source_channel_5', 'source_channel_6'] }
- Target Channels: The target channels to which messages are posted are defined in the
target_channels
dictionary.target_channels = { 'group1': 'target_channel_1', 'group2': 'target_channel_2', 'group3': 'target_channel_3' }
- The bot can remove specific words from messages before posting them. These words are loaded from the
filtered_words.txt
file.
- The bot uses Google Translate (via the
googletrans
library) to translate messages to a target language. By default, this language is set to English (en
). You can change it by modifying thetarget_language.txt
file.
- Error Logging: Errors and other log information are logged to a file located in the
log
directory (log/error_log.txt
). - Console Output: Logs are also printed to the console for easier debugging.
- The bot listens for new messages from the configured source channels.
- When a message is received, it goes through the following steps:
- Filtering: URLs, usernames, and filtered words are removed.
- Translation: If translation is enabled, the message is translated to the target language.
- Posting: The cleaned and translated message is posted to the corresponding target channel.
- Media Handling: The bot also supports forwarding media (photos, videos, documents) with the translated caption.
- The bot keeps track of the number of messages processed from each channel group.
- Statistics are displayed in the terminal every 5 minutes, showing the number of messages processed for each group.
- If an error occurs while processing a message, the bot will retry up to 3 times.
- If the retries fail, an error message is logged to both the log file and the console.
- Main Function: The bot starts by calling the
main()
function, which initializes the Telegram client, starts the event handlers, and runs until the bot is disconnected. - To run the bot:
python Translate-bot-v1.py
- The bot requires a Telegram session to be saved. Make sure to use the correct phone number when prompted.
- Ensure that your Telegram account is authorized to access the channels specified in the configuration.
This project is licensed under the MIT License. See the LICENSE file for more details.
If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are welcome.
For any questions or issues, please open an issue on GitHub or contact me at [[email protected]].