├── README.md
└── Google_Translator.py
/README.md:
--------------------------------------------------------------------------------
1 | # ATTENTION: IT IS NO LONGER MAINTED.
2 |
3 |
4 | # Google-Translator-telegram-bot-Python
5 |
6 | - It is Telegram bot which translate any text to any language in the world
7 | - It use python telegram bot
8 | - It needs some python module
9 | - **googletrans = 2.4.0**
10 | - **python-telegram-bot = 12.2.0**
11 | - get token by
12 | - open your telegram
13 | - search for @botfather or https://t.me/botfather
14 | - create new bot
15 | - take the token and paste it in python file by the name `python token="your token"`
16 | - run the python file on command line or wherever
17 | # Instruction
18 | - First make sure you have python , pip by opening command line
19 | - Enter python or pip __if you see__
20 | __python' is not recognized as an internal or external command.
21 | operable program or batch file.__
22 | - then install it from [python.org](https://www.python.org/downloads/)
23 | # Installing requirement
24 | ```
25 | pip install python-telegram-bot==12.2.0
26 | ```
27 | ```
28 | pip install googletrans==2.4.0
29 | ```
30 | ## Finally
31 | - **__open command line and run the python file__**
32 |
33 |
--------------------------------------------------------------------------------
/Google_Translator.py:
--------------------------------------------------------------------------------
1 | '''My name is Chapi Menge.Am just Programmer'''
2 | from googletrans import Translator
3 | from googletrans import Translator
4 | from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,ConversationHandler)
5 | import logging
6 | from telegram import Bot
7 | import telegram
8 | '''for the admin notify the logger info'''
9 | logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
10 | level=logging.INFO
11 | )
12 | logger = logging.getLogger(__name__)
13 |
14 | translator = Translator()
15 | token = 'Your token'
16 | bot = Bot(token)
17 | IN = range(1)
18 | form = """
19 | Send me word in any Language and Enter the text You want To Translate
20 | after that add the word to and the language you want to Translate.
21 | For Example -
22 | ፍቅር to English or
23 | love to hindu
24 | Life is just a chance to grow a soul to amharic
25 | then send it to me"""
26 |
27 | LANGUAGES = {
28 | 'af': 'afrikaans',
29 | 'sq': 'albanian',
30 | 'am': 'amharic',
31 | 'ar': 'arabic',
32 | 'hy': 'armenian',
33 | 'az': 'azerbaijani',
34 | 'eu': 'basque',
35 | 'be': 'belarusian',
36 | 'bn': 'bengali',
37 | 'bs': 'bosnian',
38 | 'bg': 'bulgarian',
39 | 'ca': 'catalan',
40 | 'ceb': 'cebuano',
41 | 'ny': 'chichewa',
42 | 'zh-cn': 'chinese (simplified)',
43 | 'zh-tw': 'chinese (traditional)',
44 | 'co': 'corsican',
45 | 'hr': 'croatian',
46 | 'cs': 'czech',
47 | 'da': 'danish',
48 | 'nl': 'dutch',
49 | 'en': 'english',
50 | 'eo': 'esperanto',
51 | 'et': 'estonian',
52 | 'tl': 'filipino',
53 | 'fi': 'finnish',
54 | 'fr': 'french',
55 | 'fy': 'frisian',
56 | 'gl': 'galician',
57 | 'ka': 'georgian',
58 | 'de': 'german',
59 | 'el': 'greek',
60 | 'gu': 'gujarati',
61 | 'ht': 'haitian creole',
62 | 'ha': 'hausa',
63 | 'haw': 'hawaiian',
64 | 'iw': 'hebrew',
65 | 'hi': 'hindi',
66 | 'hmn': 'hmong',
67 | 'hu': 'hungarian',
68 | 'is': 'icelandic',
69 | 'ig': 'igbo',
70 | 'id': 'indonesian',
71 | 'ga': 'irish',
72 | 'it': 'italian',
73 | 'ja': 'japanese',
74 | 'jw': 'javanese',
75 | 'kn': 'kannada',
76 | 'kk': 'kazakh',
77 | 'km': 'khmer',
78 | 'ko': 'korean',
79 | 'ku': 'kurdish (kurmanji)',
80 | 'ky': 'kyrgyz',
81 | 'lo': 'lao',
82 | 'la': 'latin',
83 | 'lv': 'latvian',
84 | 'lt': 'lithuanian',
85 | 'lb': 'luxembourgish',
86 | 'mk': 'macedonian',
87 | 'mg': 'malagasy',
88 | 'ms': 'malay',
89 | 'ml': 'malayalam',
90 | 'mt': 'maltese',
91 | 'mi': 'maori',
92 | 'mr': 'marathi',
93 | 'mn': 'mongolian',
94 | 'my': 'myanmar (burmese)',
95 | 'ne': 'nepali',
96 | 'no': 'norwegian',
97 | 'ps': 'pashto',
98 | 'fa': 'persian',
99 | 'pl': 'polish',
100 | 'pt': 'portuguese',
101 | 'pa': 'punjabi',
102 | 'ro': 'romanian',
103 | 'ru': 'russian',
104 | 'sm': 'samoan',
105 | 'gd': 'scots gaelic',
106 | 'sr': 'serbian',
107 | 'st': 'sesotho',
108 | 'sn': 'shona',
109 | 'sd': 'sindhi',
110 | 'si': 'sinhala',
111 | 'sk': 'slovak',
112 | 'sl': 'slovenian',
113 | 'so': 'somali',
114 | 'es': 'spanish',
115 | 'su': 'sundanese',
116 | 'sw': 'swahili',
117 | 'sv': 'swedish',
118 | 'tg': 'tajik',
119 | 'ta': 'tamil',
120 | 'te': 'telugu',
121 | 'th': 'thai',
122 | 'tr': 'turkish',
123 | 'uk': 'ukrainian',
124 | 'ur': 'urdu',
125 | 'uz': 'uzbek',
126 | 'vi': 'vietnamese',
127 | 'cy': 'welsh',
128 | 'xh': 'xhosa',
129 | 'yi': 'yiddish',
130 | 'yo': 'yoruba',
131 | 'zu': 'zulu',
132 | 'fil': 'Filipino',
133 | 'he': 'Hebrew'
134 | }
135 |
136 | LANGCODES = dict(map(reversed, LANGUAGES.items()))
137 | def start(update,context):
138 | ''' opening conversation
139 | when you send /start to the bot
140 | you can change every string to your customized names or description
141 | '''
142 |
143 | logger.info("Mr of %s: start conversations", update.message.from_user.first_name)
144 | context.bot.send_message(chat_id=update.message.chat_id,
145 | text="Wellcome to Google Translation bot. Mr/Mrs "+ update.message.from_user.first_name+ " Chapi's Always Favorite and Bestie")
146 | update.message.reply_text(form,parse_mode=telegram.ParseMode.HTML)
147 |
148 | return IN # return to state IN and wherever you enter or send it first find in IN
149 | def translater(update,context):
150 | ''' Translator Function
151 | it takes string and split it
152 | then check
153 | 1.the word 'to' in the text
154 | 2.the length of the splited text must greater than 2 means it must have at least one additional word other than 'to' and des.. language
155 | 3.check the language the user entered is found on the language dictionary
156 | it then take the last word as a destination language if it is valid
157 | then delete it the last first means the destination language and last second means 'to'
158 | get the code for the destination language from getdest function
159 | the join the text by space to have the original text format and send to to the translator function
160 | finally print the text and pronunciation of the word
161 | if the last 3 condition is false it sends error message to input the user again
162 | '''
163 |
164 | global text
165 | text = update.message.text
166 | dest = text.split()[-1]
167 | if 'to' in text and len(text.split()) > 2 and languagecheck (dest):
168 | text = text.split()
169 | dest = text[-1]
170 | dest = getdest(dest)
171 | del text[-2],text[-1]
172 | text = ' '.join(text)
173 | text = trans(text,dest)
174 | context.bot.send_message(chat_id=update.message.chat_id , text="""
175 | Translation is
176 | _________________
177 | {}
178 | _________________
179 | pronunciation is
180 | _________________
181 | {}
182 | """.format(text.text,text.pronunciation ),parse_mode=telegram.ParseMode.HTML )
183 | update.message.reply_text(form,parse_mode=telegram.ParseMode.HTML)
184 | else:
185 | update.message.reply_text('Please Enter in The correct format your input is invalid ')
186 | update.message.reply_text(form,parse_mode=telegram.ParseMode.HTML)
187 | # print(text.text)
188 | return IN
189 | def trans(text,dest='en'):
190 | ''' Translator Function
191 | it Translate the text to the destionation language'''
192 | return translator.translate(text,dest=dest)
193 | def cancel(update, context):
194 | ''' to cancel the conversation'''
195 | update.message.reply_text('Thank you! I hope we can talk again some day.\n')
196 | return ConversationHandler.END
197 | def echo(update, context):
198 | ''' echo if any one cancel the conversation and send text to be translated'''
199 | context.bot.send_message(chat_id=update.effective_chat.id, text="Please send /start to start conversation")
200 | def error(update, context):
201 | """Log Errors caused by Updates."""
202 | logger.warning('Update "%s" caused error "%s"', update, context.error)
203 | def languagecheck(text):
204 | if text.lower() in LANGCODES:
205 | return True
206 | else:
207 | return False
208 | def getdest(dest="english"):
209 | try:
210 | return LANGCODES[dest.lower()]
211 | except:
212 | return False
213 | def main():
214 | ''' updater startup'''
215 | updater = Updater(token=token,use_context=True)
216 | dispatcher = updater.dispatcher
217 | '''conversation handeled by this'''
218 | conv_handler = ConversationHandler (
219 | entry_points=[CommandHandler('start', start)],
220 |
221 | states={
222 | IN: [MessageHandler(Filters.text , translater)],
223 |
224 | },
225 |
226 | fallbacks=[CommandHandler('cancel', cancel)] ,)
227 |
228 |
229 | ''' registering the hander to dispatcher'''
230 |
231 | echo_handler = MessageHandler(Filters.text, echo)
232 | dispatcher.add_handler(echo_handler)
233 | dispatcher.add_error_handler(error)
234 | dispatcher.add_handler(conv_handler)
235 | ''' start fetching dat from the telegram'''
236 | updater.start_polling()
237 | ''' to stop the code runnig in the cmd by control+c'''
238 | updater.idle()
239 |
240 | '''driver'''
241 | if __name__ == '__main__':
242 | main()
243 |
--------------------------------------------------------------------------------