After much research, I discovered this .sh script, which allows me to send a very simple personalized notification to a Slack channel (I failed to send it to a particular member). The problem, which I am facing, is that of formatting the message of the notification. The tags offered on the Slack site to format the notification do not work correctly (that I do not know how to use them!). The notification must be built "from scratch" at the end of each execution of the Rundeck job. Rundeck's "On Failure" and "On Success" notifications are not suitable for users.
This the sh script :
#!/bin/bash
#Usage: slackpost <channel> <message>
slackhost="https://hooks.slack.com/services"
token="XXXXXXXXXX/XXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXX"
slack_username="Someone-who-loves-you"
slack_icon="smile"
channel=$1
if [[ $channel == "" ]]
then
echo "No channel specified"
exit 1
fi
text="$2"
if [[ $text == "" ]]
then
echo "No text specified"
exit 1
fi
escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )
json="{\"channel\": \"#$channel\", \"username\":\"${slack_username}\", \"icon_emoji\":\":${slack_icon}:\", \"text\": \"$escapedText\"}"
curl -s -d "payload=$json" "$slackhost/$token"
The variable $text is filled with the content of a .txt file (myFileText):
myFileText content is as follows:
text="",
{
blocks=[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hello world! <@UNVD64N02> :tada: \n\n - a \n-b"
}
}
]
}
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hello, Assistant to the Regional Manager John Doe! *Martin Scott* wants to know where you'd like to take the Paper Company investors to dinner tonight.\n\n *Please select a restaurant:*"
}
}
]
}
The .sh script is run as follows:
sh slack_post.sh "channel-receiving-the-notification" "$(< myFileText)"
As result I have :
invalid_payload
Any idea could be helpful.