Skip to content

Commit

Permalink
Merge pull request #2 from glean-notes/fix-slack-notification-with-im…
Browse files Browse the repository at this point in the history
…ages

Fix slack notification with images
  • Loading branch information
chriswardell-glean authored Apr 16, 2024
2 parents 5b9f7d1 + eb14aa2 commit 5b270fd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM python:3.11-alpine

ADD send-slack.py /send-slack.py

RUN pip install slack_sdk

COPY send-slack.py /send-slack.py

ENTRYPOINT ["python", "/send-slack.py"]
18 changes: 17 additions & 1 deletion send-slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from slack_sdk.errors import SlackApiError
import os
import sys
import urllib.request

channels_list = {}

Expand Down Expand Up @@ -58,12 +59,27 @@ def main():
# Send the slack message, if it fails an exception will fire
client.chat_postMessage(
channel=channel_id,
attachments=[{"text": message_content, "image_url": os.getenv("IMAGE_URL", "")}],
text=message_content,
username=pipeline_name,
icon_url=slack_icon,
unfurl_links=True,
unfurl_media=True,
)

image_path = os.getenv("IMAGE_PATH")
if image_path:
with open(image_path, "rb") as f:
data = f.read()
print("Getting upload URL")
upload_response = client.files_getUploadURLExternal(filename="image", length=len(data))
print("Uploading image")
request = urllib.request.Request(url=upload_response["upload_url"], data=data, method="POST")
urllib.request.urlopen(request)
print("Completing upload")
client.files_completeUploadExternal(
files=[{"id": upload_response["file_id"], "title": "image"}],
channel_id=channel_id,
)
print("Message sent.")
except SlackApiError as e:
print(f"Error posting message: {e}")
Expand Down

0 comments on commit 5b270fd

Please sign in to comment.