From 94646b7ee5eee734eedce5e97076a9108a1b9bb1 Mon Sep 17 00:00:00 2001 From: Someone Date: Wed, 8 Dec 2021 19:38:38 +0100 Subject: [PATCH] [somebot] /tissjoin --- modules/CommandTissJoin.py | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 modules/CommandTissJoin.py diff --git a/modules/CommandTissJoin.py b/modules/CommandTissJoin.py new file mode 100644 index 0000000..e61806b --- /dev/null +++ b/modules/CommandTissJoin.py @@ -0,0 +1,59 @@ +# Mattermost Bot module. +# Copyright (c) 2016-2021 by Someone (aka. Jan Vales ) +# published under MIT-License + +import re + + +REX_linesplit = re.compile(r"\xa0|\t|\s{4}") + + +from AbstractCommand import AbstractCommand +class CommandTissJoin(AbstractCommand): + TRIGGER = "tissjoin" + CONFIG = {"display_name": "somebot-command", "auto_complete": True, + "auto_complete_hint": "", + } + USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Parses input and tries to join every LVA-channel. There is no undo." + + + def on_POST(self, request, data): + result = "" + lines = data["text"].splitlines() + for line in lines: + lva_line = REX_linesplit.split(line) + if len(lva_line) < 2: + continue + + lvaname = lva_line[0].strip().replace("&", " ").replace("-", " ").replace("|", " ").replace(",", " ").strip() + lvaname = re.sub(r"\d\d\d\.\d\d\d\s*\w\w\s*$", " ", lvaname).strip() + if len(lvaname) < 5: + continue + + if lvaname.startswith("Titel Std") or lvaname.startswith("Summe "): + continue + + result += "\n+ ``"+lvaname+"``" + + # max 60 char length + channel_results = self.bot.api.search_channel(self.TEAM_ID, lvaname[:60]) # max 60 char length + + if not channel_results: + result += "\n + :stop_sign: could not find suitable channel" + + elif len(channel_results) == 1: + channel = channel_results[0] + result += "\n + :white_check_mark: found channel: ~"+channel["name"]+"\n + trying to join..." + # no useful result + self.bot.api.add_user_to_channel(channel["id"], data["user_id"]) + + else: + result += "\n + :warning: multiple channels found. Join the correct channel(s) by clicking on their name. If they are not clickable, click on ``More...`` to download the full channel-list. Close the window once it opens." + for channel in channel_results: + result += "\n + [~"+channel["name"]+"](https://mattermost.fsinf.at/w-inf-tuwien/channels/"+channel["name"]+")\n" + + + if result == "": + request.cmd_respond_text_temp("### ``TISS-Join results``\nFailed to detect any courses. :(") + + request.cmd_respond_text_temp("### ``TISS-Join results``"+result) -- 2.43.0