Skip to content
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

Convert slack upload file to new api #21

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
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
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