66import os
77import sys
88import logging
9+ import json
910from slack_sdk import WebClient
1011from slack_sdk .errors import SlackApiError
1112
1213def 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
0 commit comments