[somebot] /room
authorSomeone <someone@somenet.org>
Wed, 8 Dec 2021 18:38:38 +0000 (19:38 +0100)
committerSomeone <someone@somenet.org>
Wed, 8 Dec 2021 18:38:38 +0000 (19:38 +0100)
modules/CommandRoom.py [new file with mode: 0644]

diff --git a/modules/CommandRoom.py b/modules/CommandRoom.py
new file mode 100644 (file)
index 0000000..5d639b2
--- /dev/null
@@ -0,0 +1,60 @@
+# Mattermost Bot module.
+#  Copyright (c) 2016-2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
+#  published under MIT-License
+#  previous implementation written 2019 by Ju <daju@fsinf.at> was completely replaced by a modified previous CommandToss implementation by Someone <someone@somenet.org> in 2020.
+
+import requests
+
+
+from AbstractCommand import AbstractCommand
+class CommandRoom(AbstractCommand):
+    TRIGGER = "room"
+    CONFIG = {"display_name": "somebot-command", "auto_complete": True,
+              "auto_complete_hint": "['--chan'] <room name>",
+             }
+    USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Use TOSS to get room information."
+
+
+    def __init__(self, team_id, toss_url_api_search):
+        super().__init__(team_id)
+        self.toss_url_api_search = toss_url_api_search
+
+
+    def on_POST(self, request, data):
+        search = data["text"]
+        send_to_chan = False
+
+        if search.startswith("--chan "):
+            search = search.replace("--chan ", "", 1)
+            send_to_chan = True
+
+        if not search or search == "--chan":
+            request.cmd_respond_text_temp("You did not provide a room name.\n``/"+self.TRIGGER+" "+data["text"]+"``")
+            return
+
+        r = requests.get(self.toss_url_api_search+"?q="+requests.utils.quote(search))
+        if r.status_code != 200:
+            request.cmd_respond_text_temp(":stop_sign: TOSS-query failed. :(\n"+str(repr(r))+"\n``/"+self.TRIGGER+" "+data["text"]+"``")
+            return
+
+        msg = "#### ** [:toss: ``Results``](https://toss.fsinf.at/?q="+requests.utils.quote(search)+") ``(max 5)``**"
+        cnt = 0
+        for entry in r.json():
+            if entry["type"] == "room":
+                cnt += 1
+                if cnt > 5:
+                    break
+
+                msg += "\n#### "+entry["name"]+" ("+entry["code"]+")\n"
+                if entry["address"]:
+                    msg += ":world_map:[``"+entry["address"]+"``](https://www.google.com/maps/search/"+requests.utils.quote(entry["address"].split(";")[0])+")"
+                if entry["description"]:
+                    msg += "\n"+entry["description"]
+                msg += "\n\n----\n\n"
+
+        if cnt == 0:
+            request.cmd_respond_text_temp("#### **``No`` :toss: ``Results``** :(\n``/"+self.TRIGGER+" "+data["text"]+"``")
+        elif not send_to_chan:
+            request.cmd_respond_text_temp(msg+"\n``/"+self.TRIGGER+" "+data["text"]+"``")
+        else:
+            request.cmd_respond_text_chan(msg+"\n``/"+self.TRIGGER+" "+data["text"]+"``")