diff --git a/octoprint_Octoslack/__init__.py b/octoprint_Octoslack/__init__.py index a6931c0..7dc8e73 100644 --- a/octoprint_Octoslack/__init__.py +++ b/octoprint_Octoslack/__init__.py @@ -6,6 +6,7 @@ from datetime import timedelta 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 @@ -2855,7 +2856,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": @@ -3049,6 +3050,29 @@ 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): + # Mattermost has limitation of 16383 characters for whole webhook, so the image size is reduced + 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 '![snapshot](data:image/jpg;base64,' + b64_string + ')', None + def send_slack_message( self, event, @@ -3525,6 +3549,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