From e62a11a5c76c5392ad536771a5e2bf8de61b201b Mon Sep 17 00:00:00 2001 From: Someone Date: Wed, 8 Dec 2021 19:38:38 +0100 Subject: [PATCH] [somebot] DialogManagedPrologFeedback --- modules/DialogManagedPrologFeedback.py | 144 +++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 modules/DialogManagedPrologFeedback.py diff --git a/modules/DialogManagedPrologFeedback.py b/modules/DialogManagedPrologFeedback.py new file mode 100644 index 0000000..9ce8c6c --- /dev/null +++ b/modules/DialogManagedPrologFeedback.py @@ -0,0 +1,144 @@ +# Mattermost Bot module. +# Copyright (c) 2016-2021 by Someone (aka. Jan Vales ) +# published under MIT-License + +from inspect import cleandoc +import os + + +from AbstractCommand import * +class DialogManagedPrologFeedback(AbstractCommand): + TRIGGER = "prolog-feedback" + CONFIG = {"display_name": "somebot-command", "auto_complete": True, + "auto_complete_hint": "Generate feedback-message", + } + USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Post request for feedback. [BOT_ADMIN]" + + + def __init__(self, team_id, datadir): + super().__init__(team_id) + self.datadir = datadir + + + def on_POST(self, request, data): + self._require_bot_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.) + + msg = cleandoc(""" + ## @all `Habts Feedback für uns bzgl. des Beginners' Events diese Woche?` + Wir hoffen ihr hattet genau so viel Spaß und einen tollen Abend, wie wir. :) + Man kann immer etwas besser machen und deshalb wollen wir euch explizit um Feedback bitten. + """) + + att = [{ + "text": "", + "actions": [{"name": "Schreibe/Bearbeite dein Feedback", "integration": {"url": self.URL+"/interactive"}}] + }] + + print(self.URL) + request.cmd_respond_text_chan(msg, {"attachments":att}) + + + def on_POST_interactive(self, request, data): + import pprint + print("on_POST_interactive") + pprint.pprint(data) + + filename_feedback_day1 = self.datadir+data["user_id"]+"-feedback_day1.txt" + feedback_day1 = "" + if os.path.isfile(filename_feedback_day1): + with open(filename_feedback_day1, "r") as f: + feedback_day1 = f.read() + filename_feedback_day2 = self.datadir+data["user_id"]+"-feedback_day2.txt" + feedback_day2 = "" + if os.path.isfile(filename_feedback_day2): + with open(filename_feedback_day2, "r") as f: + feedback_day2 = f.read() + filename_feedback_day3 = self.datadir+data["user_id"]+"-feedback_day3.txt" + feedback_day3 = "" + if os.path.isfile(filename_feedback_day3): + with open(filename_feedback_day3, "r") as f: + feedback_day3 = f.read() + + dialog = { + "callback_id": data["user_id"], + "title": "Feedback zu Fachschaftsevents dieser Prolog-Woche", + "submit_label":"Speichern", + "elements":[ + { + "display_name": "Feedback zum montägigen HowTUni-Fachschaftvortrag (Max. 3000 Zeichen)", + "placeholder": "Schreibe dein Feedback hier. Max. 3000 Zeichen.", + "name": "feedback-day1", + "type": "textarea", + "optional": True, + "default": feedback_day1 + },{ + "display_name": "Feedback zu den Kleingruppen-Sessions am Dienstag (Max. 3000 Zeichen)", + "placeholder": "Schreibe dein Feedback hier. Max. 3000 Zeichen.", + "name": "feedback-day2", + "type": "textarea", + "optional": True, + "default": feedback_day2 + },{ + "display_name": "Feedback zum Abhängen am Karlsplatz am Mittwoch (Max. 3000 Zeichen)", + "placeholder": "Schreibe dein Feedback hier. Max. 3000 Zeichen.", + "name": "feedback-day3", + "type": "textarea", + "help_text": "Du kannst dein Feedback im nachhinein jederzeit bearbeiten/leeren.", + "optional": True, + "default": feedback_day3 + } + ] + } + + request.respond(200, {}) + self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog) + + + def on_POST_dialog(self, request, data): + import pprint + print("on_POST_dialog") + pprint.pprint(data) + + msg = "``prolog-feedback``\n" + filename_feedback_day1 = self.datadir+data["callback_id"]+"-feedback_day1.txt" + if data["submission"]["feedback-day1"]: + feedback_day1 = data["submission"]["feedback-day1"].strip() + else: + feedback_day1 = "" + if feedback_day1 == "": + if os.path.isfile(filename_feedback_day1): + os.remove(filename_feedback_day1) + msg += "result-day1 result was deleted.\n" + else: + with open(filename_feedback_day1, "w") as f: + f.write(feedback_day1) + msg += "result-day1 result was stored.\n" + filename_feedback_day2 = self.datadir+data["callback_id"]+"-feedback_day2.txt" + if data["submission"]["feedback-day2"]: + feedback_day2= data["submission"]["feedback-day2"].strip() + else: + feedback_day2 = "" + if feedback_day2 == "": + if os.path.isfile(filename_feedback_day2): + os.remove(filename_feedback_day2) + msg += "result-day2 result was deleted.\n" + else: + with open(filename_feedback_day2, "w") as f: + f.write(feedback_day2) + msg += "result-day2 result was stored.\n" + filename_feedback_day3 = self.datadir+data["callback_id"]+"-feedback_day3.txt" + if data["submission"]["feedback-day3"]: + feedback_day3 = data["submission"]["feedback-day3"].strip() + else: + feedback_day3 = "" + if feedback_day3 == "": + if os.path.isfile(filename_feedback_day3): + os.remove(filename_feedback_day3) + msg += "result-day3 result was deleted.\n" + else: + with open(filename_feedback_day3, "w") as f: + f.write(feedback_day3) + msg += "result-day3 result was stored.\n" + + self.bot.debug_chan(msg) + request.respond(200, {}) -- 2.43.0