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
5 from inspect import cleandoc
10 # pylint: disable=wrong-import-position
11 from AbstractCommand import AbstractCommand
12 class DialogManagedGuess(AbstractCommand):
14 CONFIG = {"display_name": "somebot-command", "auto_complete": True,
15 "auto_complete_hint": "",
17 CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PRIVATE+"Opens dialog to edit/delete your guess."
20 def __init__(self, team_id, datadir):
21 super().__init__(team_id)
23 self.datadir = datadir
26 def on_POST(self, request, data):
27 #self._open_dialog(request, data)
28 #request.respond_cmd_temp("## :white_check_mark: Success! :)\n#### Opened dialog.")
29 request.respond_cmd_err("No guessing game running.")
32 # def dis_on_POST_interactive(self, request, data):
34 # print("on_POST_interactive")
37 #self._open_dialog(request, data)
41 def dis_on_POST_dialog(self, request, data):
43 # print("on_POST_dialog")
47 if data["submission"]["guess_count"] and int(data["submission"]["guess_count"]) < 0:
48 errors["guess_count"] = "Count must be positive."
50 if data["submission"]["guess_value"] and int(data["submission"]["guess_value"]) < 0:
51 errors["guess_value"] = "Value must be positive."
54 for f in glob.glob("data/guess/*-guess_count.txt"):
55 if data["callback_id"] in f:
57 with open(f, "r") as fd:
58 counts.append(int(fd.read()))
60 if int(data["submission"]["guess_count"]) in counts:
61 errors["guess_count"] = "Count must be unique. Somebody else already logged this guess before you."
64 for f in glob.glob("data/guess/*-guess_value.txt"):
65 if data["callback_id"] in f:
67 with open(f, "r") as fd:
68 values.append(int(fd.read()))
70 if int(data["submission"]["guess_value"]) in values:
71 errors["guess_value"] = "Value must be unique. Somebody else already logged this guess before you."
74 request.respond(200, {"errors": errors})
78 user = self.bot.api.get_user(data['user_id'])
79 self._response_store(self.datadir+data["callback_id"], "guess_count.txt", str(int(data["submission"]["guess_count"])), user["username"])
80 self._response_store(self.datadir+data["callback_id"], "guess_value.txt", str(int(data["submission"]["guess_value"])), user["username"])
82 request.respond(200, {})
85 def _open_dialog(self, request, data):
86 callback_id = "res-"+data["user_id"]+"-"
88 guess_count = self._response_load(self.datadir+callback_id, "guess_count.txt")
89 guess_value = self._response_load(self.datadir+callback_id, "guess_value.txt")
92 "callback_id": callback_id,
93 "title": "Guess the coins",
94 "submit_label":"Submit",
96 "display_name": "Count of all coins?",
97 "placeholder": "Count. Positive integer.",
98 "name": "guess_count",
102 "default": guess_count
104 "display_name": "Value of all coins?",
105 "placeholder": "EUR Value. Positive integer.",
106 "name": "guess_value",
110 "default": guess_value
114 self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)
117 def _response_load(self, path, filename, default=""):
120 if os.path.isfile(path):
121 with open(path, "r") as f:
126 def _response_store(self, path, filename, data, username=""):
128 data = str(data).strip()
129 if data == "" or data == "None" or data == "0":
130 if os.path.isfile(path):
132 self.bot.debug_chan("``guess::"+filename+"`` (**``@"+username+"``**) result deleted.")
134 with open(path, "w") as f:
136 self.bot.debug_chan("``guess::"+filename+"`` by (**``@"+username+"``**) result stored.")