Prince AJ
Prince AJ

Reputation: 33

how to use slack rich formatting in linux bash script

I'm trying to send a notification to a slack channel using incoming webhook below is my bash script for that

# !/bin/sh
curl -X POST --data-urlencode 'payload={"channel": "#aws_webhooks", "username": "webhookbot",
"attachments":[
      {
         "fallback":"blahb",
         "pretext":"blahblahblahblahbla",
         "color":"warning",
         "fields":[
            {
               "title":"Alarms Updates",
               "value":" blahblahblahblahblahblah",
               "short":false
            }
         ]
      }
   ]
}'
https://hooks.slack.com/services/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

After running this script I'm facing this error, can someone help me in what I'm doing wrong in writing script

sh new.sh
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information

Upvotes: 0

Views: 3152

Answers (2)

Elad Ishay
Elad Ishay

Reputation: 11

You should add a '\' in case the url is on a separate line:

# !/bin/sh
curl -X POST --data-urlencode 'payload={"channel": "#aws_webhooks", "username": "webhookbot",
"attachments":[
      {
         "fallback":"blahb",
         "pretext":"blahblahblahblahbla",
         "color":"warning",
         "fields":[
            {
               "title":"Alarms Updates",
               "value":" blahblahblahblahblahblah",
               "short":false
            }
         ]
      }
   ]
}' \ <-------backslash
https://hooks.slack.com/services/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

Upvotes: 1

Thomas Spicer
Thomas Spicer

Reputation: 47

Not sure what is going on with your code, but you can take a look at the bash client I wrote here: https://github.com/openbridge/ob_hacky_slack

Here is a code snippet created to the post:

    # Send the payload to the Slack API
  echo "OK: All tests passed, sending message to Slack API..."
  POST=$(curl -s -S -X POST --data-urlencode "${PAYLOAD}" "${WEBHOOK}${TOKEN}");

  # Check if the message posted to the Slack API. A successful POST should return "ok". Anything other than "ok" indicates an issue
  if test "${POST}" != ok; then echo "ERROR: The POST to the Slack API failed (${POST})" && return 1; else echo "OK: Message successfully sent to the channel ${CHANNEL} via the Slack API"; fi

It might provide some clues as to your issues.

Upvotes: 0

Related Questions