I have created a hooks.slack.com/services
webhook for Slack. This is done as a custom app, as is recommended by Slack as the correct way to do this in 2023.
This hook participates in the following flow:
AWS SNS event -> Lambda -> Slack hook -> message in Slack channel
I have created the following python AWS lambda which consumes an SNS event message and then sends a formatted version of it to my webhook.
import http.client
import json
def lambda_handler(event, context):
url = "https://hooks.slack.com/services/<myurl>"
headers = {"Content-type": "application/json"}
msg = {
"channel": "<my-channel-name>",
"icon_url": "<my image url>",
"blocks": [
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Alarm:* " + event["Records"][0]["Sns"]["Subject"]
}
]
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Message:* " + event["Records"][0]["Sns"]["Message"]
}
]
}
]
}
encoded_msg = json.dumps(msg).encode("utf-8")
conn = http.client.HTTPSConnection("hooks.slack.com")
conn.request("POST", url, encoded_msg, headers)
response = conn.getresponse()
print({
"message": event["Records"][0]["Sns"]["Message"],
"status_code": response.status,
"response": response.read().decode()
})
Slack renders messages received into my channel using the following icon:
I don't want this! How can I change it to something else?
I have tried setting icon_url
in the message payload, but this is ignored.
Slack's documentation is unclear on this.