-
Notifications
You must be signed in to change notification settings - Fork 4
/
translate_bot.py
62 lines (54 loc) · 3.69 KB
/
translate_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import logging
import googletrans
import telegram
from telegram import Update, ChatAction
from telegram.ext import CallbackContext
from modules.abstract_module import AbstractModule
from utils.decorators import register_module, register_command, send_action, log_errors
@register_module()
class TranslateBot(AbstractModule):
@register_command(command="trans", short_desc="Translates the given sentence.",
long_desc="Translates the given sentence and returns the translation by setting a base language, translation language and providing a text. See `/trans languages` for a list of all supported languages and their corresponding key.",
usage=["/trans [lang-base] [lang-trans] [text]", "/trans en de Please translate this to German.", "/trans languages"])
@send_action(action=ChatAction.TYPING)
@log_errors()
def translate(self, update: Update, context: CallbackContext):
self.log(text="Trying to translate something..", logging_type=logging.INFO)
chat_id = update.message.chat_id
text = self.get_command_parameter("/trans", update)
if text is not None:
langs = googletrans.LANGCODES
if text == "languages":
message = "*I schreib da moi de Sprochkürzel ausa, de i kau:*\n"
for key, value in langs.items():
message += ''.join(key + ": " + value + ", ")
context.bot.send_message(chat_id=chat_id, text=message, parse_mode=telegram.ParseMode.MARKDOWN)
else:
splitted = text.split(" ", 2)
self.log(text="splitted contains: " + ''.join(str(x + " ") for x in splitted), logging_type=logging.INFO)
langs = langs.values()
if len(splitted) == 3 and len(splitted[0]) == 2 and splitted[0] in langs and len(splitted[1]) == 2 and splitted[1] in langs:
langfrom = splitted[0]
langto = splitted[1]
else:
langfrom = ""
langto = "de"
splitted = text.split(" ", 1)
translator = googletrans.Translator()
if (langfrom and langto):
transtext = translator.translate(text=splitted[2], src=langfrom, dest=langto)
message = "Übersetzt von *" + googletrans.LANGUAGES.get(langfrom).capitalize() + "* zu *" + googletrans.LANGUAGES.get(langto).capitalize() + "* hast des:\n" + transtext.text
context.bot.send_message(chat_id=chat_id, text=message, parse_mode=telegram.ParseMode.MARKDOWN, disable_web_page_preview=True)
else:
if len(splitted) > 1:
transtext = translator.detect(splitted[1])
if transtext.confidence > 0.4:
langfrom = transtext.lang
transtext = translator.translate(text=splitted[1], src=transtext.lang, dest=langto)
context.bot.send_message(chat_id=chat_id, text="Übersetzt von " + googletrans.LANGUAGES.get(langfrom).capitalize() + " zu " + googletrans.LANGUAGES.get(langto).capitalize() + " hast des:\n" + transtext.text)
else:
context.bot.send_message(chat_id=chat_id, text="Oida, i bin ma echt ned sicha wos des Gspeibad hasn soi.. Probiers nuamoi und gib ma vielleicht de Ausgaungssproch mit au!")
else:
context.bot.send_message(chat_id=chat_id, text="Hm, i moa du host do wo a Leerzeichen vagessn, de Wuascht kaun i ned verorbeitn.. Probiers nuamoi!")
else:
update.message.reply_text("I moa du host do wos vagessn Kollege!")