Skip to content

Commit

Permalink
Merge pull request #21 from ynput/chore/AY-6742_Convert-Slack-uploadF…
Browse files Browse the repository at this point in the history
…ile-to-new-api

Convert slack upload file to new api
  • Loading branch information
kalisp authored Sep 20, 2024
2 parents d7f33ba + 514f5fd commit 403493e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
42 changes: 23 additions & 19 deletions client/ayon_slack/plugins/publish/integrate_slack_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ def process(self, instance):
groups = slack_ids["groups"]
message = self._translate_users(message, users, groups)

msg_id, file_ids = client.send_message(channel,
message,
publish_files)
client.send_message(channel, message, publish_files)

def _handle_review_upload(self, message, message_profile, publish_files,
review_path):
Expand Down Expand Up @@ -337,25 +335,14 @@ def get_users_and_groups(self):
def send_message(self, channel, message, publish_files):
from slack_sdk.errors import SlackApiError
try:
attachment_str = "\n\n Attachment links: \n"
file_ids = []
for published_file in publish_files:
response = self.client.files_upload(
file=published_file,
filename=os.path.basename(published_file))
attachment_str += "\n<{}|{}>".format(
response["file"]["permalink"],
os.path.basename(published_file))
file_ids.append(response["file"]["id"])
attachments = self._upload_attachments(publish_files)

if publish_files:
message += attachment_str
message = self._add_attachments(attachments, message)

response = self.client.chat_postMessage(
self.client.chat_postMessage(
channel=channel,
text=message
)
return response.data["ts"], file_ids
except SlackApiError as e:
# # You will get a SlackApiError if "ok" is False
if e.response.get("error"):
Expand All @@ -368,7 +355,25 @@ def send_message(self, channel, message, publish_files):
error_str = self._enrich_error(str(e), channel)
self.log.warning("Not SlackAPI error", exc_info=True)

return None, []
def _upload_attachments(self, publish_files):
"""Returns list of permalinks to uploaded files"""
file_urls = []
for published_file in publish_files:
with open(published_file, "rb") as f:
uploaded_file = self.client.files_upload_v2(
filename=os.path.basename(published_file),
file=f
)
file_urls.append(uploaded_file.get("file").get("permalink"))

return file_urls

def _add_attachments(self, attachments, message):
"""Add permalink urls to message without displaying url."""
for permalink_url in attachments:
# format extremely important!
message += f"<{permalink_url}| >"
return message


class SlackPython2Operations(AbstractSlackOperations):
Expand Down Expand Up @@ -443,4 +448,3 @@ def send_message(self, channel, message, publish_files):
self.log.warning("Error happened: {}".format(error_str),
exc_info=True)

return None, []
2 changes: 1 addition & 1 deletion client/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"

[tool.poetry.dependencies]
python = "^3.9"
slack-sdk = "^3.6.0"
slack-sdk = "^3.19.5"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down

0 comments on commit 403493e

Please sign in to comment.