Skip to content

Mattermost base64 thumbnails as snapshot #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion octoprint_Octoslack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down