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