2 # Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 # published under MIT-License
6 class RequireException(Exception):
7 """Required precondition not met-exception. Mostly used for permission checks. Message will be forwarded to command-caller/user"""
10 class AbstractCommand():
14 CONFIG = {"display_name": "somebot-command", "auto_complete": True}
18 mm_secret_token = None
21 def __init__(self, team_id):
22 self.TEAM_ID = team_id
26 # return str(self.__class__)+" for team_id: "+str(self.TEAM_ID)
28 # return self.__str__()
31 # can/should be overridden by the user
32 def on_register(self):
33 """Consider to override. Handles the post-command-registration logic at bot startup."""
34 self._create_slash_command()
36 # can/should be overridden by the user
37 def on_shutdown(self):
38 """Consider to override. Handles the shutdown-procedure."""
41 # should be overridden by the user
42 def on_POST(self, request, data):
43 """Override. Handles the post-command logic."""
46 # should be overridden by the user
47 # manual command authentication needed!
48 def on_POST_interactive(self, request, data):
49 """Consider to override. Handles the interactive-message logic."""
52 # should be overridden by the user
53 # manual command authentication needed!
54 def on_POST_dialog(self, request, data):
55 """Consider to override. Handles the dialog logic."""
59 def _on_register(self, bot):
61 self.URL = self.bot.local_websrv_url+"/"+self.TEAM_ID+"/"+self.TRIGGER
63 self.bot.USETOPICS.setdefault("/"+self.TRIGGER, self.USEINFO)
67 def _on_shutdown(self):
71 def _on_POST(self, request, data):
73 self.on_POST(request, data)
74 except RequireException as ex:
75 request.cmd_respond_text_temp(str(ex))
78 def _on_POST_interactive(self, request, data):
80 self.on_POST_interactive(request, data)
81 except RequireException as ex:
82 request.cmd_respond_text_temp(str(ex))
85 def _on_POST_dialog(self, request, data):
87 self.on_POST_dialog(request, data)
88 except RequireException as ex:
89 request.cmd_respond_text_temp(str(ex))
92 def _create_slash_command(self):
93 # (possibly) delete old version of command
94 for command in self.bot.api.list_custom_slash_commands_per_team(self.TEAM_ID):
95 if command["url"] == self.URL or command["trigger"].lower() == self.TRIGGER.lower():
96 self.bot.api.delete_slash_command(command["id"])
98 # create slash command
99 res = self.bot.api.create_slash_command(self.TEAM_ID, self.TRIGGER.lower(), self.URL+"/command")
100 res.update(self.CONFIG)
101 self.bot.api.update_slash_command(res)
102 self.mm_secret_token = res["token"]
105 def _require_bot_admin(self, data):
107 Require exactly bot admin priviledges.
108 Throws RequireException if not priviledged.
110 if not data["user_id"] in self.bot.admin_ids:
111 raise RequireException("### Leave me alone. You are not by real dad.")
114 def _require_team_admin(self, data):
116 Require at least team admin priviledges.
117 Throws RequireException if not priviledged.
119 team_member = self.bot.api.get_team_member(data["team_id"], data["user_id"])
120 if "team_admin" not in team_member["roles"]:
121 raise RequireException("### You are not TEAM_ADMIN. :(")
124 def _require_channel_admin(self, data):
126 Require at least channel admin priviledges.
127 Throws RequireException if not priviledged.
129 raise RequireException("### Priviledge-check for CHANNEL_ADMIN not implemented/TODO, but required. yeah. nope.")