Skip to content
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
18 changes: 13 additions & 5 deletions incubating/slack-post-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
9 changes: 7 additions & 2 deletions incubating/slack-post-channel/step.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ kind: step-type
version: '1.0'
metadata:
name: slack-post-channel
version: 0.0.6
version: 0.0.7
title: Send a Slack message to a channel
isPublic: true
description: Send a message to a named Slack channel (instead of using a webhook URL)
Expand Down Expand Up @@ -51,7 +51,7 @@ spec:
},
"SLACK_IMAGE_VERSION": {
"type": "string",
"default": "0.0.6",
"default": "0.0.7",
"description": "Version (tag) of the slack-post-channel image to use."
},
"SLACK_TOKEN": {
Expand All @@ -67,6 +67,11 @@ spec:
"description": "The message to send to the channel. Use <@ID> to tag a user. Check https://api.slack.com/reference/surfaces/formatting for details.",
"default": "Message sent from Codefresh was not defined explicitely."
},
"SLACK_ATTACHMENTS": {
"type": "string",
"description": "Attachments to send. Documentation https://api.slack.com/docs/message-attachments",
"default": ""
},
"LOG_LEVEL": {
"type": "string",
"description": "Set log level, default info",
Expand Down