]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandTissJoin.py
[somebot] requirements.txt for all modules.
[pub/jan/mattermost-bot.git] / modules / CommandTissJoin.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2016-2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 #  published under MIT-License
4
5 import re
6
7
8 REX_linesplit = re.compile(r"\xa0|\t|\s{4}")
9
10
11 from AbstractCommand import AbstractCommand
12 class CommandTissJoin(AbstractCommand):
13     TRIGGER = "tissjoin"
14     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
15               "auto_complete_hint": "<as-is-copy/paste of one TISS-LVA-Cockpit table>",
16              }
17     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Parses input and tries to join every LVA-channel. There is no undo."
18
19
20     def on_POST(self, request, data):
21         result = ""
22         lines = data["text"].splitlines()
23         for line in lines:
24             lva_line = REX_linesplit.split(line)
25             if len(lva_line) < 2:
26                 continue
27
28             lvaname = lva_line[0].strip().replace("&", " ").replace("-", " ").replace("|", " ").replace(",", " ").strip()
29             lvaname = re.sub(r"\d\d\d\.\d\d\d\s*\w\w\s*$", " ", lvaname).strip()
30             if len(lvaname) < 5:
31                 continue
32
33             if lvaname.startswith("Titel   Std") or lvaname.startswith("Summe   "):
34                 continue
35
36             result += "\n+ ``"+lvaname+"``"
37
38             # max 60 char length
39             channel_results = self.bot.api.search_channel(self.TEAM_ID, lvaname[:60]) # max 60 char length
40
41             if not channel_results:
42                 result += "\n   + :stop_sign: could not find suitable channel"
43
44             elif len(channel_results) == 1:
45                 channel = channel_results[0]
46                 result += "\n   + :white_check_mark: found channel: ~"+channel["name"]+"\n   + trying to join..."
47                 # no useful result
48                 self.bot.api.add_user_to_channel(channel["id"], data["user_id"])
49
50             else:
51                 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."
52                 for channel in channel_results:
53                     result += "\n     + [~"+channel["name"]+"](https://mattermost.fsinf.at/w-inf-tuwien/channels/"+channel["name"]+")\n"
54
55
56         if result == "":
57             request.cmd_respond_text_temp("### ``TISS-Join results``\nFailed to detect any courses. :(")
58
59         request.cmd_respond_text_temp("### ``TISS-Join results``"+result)