Scott Shorkey
Scott Shorkey

Reputation: 345

Syntax help - adding attachments to slack incoming webhooks

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?

Upvotes: 1

Views: 3823

Answers (1)

Erik Kalkoken
Erik Kalkoken

Reputation: 32854

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.

Upvotes: 5

Related Questions