Created
January 5, 2025 01:58
-
-
Save mike-weiner/c414b1da6dcd62251afe8cab88173155 to your computer and use it in GitHub Desktop.
A simple Bash script that can be used to send Slack messages via curl to a Slack Channel using the Slack Block API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SLACK_CHANNEL="automation" | |
SLACK_URL="https://slack.com/api/chat.postMessage" | |
AS_USER=true | |
slackMsgHeaderBlock=$( | |
jq -n '{ | |
type: "rich_text", | |
elements: [ | |
{ | |
type: "rich_text_section", | |
elements: [ | |
{ | |
"type": "emoji", | |
"name": "rotating_light" | |
}, | |
{ | |
type: "text", | |
text: " " | |
}, | |
{ | |
type: "text", | |
style: { | |
bold: true, | |
}, | |
text: "Triage Required." | |
}, | |
{ | |
type: "text", | |
text: " " | |
}, | |
{ | |
type: "text", | |
text: "E2E could not be started." | |
}, | |
{ | |
type: "text", | |
text: " " | |
}, | |
{ | |
"type": "link", | |
"url": "https://google.com", | |
"text": "View log." | |
} | |
] | |
} | |
] | |
}' | |
) | |
slackMsgPayload=$(jq -c -n \ | |
--arg channel "$SLACK_CHANNEL" \ | |
--arg blocks "[$slackMsgHeaderBlock]" \ | |
--argjson as_user "$AS_USER" \ | |
'$ARGS.named' | |
) | |
slackMsgResponse=$(curl -s -X POST -H "Authorization: Bearer $TOKEN" -H "Content-type: application/json" -d "$slackMsgPayload" "$SLACK_URL") | |
slackThreadTs=$(jq -r '.message.ts' <<< "${slackMsgResponse}") | |
slackMsgThreadedBlocks=$( | |
jq -n '{ | |
type: "rich_text", | |
elements: [ | |
{ | |
type: "rich_text_section", | |
elements: [ | |
{ | |
type: "text", | |
text: "Check the following:" | |
} | |
] | |
}, | |
{ | |
type: "rich_text_list", | |
style: "bullet", | |
elements: [ | |
{ | |
type: "rich_text_section", | |
elements: [ | |
{ | |
type: "text", | |
text: "Action item #1." | |
} | |
] | |
}, | |
{ | |
type: "rich_text_section", | |
elements: [ | |
{ | |
type: "text", | |
text: "Action item #2." | |
} | |
] | |
} | |
] | |
} | |
] | |
}' | |
) | |
slackThreadedMsgPayload=$(jq -c -n \ | |
--arg channel "$SLACK_CHANNEL" \ | |
--arg blocks "[$slackMsgThreadedBlocks]" \ | |
--arg thread_ts "$slackThreadTs" \ | |
--argjson as_user "$AS_USER" \ | |
'$ARGS.named' | |
) | |
slackThreadedMsgResponse=$(curl -s -X POST -H "Authorization: Bearer $TOKEN" -H "Content-type: application/json" -d "$slackThreadedMsgPayload" "$SLACK_URL") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment