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
7 from AbstractCommand import AbstractCommand
8 class CommandSpoilerAdv(AbstractCommand):
9 TRIGGER = "spoiler-adv"
10 CONFIG = {"display_name": "somebot-command", "auto_complete": True,
11 "auto_complete_hint": "<post permalink> <spoiler-text>",
13 USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Attach/replace spoiler-text on any of your messages. (--remove to remove) Must be able to edit said message."
16 def on_POST(self, request, data):
17 msg_text = data['text'].strip().split(" ", 1)
19 if len(msg_text[1]) > 15000:
20 request.cmd_respond_text_temp("``/spoiler-adv`` failed: Spoiler-text must be smaller than 15000 characters :(\nYour text is: "+str(len(msg_text[1]))+" characters long.")
24 splitpath = msg_text[0].strip().strip("/").split("/")
25 if splitpath[4] == "pl":
26 pl_post_id = splitpath[5]
28 request.cmd_respond_text_temp("``/spoiler-adv`` failed: The first parameter is not a valid post permalink. :(")
31 if len(msg_text) == 1:
32 request.cmd_respond_text_temp("``/spoiler-adv`` failed: It seems like you did not supply any spoiler-text. (use --remove to remove a spoiler text) :(")
35 post = self.bot.api.get_post(pl_post_id)
36 if post['user_id'] != data['user_id']:
37 request.cmd_respond_text_temp("``/spoiler-adv`` failed: Is the permalinked post yours? :(")
41 "text": "This post contains spoilers.",
43 "name": ":ghost: show me the spoilers :ghost:",
44 "integration": {"url": self.URL+"/interactive", "context": {"action": "show_spoiler", "spoiler":msg_text[1]}}
48 post["props"].update(dict({"attachments":att}))
50 if msg_text[1] == "--remove":
51 del post["props"]["attachments"]
53 self.bot.api.patch_post(splitpath[5], props=post["props"])
54 request.cmd_respond_text_temp("### ``Done.`` :)")
57 def on_POST_interactive(self, request, data):
58 if data["context"]["action"] == "show_spoiler":
59 request.respond(200, {"skip_slack_parsing":True, "ephemeral_text": data["context"]["spoiler"]})