1

I'm writing up a slack integration and I'm trying to add a message attachment but I'm stuck on the syntax of putting it all together into a curl statement.

This is the basic curl statement that I have right now that I want to add an attachment to:

curl \
-X POST \
-H "Content-type: application/json" \
--data "{\"text\":\"$MESSAGE\"}" \
https://hooks.slack.com/services/code1/code2/code3

Below is the example given of an attachment:

{
    "attachments": [
        {
            "fallback": "Required plain-text summary of the attachment.",
            "color": "#36a64f",
            "pretext": "Optional text that appears above the attachment block",
            "author_name": "Bobby Tables",
            "author_link": "http://flickr.com/bobby/",
            "author_icon": "http://flickr.com/icons/bobby.jpg",
            "title": "Slack API Documentation",
            "title_link": "https://api.slack.com/",
            "text": "Optional text that appears within the attachment",
            "fields": [
                {
                    "title": "Priority",
                    "value": "High",
                    "short": false
                }
            ],
            "image_url": "http://my-website.com/path/to/image.jpg",
            "thumb_url": "http://example.com/path/to/thumb.png",
            "footer": "Slack API",
            "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
            "ts": 123456789
        }
    ]
}

However, I'm confused on how to place that attachment block inside the curl statement. Could someone write up a complete curl statement that would include that attachment block for me so I can see how it's done?

Scott Shorkey
  • 325
  • 4
  • 10

1 Answers1

5

Its actually pretty easy. attachments is just another key in your JSON array, on the same level as text.

So your new JSON array should look something like this (obv. before character escaping):

{
  "text": "here goes your message text",
   "attachments": 
   [
      {

         "text": "Optional text that appears within the attachment",
         ...
      }
   ]
}

Btw. you can add other keys like channel and icon_url the same way.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114