From 51442f81c56c561815b4bed156c9963a6e3b7c30 Mon Sep 17 00:00:00 2001 From: Someone Date: Wed, 8 Dec 2021 19:38:38 +0100 Subject: [PATCH] [somebot] /room --- modules/CommandRoom.py | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 modules/CommandRoom.py diff --git a/modules/CommandRoom.py b/modules/CommandRoom.py new file mode 100644 index 0000000..5d639b2 --- /dev/null +++ b/modules/CommandRoom.py @@ -0,0 +1,60 @@ +# Mattermost Bot module. +# Copyright (c) 2016-2021 by Someone (aka. Jan Vales ) +# published under MIT-License +# previous implementation written 2019 by Ju was completely replaced by a modified previous CommandToss implementation by Someone 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'] ", + } + 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"]+"``") -- 2.43.0