From e643a9e9ccad0fbadf726cd301763721c46b8ee6 Mon Sep 17 00:00:00 2001 From: Jakob Lerch Date: Fri, 13 Aug 2021 21:06:18 +0200 Subject: [PATCH 1/6] change gitignore --- .gitignore | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index d5bd4fb..39c35a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -venv/ -.idea/ -__pycache__/ +**/*.egg-info +venv +**/.idea +**/__pycache__ -- GitLab From f586c0fdd7e8ed2f3922ed734aeffbc8b567ca28 Mon Sep 17 00:00:00 2001 From: Jakob Lerch Date: Fri, 13 Aug 2021 21:06:48 +0200 Subject: [PATCH 2/6] change README.md --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 1cdfffa..ad10be3 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` + + Git-Workflow ------------ -- GitLab From d5ab2fa42c2e6a3857203cceb5e9e4ffbdb163ee Mon Sep 17 00:00:00 2001 From: Jakob Lerch Date: Fri, 13 Aug 2021 21:07:36 +0200 Subject: [PATCH 3/6] setup the project to be a module --- setup.py | 11 ++++++++ translation_bot/__init__.py | 0 translation_bot/__main__.py | 3 ++ main.py => translation_bot/main.py | 6 ++-- translate.py => translation_bot/translate.py | 29 ++++++++++++-------- 5 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 setup.py create mode 100644 translation_bot/__init__.py create mode 100644 translation_bot/__main__.py rename main.py => translation_bot/main.py (79%) rename translate.py => translation_bot/translate.py (62%) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..84bfc77 --- /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 0000000..e69de29 diff --git a/translation_bot/__main__.py b/translation_bot/__main__.py new file mode 100644 index 0000000..687b4ce --- /dev/null +++ b/translation_bot/__main__.py @@ -0,0 +1,3 @@ +from .main import bot + +bot.run() diff --git a/main.py b/translation_bot/main.py similarity index 79% rename from main.py rename to translation_bot/main.py index 7ea5b7b..f6eda70 100644 --- a/main.py +++ b/translation_bot/main.py @@ -1,5 +1,5 @@ from mmpy_bot import Bot, Settings -from translate import Translator +from .translate import Translator bot = Bot( settings=Settings( @@ -11,4 +11,6 @@ bot = Bot( ), plugins=[Translator()], ) -bot.run() \ No newline at end of file + +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 d4768b2..e23ca7f 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): -- GitLab From f7ea9b20068e79e3807514b709b0a3594d10c288 Mon Sep 17 00:00:00 2001 From: Jakob Lerch Date: Thu, 19 Aug 2021 23:44:34 +0200 Subject: [PATCH 4/6] outsource bot settings to json file --- translation_bot/main.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/translation_bot/main.py b/translation_bot/main.py index f6eda70..46f1234 100644 --- a/translation_bot/main.py +++ b/translation_bot/main.py @@ -1,13 +1,19 @@ 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 = "https://mattermost.fem-net.de", - MATTERMOST_PORT = 443, - BOT_TOKEN = "9jpt84d4s7npu8zzz1dfemrw6h", - BOT_TEAM = "FeM", - SSL_VERIFY = True, + 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()], ) -- GitLab From bea7e1753fe18351973ca076a4df44ae84807fab Mon Sep 17 00:00:00 2001 From: Jakob Lerch Date: Thu, 19 Aug 2021 23:47:48 +0200 Subject: [PATCH 5/6] fix space problem --- translation_bot/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/translation_bot/main.py b/translation_bot/main.py index 46f1234..df9ef11 100644 --- a/translation_bot/main.py +++ b/translation_bot/main.py @@ -3,9 +3,9 @@ from .translate import Translator import json with open("bot_settings.json", "r") as file: - lines = file.read() + lines = file.read() -bot_settings = json.loads(lines) +bot_settings = json.loads(lines) bot = Bot( settings=Settings( -- GitLab From 5023d28e14bc1e705c3a62c8b06df9f28f12f359 Mon Sep 17 00:00:00 2001 From: Jakob Lerch Date: Thu, 19 Aug 2021 23:51:28 +0200 Subject: [PATCH 6/6] update Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ad10be3..dcba50b 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ 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 ------------ -- GitLab