0

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.

Mohamad TAGHLOBI
  • 581
  • 5
  • 11
  • You probably need to escape the quotes in the myFileText file – Raman Sailopal Mar 23 '21 at 11:40
  • Following the Raman comment, this would be useful in your case: https://stackoverflow.com/a/51333632/10426011 – MegaDrive68k Mar 23 '21 at 13:12
  • Thank you guys for your help. It's now a little better. Payload is Ok. But I still have a such as this result : { blocks:[ { type: section, text: { type: mrkdwn, text: Hello world! @C01S0SF5V19 :tada: - a -b } ... The formatting of blocks and tags is not operational. – Mohamad TAGHLOBI Mar 23 '21 at 20:58

0 Answers0