Skip to content
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
18 changes: 13 additions & 5 deletions workflows/slack/versions/0.0.2/images/post-to-channel/lib/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
import os
import sys
import logging
import json
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

def main():
log_format = "%(asctime)s:%(levelname)s:%(name)s.%(funcName)s: %(message)s"
logging.basicConfig(format = log_format, level = os.environ['LOG_LEVEL'].upper())

channel=os.getenv('SLACK_CHANNEL')
message=os.getenv('SLACK_MESSAGE', "")
token =os.getenv('SLACK_TOKEN')
channel =os.getenv('SLACK_CHANNEL')
message =os.getenv('SLACK_MESSAGE', "")
token =os.getenv('SLACK_TOKEN')
attachments =os.getenv('SLACK_ATTACHMENTS', "")

if ( channel == None ):
logging.error("SLACK_CHANNEL is not defined")
Expand All @@ -25,6 +27,13 @@ def main():
logging.error("SLACK_TOKEN is not defined")
sys.exit(1)

if ( attachments != "" ):
try:
attachments = json.loads(attachments)
except ValueError as e:
logging.error(f"Error decoding attachments: {e}")
sys.exit(3)

logging.info("Connecting to Slack")
client = WebClient(token=token)

Expand All @@ -41,8 +50,7 @@ def main():
sys.exit(3)

try:
response = client.chat_postMessage(channel=channel, text=message)
assert response["message"]["text"] == message
response = client.chat_postMessage(channel=channel, text=message, attachments=attachments)
except SlackApiError as e:
# You will get a SlackApiError if "ok" is False
assert e.response["ok"] is False
Expand Down