TeleTrans is a Python-based Telegram bot that translates messages in real-time. It uses the OpenAI API and DeepL/Azure API for translation.
Warning
This project requires the use of Telegram Apps API, which is a high-risk operation that could easily lead to a ban of your account. Please make sure you read and understand the Telegram API Terms of Service before using this project.
- Real-time translation of messages.
- Supports multiple languages.
- Uses OpenAI and DeepLX/Azure for translation.
- Configurable source and target languages.
- Command mode for enabling/disabling translation and setting languages.
- Python 3.10+
- aiohttp
- telethon
- requests
- emoji
- azure-ai-translation-text
Click for manual install details
-
Clone the repository:
git clone https://github.com/ihategfw/teletrans.git
-
Navigate to the project directory:
cd teletrans
-
Install the required Python packages:
pip install -r requirements.txt
-
Create a
config.json
file in the project directory:{ "api_id": "your_telegram_api_id", "api_hash": "your_telegram_api_hash", "translation_service": "deeplx", "azure": { "key": "your_azure_key", "endpoint": "https://api.cognitive.microsofttranslator.com/", "region": "global" }, "deeplx": { "url": "your_deeplx_url" }, "openai": { "enable": true, "target_lang": "en", "api_key": "your_openai_api_key", "url": "https://api.openai.com/v1/chat/completions", "model": "gpt-3.5-turbo", "prompt": "Translate the following text to English: ", "temperature": 0.5 }, "target_config": {} }
api_id
andapi_hash
are required for the Telegram API. You can get them by creating a new application at my.telegram.org.translation_service
can be set todeeplx
orazure
.- DeepLX: Click here to get your unique API url.
- Azure: Click here to create an Azure Translator resource and get your Azure key.
- If
openai.enable
is set totrue
, the bot will use the OpenAI API to translate, you can configure thetarget_lang
andprompt
for OpenAI. Only effective when the target language is same asopenai.target_lang
.
-
Run the script with an optional argument to specify the working directory:
python teletrans.py </path/to/your/directory>
If no directory is specified, the script will run in the current directory.
-
Create a new service file:
sudo nano /etc/systemd/system/teletrans.service
-
Add the following content to the file:
[Unit] Description=TeleTrans After=network.target [Service] Type=simple WorkingDirectory=/path/to/teletrans ExecStart=/usr/bin/python3 /path/to/teletrans/teletrans.py /path/to/your/directory Restart=on-failure [Install] WantedBy=multi-user.target
Replace
/path/to/teletrans
with the path to the project directory./path/to/your/directory
is the directory where the bot will store the configuration and logs. -
Start the service and enable it to run on boot:
sudo systemctl start teletrans sudo systemctl enable teletrans
-
Check the status of the service:
sudo systemctl status teletrans
-
To stop the service, use:
sudo systemctl stop teletrans
Click for Docker install details
-
Install Docker:
bash <(curl -sSL https://get.docker.com)
-
Make a directory for the bot:
mkdir teletrans cd teletrans
-
Create a
config.json
file in the project directory:{ "api_id": "your_telegram_api_id", "api_hash": "your_telegram_api_hash", "target_config": {}, "openai": { "enable": true, "api_key": "your_openai_api_key", "url": "https://api.openai.com/v1/chat/completions", "model": "gpt-3.5-turbo" } }
api_id
andapi_hash
are required for the Telegram API. You can get them by creating a new application at my.telegram.org.- If
openai.enable
is set totrue
, the bot will use the OpenAI API to translate, and this is only effective when the target language is English.
-
Run the bot with Docker:
docker run -itd --name teletrans -v $(pwd):/app/config --restart=unless-stopped ghcr.io/ihategfw/teletrans:latest
-
For the first time, you need to execute the following command to log in to your Telegram account:
docker exec -it teletrans python teletrans.py /app/config
Follow the instructions to log in.
After logging in, please stop the container by pressing
Ctrl+C
and restart it:docker restart teletrans
Click for usage details
-
To enable translation from Chinese to English and Japanese, and keep the original message, use the following command in the chat:
.tt-on,zh,zh|en|ja
The code of languages supported by DeepL API can be found here.
-
To disable translation in the chat, simply use:
.tt-off
-
To enable or disable global translation, use the following command:
.tt-on-global,zh,zh|en|ja .tt-off-global
- The chat config is prioritized over the global config.
-
If you want to send a message without translating it, use the
.tt-skip
command followed by your message:.tt-skip Hello, this message will not be translated.
-
If you want to translate only this message once, use the following command, making sure to separate the command and the text with a space; this command ignores the
.tt-on
or.tt-on-global
parameter:.tt-once,en,en|zh Hello, I am teletrans bot
-
Edited message is not translated by default. If you need to translate it, insert
.tt
at the beginning of the message..tt This edited message will be translated.
-
If you want to translate the message you replied to, use the below command:
.tt,zh,zh|en|ja
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MPL-2.0 License - see the LICENSE file for details.