Skip to content

Commit c5059f5

Browse files
authored
feat: Improve slack message formatting for generic messages (#124)
1 parent b52d94e commit c5059f5

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-29
lines changed

functions/notify_slack.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,18 @@ def cloudwatch_notification(message, region):
4242

4343

4444
def default_notification(subject, message):
45-
return {
45+
attachments = {
4646
"fallback": "A new message",
47-
"fields": [{"title": subject if subject else "Message", "value": json.dumps(message) if type(message) is dict else message, "short": False}]
47+
"title": subject if subject else "Message",
48+
"fields": []
4849
}
50+
if type(message) is dict:
51+
for k, v in message.items():
52+
attachments['fields'].append({"title": k, "value": v, "short": False})
53+
else:
54+
attachments['fields'].append({"value": message, "short": False})
55+
56+
return attachments
4957

5058

5159
# Send a message to a slack channel

functions/notify_slack_test.py

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,52 @@
3131
}
3232
),
3333
(
34-
{
35-
"Records": [
36-
{
37-
"EventSource": "aws:sns",
38-
"EventVersion": "1.0",
39-
"EventSubscriptionArn": "arn:aws:sns:eu-west-2:735598076380:service-updates:d29b4e2c-6840-9c4e-ceac-17128efcc337",
40-
"Sns": {
41-
"Type": "Notification",
42-
"MessageId": "f86e3c5b-cd17-1ab8-80e9-c0776d4f1e7a",
43-
"TopicArn": "arn:aws:sns:eu-west-2:735598076380:service-updates",
44-
"Subject": "All Fine",
45-
"Message": "This\nis\na typical multi-line\nmessage from SNS!\n\nHave a ~good~ amazing day! :)",
46-
"Timestamp": "2019-02-12T15:45:24.091Z",
47-
"SignatureVersion": "1",
48-
"Signature": "WMYdVRN7ECNXMWZ0faRDD4fSfALW5MISB6O//LMd/LeSQYNQ/1eKYEE0PM1SHcH+73T/f/eVHbID/F203VZaGECQTD4LVA4B0DGAEY39LVbWdPTCHIDC6QCBV5ScGFZcROBXMe3UBWWMQAVTSWTE0eP526BFUTecaDFM4b9HMT4NEHWa4A2TA7d888JaVKKdSVNTd4bGS6Q2XFG1MOb652BRAHdARO7A6//2/47JZ5COM6LR0/V7TcOYCBZ20CRF6L5XLU46YYL3I1PNGKbEC1PIeVDVJVPcA17NfUbFXWYBX8LHfM4O7ZbGAPaGffDYLFWM6TX1Y6fQ01OSMc21OdUGV6HQR01e%==",
49-
"SigningCertUrl": "https://sns.eu-west-2.amazonaws.com/SimpleNotificationService-7dd85a2b76adaa8dd603b7a0c9150589.pem",
50-
"UnsubscribeUrl": "https://sns.eu-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-2:735598076380:service-updates:d29b4e2c-6840-9c4e-ceac-17128efcc337",
51-
"MessageAttributes": {}
52-
}
53-
}
54-
]
55-
}
34+
{
35+
"Records": [
36+
{
37+
"EventSource": "aws:sns",
38+
"EventVersion": "1.0",
39+
"EventSubscriptionArn": "arn:aws:sns:eu-west-2:735598076380:service-updates:d29b4e2c-6840-9c4e-ceac-17128efcc337",
40+
"Sns": {
41+
"Type": "Notification",
42+
"MessageId": "f86e3c5b-cd17-1ab8-80e9-c0776d4f1e7a",
43+
"TopicArn": "arn:aws:sns:eu-west-2:735598076380:service-updates",
44+
"Subject": "All Fine",
45+
"Message": "This\nis\na typical multi-line\nmessage from SNS!\n\nHave a ~good~ amazing day! :)",
46+
"Timestamp": "2019-02-12T15:45:24.091Z",
47+
"SignatureVersion": "1",
48+
"Signature": "WMYdVRN7ECNXMWZ0faRDD4fSfALW5MISB6O//LMd/LeSQYNQ/1eKYEE0PM1SHcH+73T/f/eVHbID/F203VZaGECQTD4LVA4B0DGAEY39LVbWdPTCHIDC6QCBV5ScGFZcROBXMe3UBWWMQAVTSWTE0eP526BFUTecaDFM4b9HMT4NEHWa4A2TA7d888JaVKKdSVNTd4bGS6Q2XFG1MOb652BRAHdARO7A6//2/47JZ5COM6LR0/V7TcOYCBZ20CRF6L5XLU46YYL3I1PNGKbEC1PIeVDVJVPcA17NfUbFXWYBX8LHfM4O7ZbGAPaGffDYLFWM6TX1Y6fQ01OSMc21OdUGV6HQR01e%==",
49+
"SigningCertUrl": "https://sns.eu-west-2.amazonaws.com/SimpleNotificationService-7dd85a2b76adaa8dd603b7a0c9150589.pem",
50+
"UnsubscribeUrl": "https://sns.eu-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-2:735598076380:service-updates:d29b4e2c-6840-9c4e-ceac-17128efcc337",
51+
"MessageAttributes": {}
52+
}
53+
}
54+
]
55+
}
56+
),
57+
(
58+
{
59+
"Records": [
60+
{
61+
"EventSource": "aws:sns",
62+
"EventVersion": "1.0",
63+
"EventSubscriptionArn": "arn:aws:sns:eu-west-2:735598076380:service-updates:d29b4e2c-6840-9c4e-ceac-17128efcc337",
64+
"Sns": {
65+
"Type": "Notification",
66+
"MessageId": "f86e3c5b-cd17-1ab8-80e9-c0776d4f1e7a",
67+
"TopicArn": "arn:aws:sns:eu-west-2:735598076380:service-updates",
68+
"Subject": "DMS Notification Message",
69+
"Message": "{\"Event Source\": \"replication-task\", \"Event Time\": \"2019-02-12 15:45:24.091\", \"Identifier Link\": \"https://console.aws.amazon.com/dms/home?region=eu-west-2#tasks:ids=hello-world\\nSourceId: hello-world \", \"Event ID\": \"http://docs.aws.amazon.com/dms/latest/userguide/CHAP_Events.html#DMS-EVENT-0079 \", \"Event Message\": \"Replication task has stopped.\"}",
70+
"Timestamp": "2019-02-12T15:45:24.091Z",
71+
"SignatureVersion": "1",
72+
"Signature": "WMYdVRN7ECNXMWZ0faRDD4fSfALW5MISB6O//LMd/LeSQYNQ/1eKYEE0PM1SHcH+73T/f/eVHbID/F203VZaGECQTD4LVA4B0DGAEY39LVbWdPTCHIDC6QCBV5ScGFZcROBXMe3UBWWMQAVTSWTE0eP526BFUTecaDFM4b9HMT4NEHWa4A2TA7d888JaVKKdSVNTd4bGS6Q2XFG1MOb652BRAHdARO7A6//2/47JZ5COM6LR0/V7TcOYCBZ20CRF6L5XLU46YYL3I1PNGKbEC1PIeVDVJVPcA17NfUbFXWYBX8LHfM4O7ZbGAPaGffDYLFWM6TX1Y6fQ01OSMc21OdUGV6HQR01e%==",
73+
"SigningCertUrl": "https://sns.eu-west-2.amazonaws.com/SimpleNotificationService-7dd85a2b76adaa8dd603b7a0c9150589.pem",
74+
"UnsubscribeUrl": "https://sns.eu-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-2:735598076380:service-updates:d29b4e2c-6840-9c4e-ceac-17128efcc337",
75+
"MessageAttributes": {}
76+
}
77+
}
78+
]
79+
}
5680
),
5781
(
5882
{
@@ -67,11 +91,11 @@
6791
}
6892
),
6993
(
70-
{
71-
"AlarmType": "Unsupported alarm type",
72-
"AWSAccountId": "000000000000",
73-
"NewStateValue": "ALARM",
74-
}
94+
{
95+
"AlarmType": "Unsupported alarm type",
96+
"AWSAccountId": "000000000000",
97+
"NewStateValue": "ALARM",
98+
}
7599
)
76100
)
77101

0 commit comments

Comments
 (0)