├── Procfile ├── requirements.txt ├── README.md ├── LICENSE └── main.py /Procfile: -------------------------------------------------------------------------------- 1 | worker: python3 main.py 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pyrogram==1.2.9 2 | tgcrypto==1.2.2 3 | googletrans==4.0.0-rc1 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Translator Bot 2 | A google translator telegram bot 3 | 4 | --- 5 | 6 | ## Deploy 7 | 8 | ```sh 9 | git clone https://github.com/FayasNoushad/Translator-Bot.git 10 | cd Translator-Bot 11 | python3 -m venv venv 12 | . ./venv/bin/activate 13 | pip3 install -r requirements.txt 14 | # 15 | python3 main.py 16 | ``` 17 | 18 | --- 19 | 20 | ## Variables 21 | 22 | - `API_HASH` Your API Hash from my.telegram.org 23 | - `API_ID` Your API ID from my.telegram.org 24 | - `BOT_TOKEN` Your bot token from @BotFather 25 | 26 | --- 27 | 28 | ### Use [Translator-Bot-V3](https://github.com/FayasNoushad/Translator-Bot-V3), it is the latest and best version with more features. 29 | 30 | --- 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Fayas Noushad 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import os 2 | from io import BytesIO 3 | from googletrans import Translator 4 | from pyrogram import Client, filters, enums 5 | from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton 6 | 7 | 8 | Bot = Client( 9 | "Translator Bot", 10 | bot_token = os.environ["BOT_TOKEN"], 11 | api_id = int(os.environ["API_ID"]), 12 | api_hash = os.environ["API_HASH"] 13 | ) 14 | 15 | START_TEXT = """Hello {}, 16 | 17 | I am a google translator telegram bot.""" 18 | HELP_TEXT = """ 19 | - Just send a text with language code 20 | 21 | example :- `This is a sample text | ml` 22 | """ 23 | ABOUT_TEXT = """**About Me** 24 | 25 | - **Bot :** `Translator Bot` 26 | - **Developer :** 27 | • [GitHub](https://github.com/FayasNoushad) 28 | • [Telegram](https://telegram.me/FayasNoushad) 29 | - **Source :** [Click here](https://github.com/FayasNoushad/Translator-Bot) 30 | - **Language :** [Python3](https://python.org) 31 | - **Library :** [Pyrogram](https://pyrogram.org)""" 32 | START_BUTTONS = InlineKeyboardMarkup( 33 | [ 34 | [ 35 | InlineKeyboardButton('Help', callback_data='help'), 36 | InlineKeyboardButton('About', callback_data='about'), 37 | InlineKeyboardButton('Close', callback_data='close') 38 | ] 39 | ] 40 | ) 41 | HELP_BUTTONS = InlineKeyboardMarkup( 42 | [ 43 | [ 44 | InlineKeyboardButton('Home', callback_data='home'), 45 | InlineKeyboardButton('About', callback_data='about'), 46 | InlineKeyboardButton('Close', callback_data='close') 47 | ] 48 | ] 49 | ) 50 | ABOUT_BUTTONS = InlineKeyboardMarkup( 51 | [ 52 | [ 53 | InlineKeyboardButton('Feedback', url='https://telegram.me/FayasNoushad') 54 | ], 55 | [ 56 | InlineKeyboardButton('Home', callback_data='home'), 57 | InlineKeyboardButton('Help', callback_data='help'), 58 | InlineKeyboardButton('Close', callback_data='close') 59 | ] 60 | ] 61 | ) 62 | CLOSE_BUTTON = InlineKeyboardMarkup( 63 | [ 64 | [ 65 | InlineKeyboardButton('Close', callback_data='close') 66 | ] 67 | ] 68 | ) 69 | TRANSLATE_BUTTON = InlineKeyboardMarkup( 70 | [ 71 | [ 72 | InlineKeyboardButton("Feedback", url="https://telegram.me/FayasNoushad") 73 | ] 74 | ] 75 | ) 76 | DEFAULT_LANGUAGE = os.environ.get("DEFAULT_LANGUAGE", "en") 77 | 78 | 79 | @Bot.on_callback_query() 80 | async def cb_data(bot, update): 81 | if update.data == "home": 82 | await update.message.edit_text( 83 | text=START_TEXT.format(update.from_user.mention), 84 | disable_web_page_preview=True, 85 | reply_markup=START_BUTTONS 86 | ) 87 | elif update.data == "help": 88 | await update.message.edit_text( 89 | text=HELP_TEXT, 90 | disable_web_page_preview=True, 91 | reply_markup=HELP_BUTTONS 92 | ) 93 | elif update.data == "about": 94 | await update.message.edit_text( 95 | text=ABOUT_TEXT, 96 | disable_web_page_preview=True, 97 | reply_markup=ABOUT_BUTTONS 98 | ) 99 | else: 100 | await update.message.delete() 101 | 102 | 103 | @Bot.on_message(filters.command(["start"])) 104 | async def start(bot, update): 105 | text = START_TEXT.format(update.from_user.mention) 106 | reply_markup = START_BUTTONS 107 | await update.reply_text( 108 | text=text, 109 | disable_web_page_preview=True, 110 | reply_markup=reply_markup 111 | ) 112 | 113 | 114 | @Bot.on_message(filters.text) 115 | async def translate(bot, update): 116 | if update.chat.type == enums.ChatType.PRIVATE: 117 | if " | " in update.text: 118 | text, language = update.text.split(" | ", 1) 119 | else: 120 | text = update.text 121 | language = DEFAULT_LANGUAGE 122 | else: 123 | text = update.reply_to_message.text 124 | if " " in update.text: 125 | language = update.text.split(" | ", 1)[1] 126 | else: 127 | language = DEFAULT_LANGUAGE 128 | translator = Translator() 129 | message = await update.reply_text("`Translating...`") 130 | try: 131 | translate = translator.translate(text, dest=language) 132 | translate_text = f"**Translated to {language}**" 133 | translate_text += f"\n\n`{translate.text}`" 134 | if len(translate_text) < 4096: 135 | await message.edit_text( 136 | text=translate_text, 137 | disable_web_page_preview=True, 138 | reply_markup=TRANSLATE_BUTTON 139 | ) 140 | else: 141 | with BytesIO(str.encode(str(translate_text))) as translate_file: 142 | translate_file.name = language + ".txt" 143 | await update.reply_document( 144 | document=translate_file, 145 | reply_markup=TRANSLATE_BUTTON 146 | ) 147 | await message.delete() 148 | except Exception as error: 149 | print(error) 150 | await message.edit_text("Something wrong.") 151 | 152 | 153 | Bot.run() 154 | --------------------------------------------------------------------------------