]> git.somenet.org - pub/jan/mattermost.git/blob - somebot/modules/CommandSpoilerAdv.py
[somebot] /spoiler-adv <post-permalink> <spoiler-text>
[pub/jan/mattermost.git] / somebot / modules / CommandSpoilerAdv.py
1 # Mattermost Bot.
2 #  Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 #  published under MIT-License
4
5
6
7 from AbstractCommand import *
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>",
12              }
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."
14
15
16     def on_POST(self, request, data):
17         msg_text = data['text'].strip().split(" ", 1)
18
19         if len(msg_text[1]) > 7000:
20             request.cmd_respond_text_temp("``/spoiler-adv`` failed: Spoiler-text must be smaller than 7000 characters :(\nYour text is: "+str(len(msg_text[1]))+" characters long.")
21             return
22
23         try:
24             splitpath = msg_text[0].strip().strip("/").split("/")
25             if splitpath[4] == "pl":
26                 pl_post_id = splitpath[5]
27         except:
28             request.cmd_respond_text_temp("``/spoiler-adv`` failed: The first parameter is not a valid post permalink. :(")
29             return
30
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) :(")
33             return
34
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? :(")
38             return
39
40         att = [{
41             "text": "This post contains spoilers.",
42             "actions": [{
43                 "name": ":ghost: show me the spoilers :ghost:",
44                 "integration": {"url": self.URL+"/interactive", "context": {"action": "show_spoiler", "spoiler":msg_text[1]}}
45                 }]
46             }]
47
48         post["props"].update(dict({"attachments":att}))
49
50         if msg_text[1] == "--remove":
51             del post["props"]["attachments"]
52
53         self.bot.api.patch_post(splitpath[5], post["message"], post["props"])
54         request.cmd_respond_text_temp("### ``Done.`` :)")
55
56
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"]})