Skip to content

Commit d23a436

Browse files
authored
remove message assert as it fails when using interpolation and add SLACK_ATTACHMENTS (#481)
* remove message assert as it fails when using interpolation * add optional arg SLACK_ATTACHMENTS * bump default image version to 0.0.7
1 parent a876246 commit d23a436

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

incubating/slack-post-channel/lib/slack.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
import os
77
import sys
88
import logging
9+
import json
910
from slack_sdk import WebClient
1011
from slack_sdk.errors import SlackApiError
1112

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

16-
channel=os.getenv('SLACK_CHANNEL')
17-
message=os.getenv('SLACK_MESSAGE', "")
18-
token =os.getenv('SLACK_TOKEN')
17+
channel =os.getenv('SLACK_CHANNEL')
18+
message =os.getenv('SLACK_MESSAGE', "")
19+
token =os.getenv('SLACK_TOKEN')
20+
attachments =os.getenv('SLACK_ATTACHMENTS', "")
1921

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

30+
if ( attachments != "" ):
31+
try:
32+
attachments = json.loads(attachments)
33+
except ValueError as e:
34+
logging.error(f"Error decoding attachments: {e}")
35+
sys.exit(3)
36+
2837
logging.info("Connecting to Slack")
2938
client = WebClient(token=token)
3039

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

4352
try:
44-
response = client.chat_postMessage(channel=channel, text=message)
45-
assert response["message"]["text"] == message
53+
response = client.chat_postMessage(channel=channel, text=message, attachments=attachments)
4654
except SlackApiError as e:
4755
# You will get a SlackApiError if "ok" is False
4856
assert e.response["ok"] is False

incubating/slack-post-channel/step.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ kind: step-type
22
version: '1.0'
33
metadata:
44
name: slack-post-channel
5-
version: 0.0.6
5+
version: 0.0.7
66
title: Send a Slack message to a channel
77
isPublic: true
88
description: Send a message to a named Slack channel (instead of using a webhook URL)
@@ -51,7 +51,7 @@ spec:
5151
},
5252
"SLACK_IMAGE_VERSION": {
5353
"type": "string",
54-
"default": "0.0.6",
54+
"default": "0.0.7",
5555
"description": "Version (tag) of the slack-post-channel image to use."
5656
},
5757
"SLACK_TOKEN": {
@@ -67,6 +67,11 @@ spec:
6767
"description": "The message to send to the channel. Use <@ID> to tag a user. Check https://api.slack.com/reference/surfaces/formatting for details.",
6868
"default": "Message sent from Codefresh was not defined explicitely."
6969
},
70+
"SLACK_ATTACHMENTS": {
71+
"type": "string",
72+
"description": "Attachments to send. Documentation https://api.slack.com/docs/message-attachments",
73+
"default": ""
74+
},
7075
"LOG_LEVEL": {
7176
"type": "string",
7277
"description": "Set log level, default info",

0 commit comments

Comments
 (0)