https://github.com/romkravets/telegram-bot-googletrans
Telegram Translation Bot A public Telegram bot that translates text, forwarded posts, and photos โ right where you are.
https://github.com/romkravets/telegram-bot-googletrans
deepl google-translate inline-bot ocr poll ptb python python-telegram-bot sqlite telegram-bot-ai-assistant tesseract translation
Last synced: 17 days ago
JSON representation
Telegram Translation Bot A public Telegram bot that translates text, forwarded posts, and photos โ right where you are.
- Host: GitHub
- URL: https://github.com/romkravets/telegram-bot-googletrans
- Owner: romkravets
- Created: 2025-06-11T15:44:08.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2026-05-16T09:01:59.000Z (17 days ago)
- Last Synced: 2026-05-16T10:46:49.255Z (17 days ago)
- Topics: deepl, google-translate, inline-bot, ocr, poll, ptb, python, python-telegram-bot, sqlite, telegram-bot-ai-assistant, tesseract, translation
- Language: Python
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Telegram Translation Bot
A public Telegram bot that translates text, forwarded posts, and photos โ right where you are.
**Developer:** [Roman Kravets](https://github.com/romkravets)
---
## Features
| Feature | Description |
|---------|-------------|
| ๐ฌ Text translation | Send any text โ get an instant translation |
| ๐จ Forwarded posts | Forward a channel post or message โ bot translates it automatically |
| ๐ท Photo OCR | Send a photo with text โ bot extracts and translates it via Tesseract |
| โ๏ธ Inline mode | Type `@botname text` in **any** chat โ see translation without leaving |
| ๐ฅ Group support | Mention the bot `@botname your text` in any group |
| ๐ 30+ languages | DeepL (primary) + Google Translate (fallback) |
| ๐พ Preferences | Per-user language saved in SQLite โ remembered across sessions |
---
## How to use
### Private chat
Just send any text, forward a post, or send a photo with text.
### Groups
Mention the bot with your text:
```
@YourBotName ะัะธะฒัั, ัะบ ัะฟัะฐะฒะธ?
```
For forwarded posts in groups โ mention must appear in the caption.
### Inline mode (translate anywhere)
Type `@YourBotName` followed by a space and your text **in any chat**:
```
@YourBotName The quick brown fox jumps over the lazy dog
```
A translation result will appear as a popup โ tap it to send.
This is the fastest way to translate without switching chats.
### Commands
| Command | Description |
|---------|-------------|
| `/start` | Welcome message + choose target language |
| `/lang` | Open language picker |
| `/lang Polish` | Set language directly by name |
| `/help` | Show this usage guide |
---
## Supported languages
DeepL (high quality) + Google Translate (fallback for all languages):
๐บ๐ฆ Ukrainian ยท ๐ฌ๐ง English ยท ๐ฉ๐ช German ยท ๐ซ๐ท French ยท ๐ช๐ธ Spanish ยท ๐ฎ๐น Italian ยท ๐ต๐ฑ Polish ยท ๐ต๐น Portuguese ยท ๐จ๐ณ Chinese ยท ๐ฏ๐ต Japanese ยท ๐ฐ๐ท Korean ยท ๐ท๐บ Russian ยท ๐ธ๐ฆ Arabic ยท ๐ณ๐ฑ Dutch ยท ๐ธ๐ช Swedish ยท ๐ฉ๐ฐ Danish ยท ๐ซ๐ฎ Finnish ยท ๐จ๐ฟ Czech ยท ๐ธ๐ฐ Slovak ยท ๐ท๐ด Romanian ยท ๐ญ๐บ Hungarian ยท ๐ง๐ฌ Bulgarian ยท ๐ฌ๐ท Greek ยท ๐น๐ท Turkish ยท ๐ฎ๐ฉ Indonesian ยท ๐ณ๐ด Norwegian ยท ๐ฎ๐ณ Hindi ยท ๐ป๐ณ Vietnamese ยท ๐น๐ญ Thai ยท ๐ฎ๐ฑ Hebrew ยท ๐ฎ๐ท Persian
---
## Architecture
```
telegram-bot-googletrans/
โโโ bot.py # Entry point: registers all handlers, starts polling
โโโ config.py # Centralised env var loading (BOT_TOKEN, DEEPL_API_KEY, DB_PATH)
โโโ languages.py # Language registry (31 languages, DeepL + Google codes)
โโโ handlers/
โ โโโ start.py # /start, /lang, /help commands + language keyboard callback
โ โโโ translate.py # Text and forwarded message handlers
โ โโโ photo.py # Photo handler: download โ OCR โ translate
โ โโโ inline.py # Inline query handler (@botname text in any chat)
โโโ services/
โ โโโ translator.py # DeepL (thread-local singleton) + Google fallback
โ โโโ ocr.py # Tesseract OCR via pytesseract + Pillow
โโโ db/
โ โโโ storage.py # SQLite: init, get_language, set_language
โโโ tests/ # 20 unit tests (languages, storage, OCR, translator)
โโโ ecosystem.config.js # PM2 process config for VPS deployment
โโโ requirements.txt # Production dependencies
โโโ requirements-dev.txt # Dev dependencies (adds pytest)
```
All blocking I/O (SQLite, HTTP translation requests, Tesseract OCR) runs via `asyncio.to_thread` so the event loop is never blocked.
---
## Local development
### Prerequisites
- Python 3.10+
- Tesseract OCR installed
Install Tesseract (macOS):
```bash
brew install tesseract
```
Install Tesseract (Ubuntu/Debian):
```bash
sudo apt install tesseract-ocr tesseract-ocr-ukr tesseract-ocr-deu \
tesseract-ocr-fra tesseract-ocr-spa tesseract-ocr-pol tesseract-ocr-chi-sim
```
### Setup
```bash
git clone https://github.com/romkravets/telegram-bot-googletrans
cd telegram-bot-googletrans
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements-dev.txt # includes pytest
cp .env.example .env
# edit .env: fill in BOT_TOKEN and optionally DEEPL_API_KEY
```
### Run tests
```bash
pytest
```
### Start the bot
```bash
python3 bot.py
```
---
## Enable inline mode (required for @botname in any chat)
1. Open [@BotFather](https://t.me/BotFather) in Telegram
2. Send `/setinline`
3. Choose your bot
4. Enter a placeholder hint, e.g. `Enter text to translate...`
That's it. No code changes needed โ the bot already handles inline queries.
---
## VPS deployment (Ubuntu/Debian + PM2)
### 1. Install Node.js + PM2
```bash
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo npm install -g pm2
```
### 2. Install Tesseract
```bash
sudo apt install tesseract-ocr tesseract-ocr-ukr tesseract-ocr-deu \
tesseract-ocr-fra tesseract-ocr-spa tesseract-ocr-pol tesseract-ocr-chi-sim
```
Add more packs as needed: `tesseract-ocr-jpn`, `tesseract-ocr-chi-sim`, etc.
### 3. Clone and configure
```bash
git clone https://github.com/romkravets/telegram-bot-googletrans /opt/translate-bot
cd /opt/translate-bot
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
nano .env # fill in BOT_TOKEN and DEEPL_API_KEY
```
### 4. Start with PM2 and enable auto-start on reboot
```bash
pm2 start ecosystem.config.js
pm2 startup systemd
pm2 save
```
### PM2 commands
```bash
pm2 logs translate-bot # live logs
pm2 restart translate-bot # restart
pm2 stop translate-bot # stop
pm2 list # all running processes
```
---
## Environment variables
| Variable | Required | Description |
|----------|----------|-------------|
| `BOT_TOKEN` | โ
| From [@BotFather](https://t.me/BotFather) |
| `DEEPL_API_KEY` | optional | [DeepL free tier](https://www.deepl.com/pro-api): 500k chars/month. If not set, falls back to Google Translate for all requests |
| `DB_PATH` | optional | SQLite file path (default: `./data/translate_bot.db`) |
---
## Tech stack
- [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot) v22 โ async bot framework
- [DeepL Python SDK](https://github.com/DeepLcom/deepl-python) โ primary translation engine
- [deep-translator](https://github.com/nidhaloff/deep-translator) โ Google Translate fallback
- [pytesseract](https://github.com/madmaze/pytesseract) + [Pillow](https://python-pillow.org/) โ OCR
- SQLite (stdlib) โ user language preferences
- [PM2](https://pm2.keymetrics.io/) โ process manager for VPS