]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandRoom.py
modules/WSDMNotSupportedNotification.py
[pub/jan/mattermost-bot.git] / modules / CommandRoom.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 #  published under MIT-License
4 #  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.
5
6 import requests
7
8
9 # pylint: disable=wrong-import-position
10 from AbstractCommand import AbstractCommand
11 class CommandRoom(AbstractCommand):
12     TRIGGER = "room"
13     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
14               "auto_complete_hint": "['--chan'] <room name>",
15              }
16     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Use TOSS to get room information."
17
18
19     def __init__(self, team_id, toss_url_api_search):
20         super().__init__(team_id)
21         self.toss_url_api_search = toss_url_api_search
22
23
24     def on_POST(self, request, data):
25         search = data["text"]
26         send_to_chan = False
27
28         if search.startswith("--chan "):
29             search = search.replace("--chan ", "", 1)
30             send_to_chan = True
31
32         if not search or search == "--chan":
33             request.cmd_respond_text_temp("You did not provide a room name.\n``/"+self.TRIGGER+" "+data["text"]+"``")
34             return
35
36         r = requests.get(self.toss_url_api_search+"?q="+requests.utils.quote(search))
37         if r.status_code != 200:
38             request.cmd_respond_text_temp(":stop_sign: TOSS-query failed. :(\n"+str(repr(r))+"\n``/"+self.TRIGGER+" "+data["text"]+"``")
39             return
40
41         msg = "#### ** [:toss: ``Results``](https://toss.fsinf.at/?q="+requests.utils.quote(search)+") ``(max 5)``**"
42         cnt = 0
43         for entry in r.json():
44             if entry["type"] == "room":
45                 cnt += 1
46                 if cnt > 5:
47                     break
48
49                 msg += "\n#### "+entry["name"]+" ("+entry["code"]+")\n"
50                 if entry["address"]:
51                     msg += ":world_map:[``"+entry["address"]+"``](https://www.google.com/maps/search/"+requests.utils.quote(entry["address"].split(";")[0])+")"
52                 if entry["description"]:
53                     msg += "\n"+entry["description"]
54                 msg += "\n\n----\n\n"
55
56         if cnt == 0:
57             request.cmd_respond_text_temp("#### **``No`` :toss: ``Results``** :(\n``/"+self.TRIGGER+" "+data["text"]+"``")
58         elif not send_to_chan:
59             request.cmd_respond_text_temp(msg+"\n``/"+self.TRIGGER+" "+data["text"]+"``")
60         else:
61             request.cmd_respond_text_chan(msg+"\n``/"+self.TRIGGER+" "+data["text"]+"``")