[somebot] /clp ["--yes"|"--meme"]
authorSomeone <someone@somenet.org>
Wed, 8 Dec 2021 18:38:38 +0000 (19:38 +0100)
committerSomeone <someone@somenet.org>
Wed, 8 Dec 2021 18:38:38 +0000 (19:38 +0100)
modules/CommandCoronaPresenceLogger.py [new file with mode: 0644]

diff --git a/modules/CommandCoronaPresenceLogger.py b/modules/CommandCoronaPresenceLogger.py
new file mode 100644 (file)
index 0000000..fb3a405
--- /dev/null
@@ -0,0 +1,84 @@
+# Mattermost Bot module.
+#  Copyright (c) 2016-2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
+#  Copyright (c) 2020 by Sabrina -- minor rewording of the message
+#  published under MIT-License
+
+import datetime
+import random
+import re
+import threading
+from inspect import cleandoc
+
+from webdav3.client import Client
+
+
+from AbstractCommand import AbstractCommand
+class CommandCoronaPresenceLogger(AbstractCommand):
+    TRIGGER = "cpl"
+    CONFIG = {"display_name": "somebot-command", "auto_complete": False}
+    CONFIG["description"] = "Corona-presence-logging"
+    USEINFO = cleandoc("""
+        ``/cpl`` is our approach to corona related presence logging at fsinf.
+        Use ``/cpl --yes`` to get your presence at fsinf today logged.
+        Consider using this command if you stay longer than a few seconds at fsinf (grabbing a Drink for an exam).
+
+        This information is retained for about a month.
+
+        If we get informed of somebody testing positive for COVID-19, we shall inform anyone who was at fsinf on the same days.
+        If you shall test positive for COVID-19, please inform us, so we can inform anyone who was at fsinf on the same days as you.
+        """)
+
+
+    def __init__(self, team_id, datadir, dav_options=None):
+        super().__init__(team_id)
+        self.datadir = datadir
+        self.dav_options = dav_options
+
+
+    def on_POST(self, request, data):
+        data['text'] = data['text'].strip()
+
+        if re.match(r"^(-|—|–)+meme$", data["text"]) and self.dav_options:
+            self.channel_index_th = threading.Timer(1.0, self._upload_meme, [request, data])
+            self.channel_index_th.setName("CommandCoronaPresenceLogger::upload_meme()")
+            self.channel_index_th.setDaemon(True)
+            self.channel_index_th.start()
+
+            request.cmd_respond_text_chan("``AUTODELETE-DAY`` **``CPL, OIDA!``**", props={"cpl-meme-upload-todo":"todo"})
+
+        # accept all.
+        #elif re.match(r"^(-|—|–)+y(e|a+)s$", data['text']):
+        if re.match(r"^(-|—|–)+", data['text']):
+            filename = self.datadir + datetime.date.today().isoformat()+"--"+data["user_id"]
+            with open(filename, "a") as f:
+                f.write(data["user_name"]+" -- "+datetime.datetime.now().isoformat()+"\n")
+                request.cmd_respond_text_temp(cleandoc("""
+                    ## :white_check_mark: ``Your presence at FSINF today has been logged.`` :white_check_mark:
+                    This information is retained for about a month.
+
+                    If we get informed of somebody testing COVID-19 positive, we shall inform anyone being at fsinf on the same days.
+                    If you shall test COVID-19 positive, please inform us, so we can inform anyone who was at fsinf on the same days as you.
+                    """))
+                self.bot.api.add_user_to_channel("m5sik9jwmib6mfpnpcmsib1h7w", data["user_id"])
+                self.bot.debug_chan("``/cpl`` presence logged: ``"+data["user_name"]+"``")
+
+        else:
+            request.cmd_respond_text_temp("## :warning: Please use ``/cpl --yes`` to confirm your presence at fsinf. :warning: \n\n"+self.USEINFO)
+
+
+    # racecondition if called in the same channel too fast. nonissue?
+    def _upload_meme(self, request, data):
+            post_cnt = 0
+            for p in self.bot.api.get_posts_for_channel(data["channel_id"]):
+                post_cnt += 1
+                if post_cnt > 20:
+                    break
+
+                if "cpl-meme-upload-todo" in p["props"] and p["props"]["cpl-meme-upload-todo"] == "todo":
+                    client = Client(self.dav_options)
+                    file_list = client.list()
+                    del file_list[0]
+                    client.download_file(random.choice(file_list), '/tmp/cpl-meme.png')
+
+                    self.bot.api.create_post(p["channel_id"], '``AUTODELETE-DAY`` **``CPL, OIDA!``**', props={"from_webhook":"true"}, filepaths=['/tmp/cpl-meme.png'], root_id=p["root_id"])
+                    self.bot.api.delete_post(p["id"])