]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/DialogManagedLVAFeedback.py
config.py.example
[pub/jan/mattermost-bot.git] / modules / DialogManagedLVAFeedback.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2016-2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 #  published under MIT-License
4
5 from inspect import cleandoc
6 import csv
7 import os
8
9
10 from AbstractCommand import *
11 class DialogManagedLVAFeedback(AbstractCommand):
12     TRIGGER = "lva-feedback"
13     CONFIG = {"display_name": "somebot-command", "auto_complete": False,
14               "auto_complete_hint": "",
15              }
16     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "preview-spams channel and asks for feedback. spam manuall!. [BOT_ADMIN]"
17
18
19     def __init__(self, team_id, datadir):
20         super().__init__(team_id)
21
22         self.datadir = datadir
23         self.channels = dict()
24
25         with open(self.datadir+'channels.csv', newline='') as csvfile:
26             self.channels = {row[0]:row for row in csv.reader(csvfile, delimiter=',', quotechar='"')}
27
28
29     def on_POST(self, request, data):
30         self._require_bot_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
31
32         msg = cleandoc("""
33             ## :pencil:``COVID course feedback 2.0!``:rocket:
34             The next term is going to be another distance-learning :dl: one.
35             We want your feedback in order to find out which courses need to be improved asap to achieve the excellence we are accustomed to, or better.
36             The feedback will be aggregated, anonymised and discussed with the deans of study affairs and possibly with the vice rector for teaching matter and/or the in-house lawyers.
37
38             #### :point_right: This Feedback form will run up until 2021-02-07 23:59 :point_left:
39
40             What we want to know:
41             + Feedback for this course.
42             + How did you like the modalities of this course? (Would you want it to be applied to others?)
43             + This course's modalities - describe them in a few words. (Also consider updating the info in VoWi - its a work resource for us and we use it to represent you)
44             + Feedback about this survey for :fsinf:. (This will not be shared outside of fsinf)
45
46             :information_source: *We are your legaly appointed representatives - we will never forward any personal info about who wrote what without explicit permission. But please dont make us read a list of profanities.* :)
47             """)
48
49         att = [{
50             "actions": [{"name": ":pencil: Give/edit your feedback", "integration": {"url": self.URL+"/interactive"}}]
51             }]
52
53         print(self.URL)
54         request.cmd_respond_text_chan(msg, {"attachments":att})
55
56
57     def on_POST_interactive(self, request, data):
58         #import pprint
59         #print("on_POST_interactive")
60         #pprint.pprint(data)
61
62 #        request.respond(200, {"ephemeral_text": "## ``The submission period ended. We are now evaluating the responses!`` :)"})
63
64         callback_id = "res-"+data["team_id"]+"-"+data["channel_id"]+"-"+data["user_id"]
65
66         feedback_course = self._response_load(self.datadir+callback_id, "-feedback_course.txt")
67         feedback_modus = self._response_load(self.datadir+callback_id, "-feedback_modus.txt")
68         feedback_modus_like = self._response_load(self.datadir+callback_id, "-feedback_modus_like.txt", "0")
69         feedback_fsinf = self._response_load(self.datadir+callback_id, "-feedback_fsinf.txt")
70
71         dialog = {
72             "callback_id": callback_id,
73             "title": "COVID Course feedback 2.0",
74             "submit_label":"Submit",
75             "elements":[{
76                 "display_name": "Your Comment/Feedback about the course (Max. 3000 characters)",
77                 "placeholder": "Type here. Max. 3000 characters.",
78                 "name": "feedback_course",
79                 "type": "textarea",
80                 "help_text": "We will aggregate and forward your feedback to the deans of study affairs, the vice rector for teaching and possibly others that are deemed able to fix something. You can edit/blank it later at any time.",
81                 "optional": True,
82                 "default": feedback_course
83             },{
84                 "display_name": "Describe the modalities in a few words (Max. 3000 characters)",
85                 "placeholder": "Type here. Max. 3000 characters.",
86                 "name": "feedback_modus",
87                 "type": "textarea",
88                 "help_text": "You can edit/blank it later at any time.",
89                 "optional": True,
90                 "default": feedback_modus
91             },{
92                 "display_name": "How did you like the modalities of this course? (Do you want it to be applied to others?)",
93                 "placeholder": "Type here. Max. 3000 characters.",
94                 "name": "feedback_modus_like",
95                 "type": "radio",
96                 "options": [
97                     {"text": "I loved it", "value": "2" },
98                     {"text": "I liked it", "value": "1" },
99                     {"text": "I'm indifferent/Don't want to answer (default)", "value": "0" },
100                     {"text": "I disliked it", "value": "-1" },
101                     {"text": "I hated it", "value": "-2" },
102                 ],
103                 "help_text": "You can edit it later at any time.",
104                 "optional": True,
105                 "default": feedback_modus_like
106             },{
107                 "display_name": "Feedback (about this survey) for FSInf (Max. 3000 characters)",
108                 "placeholder": "Type here. Max. 3000 characters.",
109                 "name": "feedback_fsinf",
110                 "type": "textarea",
111                 "help_text": "We will not forward this text to anyone. You can edit/blank it later at any time.",
112                 "optional": True,
113                 "default": feedback_fsinf
114             }]
115         }
116
117         request.respond(200, {})
118         self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)
119
120
121     def on_POST_dialog(self, request, data):
122         #import pprint
123         #print("on_POST_dialog")
124         #pprint.pprint(data)
125
126         c = self.bot.api.get_channel(data["channel_id"])
127
128         self._response_store(self.datadir+data["callback_id"], "-feedback_course.txt", data["submission"]["feedback_course"], c["name"])
129         self._response_store(self.datadir+data["callback_id"], "-feedback_modus.txt", data["submission"]["feedback_modus"], c["name"])
130         self._response_store(self.datadir+data["callback_id"], "-feedback_modus_like.txt", data["submission"]["feedback_modus_like"], c["name"])
131         self._response_store(self.datadir+data["callback_id"], "-feedback_fsinf.txt", data["submission"]["feedback_fsinf"], c["name"])
132
133         request.respond(200, {})
134
135
136     def _response_load(self, path, filename, default=""):
137         path = path+filename
138         ret = default
139         if os.path.isfile(path):
140             with open(path, "r") as f:
141                 ret = f.read()
142         return ret
143
144
145     def _response_store(self, path, filename, data, channelname=""):
146         path = path+filename
147         data = str(data).strip()
148         if data == "" or data == "None" or data == "0":
149             if os.path.isfile(path):
150                 os.remove(path)
151                 self.bot.debug_chan("``lva-feedback::"+channelname+filename+"`` result deleted.")
152         else:
153             with open(path, "w") as f:
154                 f.write(data)
155             self.bot.debug_chan("``lva-feedback::"+channelname+filename+"`` result stored.\n```\n"+data+"\n```")