6
6
import os
7
7
import sys
8
8
import logging
9
+ import json
9
10
from slack_sdk import WebClient
10
11
from slack_sdk .errors import SlackApiError
11
12
12
13
def main ():
13
14
log_format = "%(asctime)s:%(levelname)s:%(name)s.%(funcName)s: %(message)s"
14
15
logging .basicConfig (format = log_format , level = os .environ ['LOG_LEVEL' ].upper ())
15
16
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' , "" )
19
21
20
22
if ( channel == None ):
21
23
logging .error ("SLACK_CHANNEL is not defined" )
@@ -25,6 +27,13 @@ def main():
25
27
logging .error ("SLACK_TOKEN is not defined" )
26
28
sys .exit (1 )
27
29
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
+
28
37
logging .info ("Connecting to Slack" )
29
38
client = WebClient (token = token )
30
39
@@ -41,8 +50,7 @@ def main():
41
50
sys .exit (3 )
42
51
43
52
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 )
46
54
except SlackApiError as e :
47
55
# You will get a SlackApiError if "ok" is False
48
56
assert e .response ["ok" ] is False
0 commit comments