]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/DialogManagedPrologFeedback.py
modules/CommandShouldITUIt.py
[pub/jan/mattermost-bot.git] / modules / DialogManagedPrologFeedback.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
5 from inspect import cleandoc
6 import os
7
8
9 # pylint: disable=wrong-import-position
10 from AbstractCommand import AbstractCommand
11 class DialogManagedPrologFeedback(AbstractCommand):
12     TRIGGER = "prolog-feedback"
13     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
14               "auto_complete_hint": "Generate feedback-message",
15              }
16     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Post request for feedback. [BOT_ADMIN]"
17
18
19     def __init__(self, team_id, datadir):
20         super().__init__(team_id)
21         self.datadir = datadir
22
23
24     def on_POST(self, request, data):
25         self._require_bot_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
26
27         msg = cleandoc("""
28             ## @all `Habts Feedback für uns bzgl. des Beginners' Events diese Woche?`
29             Wir hoffen ihr hattet genau so viel Spaß und einen tollen Abend, wie wir. :)
30             Man kann immer etwas besser machen und deshalb wollen wir euch explizit um Feedback bitten.
31             """)
32
33         att = [{
34             "text": "",
35             "actions": [{"name": "Schreibe/Bearbeite dein Feedback", "integration": {"url": self.URL+"/interactive"}}]
36             }]
37
38         print(self.URL)
39         request.cmd_respond_text_chan(msg, {"attachments":att})
40
41
42     def on_POST_interactive(self, request, data):
43         import pprint
44         print("on_POST_interactive")
45         pprint.pprint(data)
46
47         filename_feedback_day1 = self.datadir+data["user_id"]+"-feedback_day1.txt"
48         feedback_day1 = ""
49         if os.path.isfile(filename_feedback_day1):
50             with open(filename_feedback_day1, "r") as f:
51                 feedback_day1 = f.read()
52         filename_feedback_day2 = self.datadir+data["user_id"]+"-feedback_day2.txt"
53         feedback_day2 = ""
54         if os.path.isfile(filename_feedback_day2):
55             with open(filename_feedback_day2, "r") as f:
56                 feedback_day2 = f.read()
57         filename_feedback_day3 = self.datadir+data["user_id"]+"-feedback_day3.txt"
58         feedback_day3 = ""
59         if os.path.isfile(filename_feedback_day3):
60             with open(filename_feedback_day3, "r") as f:
61                 feedback_day3 = f.read()
62
63         dialog = {
64             "callback_id": data["user_id"],
65             "title": "Feedback zu Fachschaftsevents dieser Prolog-Woche",
66             "submit_label":"Speichern",
67             "elements":[
68                 {
69                 "display_name": "Feedback zum montägigen HowTUni-Fachschaftvortrag (Max. 3000 Zeichen)",
70                 "placeholder": "Schreibe dein Feedback hier. Max. 3000 Zeichen.",
71                 "name": "feedback-day1",
72                 "type": "textarea",
73                 "optional": True,
74                 "default": feedback_day1
75                 },{
76                 "display_name": "Feedback zu den Kleingruppen-Sessions am Dienstag (Max. 3000 Zeichen)",
77                 "placeholder": "Schreibe dein Feedback hier. Max. 3000 Zeichen.",
78                 "name": "feedback-day2",
79                 "type": "textarea",
80                 "optional": True,
81                 "default": feedback_day2
82                 },{
83                 "display_name": "Feedback zum Abhängen am Karlsplatz am Mittwoch (Max. 3000 Zeichen)",
84                 "placeholder": "Schreibe dein Feedback hier. Max. 3000 Zeichen.",
85                 "name": "feedback-day3",
86                 "type": "textarea",
87                 "help_text": "Du kannst dein Feedback im nachhinein jederzeit bearbeiten/leeren.",
88                 "optional": True,
89                 "default": feedback_day3
90                 }
91             ]
92         }
93
94         request.respond(200, {})
95         self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)
96
97
98     def on_POST_dialog(self, request, data):
99         import pprint
100         print("on_POST_dialog")
101         pprint.pprint(data)
102
103         msg = "``prolog-feedback``\n"
104         filename_feedback_day1 = self.datadir+data["callback_id"]+"-feedback_day1.txt"
105         if data["submission"]["feedback-day1"]:
106             feedback_day1 = data["submission"]["feedback-day1"].strip()
107         else:
108             feedback_day1 = ""
109         if feedback_day1 == "":
110             if os.path.isfile(filename_feedback_day1):
111                 os.remove(filename_feedback_day1)
112                 msg += "result-day1 result was deleted.\n"
113         else:
114             with open(filename_feedback_day1, "w") as f:
115                 f.write(feedback_day1)
116             msg += "result-day1 result was stored.\n"
117         filename_feedback_day2 = self.datadir+data["callback_id"]+"-feedback_day2.txt"
118         if data["submission"]["feedback-day2"]:
119             feedback_day2= data["submission"]["feedback-day2"].strip()
120         else:
121             feedback_day2 = ""
122         if feedback_day2 == "":
123             if os.path.isfile(filename_feedback_day2):
124                 os.remove(filename_feedback_day2)
125                 msg += "result-day2 result was deleted.\n"
126         else:
127             with open(filename_feedback_day2, "w") as f:
128                 f.write(feedback_day2)
129             msg += "result-day2 result was stored.\n"
130         filename_feedback_day3 = self.datadir+data["callback_id"]+"-feedback_day3.txt"
131         if data["submission"]["feedback-day3"]:
132             feedback_day3 = data["submission"]["feedback-day3"].strip()
133         else:
134             feedback_day3 = ""
135         if feedback_day3 == "":
136             if os.path.isfile(filename_feedback_day3):
137                 os.remove(filename_feedback_day3)
138                 msg += "result-day3 result was deleted.\n"
139         else:
140             with open(filename_feedback_day3, "w") as f:
141                 f.write(feedback_day3)
142             msg += "result-day3 result was stored.\n"
143
144         self.bot.debug_chan(msg)
145         request.respond(200, {})