diff --git a/.gitignore b/.gitignore index d5bd4fb0f4d5644fd884ad06240ef4548532cd93..39c35a55b230d6dc0b6bbe8928a3cd175d4518f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -venv/ -.idea/ -__pycache__/ +**/*.egg-info +venv +**/.idea +**/__pycache__ diff --git a/README.md b/README.md index 1cdfffa588274e0bb9d489628bcfaa1b71fada6b..dcba50b133889523e9e999d9c1f67382f1eed54f 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,15 @@ Currently there is only support for the Mattermost instance of the FeM e.V. Implementing support for other platforms would be possible if needed. + +Getting started +--------------- + +* `git clone ...` +* `pip install -e .` +* `python3 -m translation_bot` +* add json file for bot configuration. see main.py and understand the code and read mmpy bot docu for documentation + Git-Workflow ------------ diff --git a/main.py b/main.py deleted file mode 100644 index 7ea5b7b9098233467f179257e036dcc94863aa25..0000000000000000000000000000000000000000 --- a/main.py +++ /dev/null @@ -1,14 +0,0 @@ -from mmpy_bot import Bot, Settings -from translate import Translator - -bot = Bot( - settings=Settings( - MATTERMOST_URL = "https://mattermost.fem-net.de", - MATTERMOST_PORT = 443, - BOT_TOKEN = "9jpt84d4s7npu8zzz1dfemrw6h", - BOT_TEAM = "FeM", - SSL_VERIFY = True, - ), - plugins=[Translator()], -) -bot.run() \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000000000000000000000000000000000000..84bfc777d4c073c6a7eb03446084d572c6ddb10d --- /dev/null +++ b/setup.py @@ -0,0 +1,11 @@ +import setuptools + +setuptools.setup( + name="translation_bot", + version="0.0.0", + description="mattermost translation bot", + python_requires=">=3.6", + install_rexuires=[ + "mmpy_bot" + ] +) diff --git a/translation_bot/__init__.py b/translation_bot/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/translation_bot/__main__.py b/translation_bot/__main__.py new file mode 100644 index 0000000000000000000000000000000000000000..687b4ce973f72af8ad8db8d04627f5cf95746d41 --- /dev/null +++ b/translation_bot/__main__.py @@ -0,0 +1,3 @@ +from .main import bot + +bot.run() diff --git a/translation_bot/main.py b/translation_bot/main.py new file mode 100644 index 0000000000000000000000000000000000000000..df9ef11e0026c6f52c548160df453c026226dd23 --- /dev/null +++ b/translation_bot/main.py @@ -0,0 +1,22 @@ +from mmpy_bot import Bot, Settings +from .translate import Translator +import json + +with open("bot_settings.json", "r") as file: + lines = file.read() + +bot_settings = json.loads(lines) + +bot = Bot( + settings=Settings( + MATTERMOST_URL = bot_settings["mattormost_url"], + MATTERMOST_PORT = bot_settings["mattermost_port"], + BOT_TOKEN = bot_settings["bot_token"], + BOT_TEAM = bot_settings["bot_team"], + SSL_VERIFY = bot_settings["ssl_verify"], + ), + plugins=[Translator()], +) + +if __name__ == "__main__": + bot.run() diff --git a/translate.py b/translation_bot/translate.py similarity index 62% rename from translate.py rename to translation_bot/translate.py index d4768b28a59283486520075888327909bb0febc2..e23ca7f856ede7f98e30f73cb514325ee4d0a84c 100644 --- a/translate.py +++ b/translation_bot/translate.py @@ -8,27 +8,32 @@ class Translator(Plugin): def __init__(self): super().__init__() - self.handle_command_translate.docstring = "translate an own post. Syntax: !tr  ; e.g. '!tr en de I am a happy text.' will be translated from english to german." # self.handle_command_translate_thread.docstring = "translate a thread. Answer to a message of the thread you want to translate. Arguments: `` ``" @listen_to(r"\A!tr ") async def handle_command_translate(self, message: Message): + """translate an own post. Syntax: !tr  ; e.g. '!tr en de I am a happy text.' will be translated from english to german.""" + if message.sender_name != "System": # parse regex match = re.compile(r"\A!tr (\w+) (\w+) (.*)").match(message.text) - source_lang = match.group(1) - target_lang = match.group(2) - - text_to_translate = match.group(3) - - # translate - translated_text = self.translate(text_to_translate, source_lang, target_lang) - translated_text = "(translated: " + source_lang + " into " + target_lang + ")\n" + translated_text - - # post message - self.driver.reply_to(message, translated_text) + if not match: + self.driver.reply_to(message, "Invalid command. Type '!help'.") + + else: + source_lang = match.group(1) + target_lang = match.group(2) + + text_to_translate = match.group(3) + + # translate + translated_text = self.translate(text_to_translate, source_lang, target_lang) + translated_text = "(translated: " + source_lang + " into " + target_lang + ")\n" + translated_text + + # post message + self.driver.reply_to(message, translated_text) # @listen_to(r"\A!trt ") # def handle_command_translate_thread(self, message: Message):