From 787605e98bc74d3a73446fd7562a083e6a1e5c99 Mon Sep 17 00:00:00 2001 From: schweigel Date: Fri, 26 Mar 2021 14:14:19 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9E=95=20Added=20Thumbnails=20as=20Snaps?= =?UTF-8?q?hot=20for=20Mattermost?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- octoprint_Octoslack/__init__.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/octoprint_Octoslack/__init__.py b/octoprint_Octoslack/__init__.py index 2b37642..5291133 100644 --- a/octoprint_Octoslack/__init__.py +++ b/octoprint_Octoslack/__init__.py @@ -7,6 +7,7 @@ from slacker import Slacker, IncomingWebhook from imgurpython import ImgurClient from imgurpython.helpers.error import ImgurClientError, ImgurClientRateLimitError +from io import BytesIO from pushbullet import Pushbullet from pushover_complete import PushoverAPI from rocketchat.api import RocketChatAPI @@ -2744,7 +2745,7 @@ def get_formatting_elements(self): ##returns bold formatting str, key/value separator (often used when bold can't be used), newline connection_method = self.connection_method() if connection_method == "WEBHOOK" and self.mattermost_mode(): - return "**", "**", " ", "\n" + return "**", "**", " ", "\n\n" elif connection_method == "WEBHOOK" or connection_method == "APITOKEN": return "*", "*", " ", "\n" elif connection_method == "PUSHOVER": @@ -2938,6 +2939,28 @@ def execute_command(self, event, command, capture_output, command_rsp): command_rsp.put(command_output) command_rsp.put(error_msg) + def get_mattermost_thumbnail(self): + NEW_SIZE = (240, 160) + QUALITY = 50 + SIZETARGET = 5500 + + local_file_path, error_msgs = self.retrieve_snapshot_images() + if local_file_path == None: + return None, error_msgs + + image = Image.open(local_file_path) + image.thumbnail(NEW_SIZE, Image.ANTIALIAS) + + SIZE = SIZETARGET + 1 + while SIZE >= SIZETARGET and QUALITY >= 1 : + buffered = BytesIO() + image.save(buffered, format="JPEG", quality = QUALITY) + b64_string = base64.b64encode(buffered.getvalue()).decode('utf-8') + SIZE = len(b64_string) + QUALITY -= 1 + + return '![cam](data:image/jpg;base64,' + b64_string + ')', None + def send_slack_message( self, event, @@ -3414,6 +3437,12 @@ def send_slack_message( text += newline + " - " text += timelapse_error + if connection_method == "WEBHOOK" and self.mattermost_mode() and includeSnapshot and snapshot_upload_method == "NONE": + thumbnail, error_msg = self.get_mattermost_thumbnail() + if not error_msg == None: + self._logger.debug(error_msg) + text += newline + thumbnail + if not text == None and len(text) > 0: attachment["text"] = text From 6e973c8401ccb0ec3bddae729fecf3799b8bf5a6 Mon Sep 17 00:00:00 2001 From: schweigel Date: Fri, 26 Mar 2021 14:30:01 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=8F=20Comment=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- octoprint_Octoslack/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/octoprint_Octoslack/__init__.py b/octoprint_Octoslack/__init__.py index 5291133..2fff60a 100644 --- a/octoprint_Octoslack/__init__.py +++ b/octoprint_Octoslack/__init__.py @@ -2940,6 +2940,7 @@ def execute_command(self, event, command, capture_output, command_rsp): command_rsp.put(error_msg) def get_mattermost_thumbnail(self): + # Mattermost has limitation of 16383 characters for whole webhook, so the image size is reduced NEW_SIZE = (240, 160) QUALITY = 50 SIZETARGET = 5500 @@ -2959,7 +2960,7 @@ def get_mattermost_thumbnail(self): SIZE = len(b64_string) QUALITY -= 1 - return '![cam](data:image/jpg;base64,' + b64_string + ')', None + return '![snapshot](data:image/jpg;base64,' + b64_string + ')', None def send_slack_message( self,