1 # Mattermost APIv4 Bot.
2 # written 2019 by Ju <daju@fsinf.at>
10 # pylint: disable=wrong-import-position
11 from AbstractCommand import AbstractCommand
12 class CommandRaumsuche(AbstractCommand):
14 CONFIG = {"display_name": "somebot-command", "auto_complete": True,
15 "auto_complete_hint": "Name",
17 USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Tries to find the room with given name."
19 # maximum results/rooms listed
22 WIKI_URL = "wiki.fsinf.at"
23 # link to page with all rooms
24 WIKI_URL_ROOMS = "https://wiki.fsinf.at/wiki/Kategorie:Raumcode"
25 # wiki-login (optional)
27 #MWS_PASS = "PASSWORD"
28 # osm - experimental - uncomment to deactivate
29 OSM_URL = "https://www.openstreetmap.org/search?query="
30 # OSWM link with coordinates - uncomment to deactivate
31 OSM_COORDINATES = "https://www.openstreetmap.org/?"
33 FEEDBACK = "\n\nFeedback zu diesem Feature bitte an **[@ju](https://mattermost.fsinf.at/fsinf-intern/messages/@ju)** =^.^="
35 HELP_TEXT = "Folgende Tipps können dir bei der Suche helfen:\n"
37 HELP_TEXT += "+ die Suche ist nicht case sensitiv\n"
38 HELP_TEXT += "+ Verwende 'EI 7' statt 'EI7'\n"
39 HELP_TEXT += "+ sollte ein Raum nicht eingetragen sein, oder der Suchbegriff nicht passen, schau auf [diese Seite](https://wiki.fsinf.at/wiki/Raum:Hauptseite) und trage ihn nach. Das Wiki lebt von eurer Hilfe."
43 def __init__(self, team_id, mw_user=None, mw_user_pw=None):
44 super().__init__(team_id)
45 self.mw_user = mw_user
46 self.mw_user_pw = mw_user_pw
49 def on_POST(self, request, data):
52 if data["text"].strip() != "" and data["text"].replace("--chan ", "").strip() != "":
53 if data["text"].startswith("--chan "):
54 msg = self.roomSearch(data["text"].replace("--chan ", ""))
55 request.cmd_respond_text_chan(msg)
58 msg = self.roomSearch(data["text"])
59 request.cmd_respond_text_temp(msg)
62 request.cmd_respond_text_temp("Du hast vergessen einen Raumnamen anzugeben.\n\n" + self.HELP_TEXT)
66 def roomSearch(self, searchTerm):
67 searchTerm = searchTerm.strip()
68 if searchTerm == "help" or searchTerm == "?":
71 # a try to prevent injections
72 searchTerm = re.sub("[^\w]+", " ", searchTerm)
73 searchTerm = searchTerm.replace(" ", " ").strip()
75 # space to * for matching "HS 11" to "HS 11" and "HS11"
76 searchTerm = searchTerm.replace(" ", "*")
77 query="[[Category:Raumcode]]|[[RoomName::~*" + searchTerm + "*]] OR [[SearchValue::~*" + searchTerm + "*]] OR [[RoomCode::~*" + searchTerm + "*]]|?RoomName|?Address|?RoomCode|?Longitude|?Latitude"
78 result = "Du hast nach **`" + searchTerm + "`** gesucht."
80 # check if service is available, if so execute query
82 mws = mwclient.Site(self.WIKI_URL, path="/", retry_timeout=120)
84 return "Der Service ist gerade nicht verfügbar (zB keine Verbindung zum wiki)."
86 #use user + pass for wiki connection if it is set
87 if self.mw_user and self.mw_user_pw:
88 mws.login(self.mw_user, self.mw_user_pw)
89 # TODO: wrong credentials behandeln
92 query_result = mws.ask(query)
95 # https://stackoverflow.com/a/21525143
97 if next(query_result, _exhausted) == _exhausted:
98 return result + " Leider konnten keine Ergebnisse gefunden werden.\n\n" + self.HELP_TEXT
100 result += " Folgende Räume wurden gefunden:\n"
101 # get items again, because they get consumed in the isEmpty
102 query_result = mws.ask(query)
105 for p in query_result:
106 if counter < self.MAX_RESULTS:
108 # check if values are present
109 if len(next(iter(p.values()))["printouts"]["RoomCode"]) > 0:
110 roomCode = next(iter(p.values()))["printouts"]["RoomCode"][0]
113 if len(next(iter(p.values()))["printouts"]["RoomName"]) > 0:
114 roomName = next(iter(p.values()))["printouts"]["RoomName"][0]
117 if len(next(iter(p.values()))["printouts"]["Address"]) > 0:
118 address = next(iter(p.values()))["printouts"]["Address"][0]
121 if len(next(iter(p.values()))["printouts"]["Longitude"]) > 0:
122 longitude = next(iter(p.values()))["printouts"]["Longitude"][0]["fulltext"]
125 if len(next(iter(p.values()))["printouts"]["Latitude"]) > 0:
126 latitude = next(iter(p.values()))["printouts"]["Latitude"][0]["fulltext"]
130 url = next(iter(p.values()))["fullurl"]
132 result += "\n+ [" + roomCode + "](" + url + "), **" + roomName + "** findest du hier: "
135 # priority over "OSM_URL":
136 if "OSM_COORDINATES" in dir(self) and len(latitude) > 0 and len(longitude) > 0:
137 # mlat={{{latitude}}}&mlon={{{longitude}}}#map=19/{{{latitude}}}/{{{longitude}}}
138 osm_query = "mlat=" + latitude + "&mlon=" + longitude + "#map=19/" + latitude + "/" + longitude
139 result += "[" + address + "](" + self.OSM_COORDINATES + osm_query + " \"link to open street maps\")"
140 # link without coordinates:
141 elif "OSM_URL" in dir(self):
142 # case format "1040 Wien, Favoritenstraße 9; durch Gittertor und Hof zu Stiege II; ins 2. OG; vom Lift aus rechts;"
143 if "wien" in address.split(",")[0].lower():
144 osm_query = "wien%20" + address.split(";")[0].split(",")[1]
145 # case format "Floragasse 7, 2. Stock"
147 osm_query = "wien%20" + address.split(",")[0]
148 # transforms space to %20
149 osm_query = osm_query.replace(" ", "%20")
150 # transforms street number "22-25" to "22"
151 osm_query = re.sub(r"-\d+", r"", osm_query)
153 result += "[" + address + "](" + self.OSM_URL + osm_query + ")"
154 # else print plain address
161 result += "\n\nSollte der Raum nicht dabei sein, kannst du ihn unter [Wiki-Räume](" + self.WIKI_URL_ROOMS + ") nachtragen bzw. einen Suchbegriff hinzufügen." + self.FEEDBACK
166 if __name__ == "__main__":
167 a = CommandRaumsuche("asdf")
168 print(a.roomSearch(sys.argv[1]))