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