├── .env ├── README.md ├── .gitignore └── Translator-Telegram-Bot-with-LLMs.py /.env: -------------------------------------------------------------------------------- 1 | # Telegram Bot Token 2 | TELEGRAM_BOT_TOKEN = "your_telegram_bot_token" 3 | 4 | # Gemini API Key 5 | GEMINI_API_KEY = "your_gemini_api_key" 6 | 7 | # Other Configuration Settings 8 | DEFAULT_TIMEOUT=30 9 | RATE_LIMIT=1 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Telegram Translation Bot 2 | 3 | ## Overview 4 | 5 | This is a Telegram bot specialized in translation, leveraging Large Language Models (LLMs) for high-quality text translation. The bot is built using Python and integrates the **Gemini** LLM, an easy to use google AI model, to provide accurate translations across multiple languages. 6 | 7 | ## Features 8 | 9 | - Supports multiple languages 10 | - Uses **Gemini** for advanced translation 11 | - Works with Telegram Bot API 12 | - Can be deployed on a server for 24/7 operation 13 | 14 | ## Prerequisites 15 | 16 | Before running the bot, ensure you have the following installed: 17 | 18 | - Python 3.8+ 19 | - `requests`, `python-telegram-bot`, and `dotenv` libraries 20 | 21 | Install dependencies using: 22 | 23 | ```bash 24 | pip install -r requirements.txt 25 | ``` 26 | 27 | ## Setup and Configuration 28 | 29 | ### 1. Get a Telegram Bot Token 30 | 31 | 1. Open Telegram and search for [@BotFather](https://t.me/BotFather). 32 | 2. Start a chat and use the command: 33 | ``` 34 | /newbot 35 | ``` 36 | 3. Follow the instructions and copy the generated bot token. 37 | 38 | ### 2. Obtain an API Key for Gemini 39 | 40 | You need an API key from **Gemini** to access its translation model. Visit the [Gemini API portal](https://ai.google.dev/gemini-api/docs/api-key) and generate a key. 41 | 42 | ### 3. Configure Environment Variables 43 | 44 | Create a `.env` file in the project root and add: 45 | 46 | ```ini 47 | TELEGRAM_BOT_TOKEN=your_telegram_bot_token 48 | Gemini_API_KEY=your_gemini_api_key 49 | ``` 50 | 51 | ## Running the Bot 52 | 53 | Run the bot with: 54 | 55 | ```bash 56 | python bot.py 57 | ``` 58 | 59 | If deploying on a VPS, consider using `nohup` or `tmux` to keep it running: 60 | 61 | ```bash 62 | nohup python bot.py 63 | ``` 64 | 65 | ## Deployment 66 | 67 | ### Option 1: Heroku 68 | 69 | 1. Install Heroku CLI and log in: 70 | ```bash 71 | heroku login 72 | ``` 73 | 2. Create a new Heroku app: 74 | ```bash 75 | heroku create your-bot-name 76 | ``` 77 | 3. Set environment variables: 78 | ```bash 79 | heroku config:set TELEGRAM_BOT_TOKEN=your_telegram_bot_token 80 | heroku config:set GEMINI_API_KEY=your_gemini_api_key 81 | ``` 82 | 4. Deploy the bot: 83 | ```bash 84 | git push heroku main 85 | ``` 86 | 87 | ### Option 2: VPS (Recommended for 24/7 availability) 88 | 89 | 1. Set up a VPS on DigitalOcean, AWS, or another provider. 90 | 2. Install Python and required libraries. 91 | 3. Use `screen` or `tmux` to keep the bot running persistently. 92 | 93 | ## API Integration 94 | 95 | The bot interacts with gemini API as follows: 96 | 97 | ```python 98 | import requests 99 | 100 | import requests 101 | 102 | def translate_text(text, source_lang, target_lang): 103 | url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent" 104 | 105 | headers = {"Content-Type": "application/json"} 106 | params = {"key": os.getenv("GEMINI_API_KEY")} 107 | 108 | payload = { 109 | "contents": [{ 110 | "parts": [{ 111 | "text": f"Translate the following text from {source_lang} to {target_lang}: {text}" 112 | }] 113 | }] 114 | } 115 | 116 | response = requests.post(url, json=payload, headers=headers, params=params) 117 | 118 | if response.status_code == 200: 119 | return response.json().get("candidates", [{}])[0].get("content", {}).get("parts", [{}])[0].get("text", "Translation failed") 120 | else: 121 | return f"Error {response.status_code}: {response.text}" 122 | 123 | ``` 124 | 125 | ## Contributing 126 | 127 | Feel free to open issues and submit pull requests to improve the bot. 128 | 129 | ## 130 | 131 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # UV 98 | # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | #uv.lock 102 | 103 | # poetry 104 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 105 | # This is especially recommended for binary packages to ensure reproducibility, and is more 106 | # commonly ignored for libraries. 107 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 108 | #poetry.lock 109 | 110 | # pdm 111 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 112 | #pdm.lock 113 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 114 | # in version control. 115 | # https://pdm.fming.dev/latest/usage/project/#working-with-version-control 116 | .pdm.toml 117 | .pdm-python 118 | .pdm-build/ 119 | 120 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 121 | __pypackages__/ 122 | 123 | # Celery stuff 124 | celerybeat-schedule 125 | celerybeat.pid 126 | 127 | # SageMath parsed files 128 | *.sage.py 129 | 130 | # Environments 131 | .env 132 | .venv 133 | env/ 134 | venv/ 135 | ENV/ 136 | env.bak/ 137 | venv.bak/ 138 | 139 | # Spyder project settings 140 | .spyderproject 141 | .spyproject 142 | 143 | # Rope project settings 144 | .ropeproject 145 | 146 | # mkdocs documentation 147 | /site 148 | 149 | # mypy 150 | .mypy_cache/ 151 | .dmypy.json 152 | dmypy.json 153 | 154 | # Pyre type checker 155 | .pyre/ 156 | 157 | # pytype static type analyzer 158 | .pytype/ 159 | 160 | # Cython debug symbols 161 | cython_debug/ 162 | 163 | # PyCharm 164 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 165 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 166 | # and can be added to the global gitignore or merged into this file. For a more nuclear 167 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 168 | #.idea/ 169 | 170 | # PyPI configuration file 171 | .pypirc 172 | -------------------------------------------------------------------------------- /Translator-Telegram-Bot-with-LLMs.py: -------------------------------------------------------------------------------- 1 | import sys, io, os, time, logging, asyncio 2 | import aiohttp, telebot 3 | from dotenv import load_dotenv 4 | from functools import wraps 5 | from aiohttp import ClientTimeout 6 | 7 | # Set the standard output encoding to UTF-8 8 | sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') 9 | load_dotenv() # Load environment variables from a .env file 10 | 11 | # Retrieve the Telegram bot token and Gemini API key from environment variables 12 | TELEGRAM_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') 13 | GEMINI_API_KEY = os.getenv('GEMINI_API_KEY') 14 | if not all([TELEGRAM_TOKEN, GEMINI_API_KEY]): 15 | raise ValueError("Missing required environment variables") 16 | 17 | # Set default timeout for HTTP requests and rate limit for bot responses 18 | DEFAULT_TIMEOUT = ClientTimeout(total=30) 19 | RATE_LIMIT = 1 20 | 21 | # Configure logging to output to both console and a file 22 | logging.basicConfig( 23 | level=logging.INFO, 24 | format='%(asctime)s - %(levelname)s - %(message)s', 25 | handlers=[logging.StreamHandler(), logging.FileHandler('bot.log')] 26 | ) 27 | logger = logging.getLogger(__name__) 28 | 29 | # Decorator to enforce rate limiting on bot functions 30 | def rate_limit(func): 31 | last_call = 0 32 | @wraps(func) 33 | def wrapper(*args, **kwargs): 34 | nonlocal last_call 35 | now = time.time() 36 | if now - last_call < RATE_LIMIT: 37 | time.sleep(RATE_LIMIT - (now - last_call)) 38 | last_call = time.time() 39 | return func(*args, **kwargs) 40 | return wrapper 41 | 42 | # Initialize the Telegram bot with the provided token 43 | bot = telebot.TeleBot(TELEGRAM_TOKEN) 44 | 45 | # Asynchronous function to call the Gemini language model API 46 | async def call_language_model(prompt, session): 47 | try: 48 | async with session.post( 49 | "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent", 50 | headers={"Content-Type": "application/json"}, 51 | params={"key": os.getenv("GEMINI_API_KEY")}, 52 | json={ 53 | "contents": [ 54 | { 55 | "parts": [ 56 | { 57 | "text": "Translate between English and Persian (Farsi) with accuracy and cultural awareness. " 58 | "Ensure contextually appropriate translations, maintaining a formal tone. " 59 | "If multiple translations exist, choose the most suitable one. " 60 | "Keep responses clear, concise, and professional.\n\n" 61 | "Text: " + prompt 62 | } 63 | ] 64 | } 65 | ], 66 | "temperature": 0.7, 67 | "max_output_tokens": 500, 68 | } 69 | ) as response: 70 | if response.status == 200: 71 | response_json = await response.json() 72 | return response_json["candidates"][0]["content"]["parts"][0]["text"].strip() 73 | else: 74 | logger.error(f"API error: {response.status} - {await response.text()}") 75 | return "Translation service error" 76 | except Exception as e: 77 | logger.error(f"Error: {str(e)}") 78 | return "An error occurred during translation" 79 | 80 | # Handler for incoming messages to the bot 81 | @bot.message_handler(func=lambda _: True) 82 | def handle_message(message): 83 | try: 84 | # Process the translation asynchronously and send the response back to the user 85 | response = asyncio.run(process_translation(message.text)) 86 | bot.reply_to(message, response[:4000] + "\n[...]" if len(response) > 4000 else response) 87 | except Exception as e: 88 | logger.error(f"Translation Error: {str(e)}") 89 | bot.reply_to(message, "Error in translation process") 90 | 91 | # Asynchronous function to process the translation request 92 | async def process_translation(text): 93 | async with aiohttp.ClientSession(timeout=DEFAULT_TIMEOUT) as session: 94 | response = await call_language_model(text, session) 95 | return response.encode('utf-8', 'ignore').decode('utf-8') 96 | 97 | # Main entry point of the script 98 | if __name__ == "__main__": 99 | try: 100 | logger.info("Starting Telegram Bot...") 101 | bot.infinity_polling(timeout=60) # Start polling for new messages 102 | except KeyboardInterrupt: 103 | logger.info("Bot stopped by user") 104 | except Exception as e: 105 | logger.error(f"Fatal error: {str(e)}") --------------------------------------------------------------------------------