asp
asp

Reputation: 855

Formatting slack message posted from shell script

I have a shell program which write multiline output to a variable called "$text" which outputs as below on terminal.

echo "$text"

==============================================
Oracle Host: xxxxxxxxx
==============================================
orcl:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------
orcltest:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------

Now to trying to post this as above output in slack channel, so trying to read the lines from above variable with code below:

while IFS= read -r line; do
#printf '%s\n' "$line"
text="$text$line\n"
done <<< "$text"
escapedText=$(echo $text | sed 's/"/\\"/g; s/'\''/\\'\''/g' )
json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}"
curl -X POST --data-urlencode "payload=$json" https://hooks.slack.com/services/xxxxx/xxxx/xxxxx"

its showing error like:

missing_text_or_fallback_or_attachments

but in above code I guess while loop is not working properly...is that correct way of reading multi line variable and pass on to payload in curl statement

Upvotes: 1

Views: 4186

Answers (3)

Walter A
Walter A

Reputation: 20032

The escapedText=$(echo $text | sed 's/"/\\"/g; s/'\''/\\'\''/g' ) can cause problems. The $text should be quoted when you do not want the shell to interpret the data (eat spaces and newlines, get confused by special characters).

The loop reading from the var is not needed and might give problems when the last character in the var is not a \n. The read command only reads "complete lines", anything after the last \n is ignored.
This problem is also possible when reading from a file. Most times when a file has been created (using vi or echo "Something" > file) the file will be created with a closing \n. The loop, filling a var $text with the contents of $text is not needed.

Upvotes: 1

asp
asp

Reputation: 855

Thanks to @Walter A @Erik Kalkoken for your time and help

Ultimately what worked is as per Walter A, I just replaced

escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )

with

escapedText="${text}"

and removed below section together looping through text

while IFS= read -r line; do
#printf '%s\n' "$line"
text="$text$line\n"
done <<< "$text"

Upvotes: 0

Erik Kalkoken
Erik Kalkoken

Reputation: 32854

I think you are not using the correct format and syntax for your POST to webhook.

The correct format is raw POST with a JSON body like so:

POST https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
Content-type: application/json
{
    "text": "Hello, world."
}

But you have a weird payload parameter in your curl. Also you need to set JSON as content type.

Assuming your JSON syntax is correct, this should work:

curl https://hooks.slack.com/services/xxx -X POST -H "Content-type: application/json" --data '$json'

Upvotes: 0

Related Questions