Search code examples
pythonpython-3.xslackslack-api

Slack send attachment returning 500 error


I am trying to send an attachment to a Slack web hook.

I have followed the API documentation and can send a simple message. When I try send an attachment I get a 500 error, I presume there is an issue with my payload but I cannot work it out for the life of me.

How do I get the attachment to post successfully?

import slackweb

slack = slackweb.Slack(url='WEB HOOK URL HERE')

slack.notify(text="Maguro is a sushi")

attachments = []
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
        }
    ]
}

attachments.append(attachment)
slack.notify(attachments=attachments)

Solution

  • You're missing two required fields in your Slack message with attachments: text and channel. You also need to lowercase the value of the short field to false.

    See the corrected message here in Slack's message tester:

    {
        "text": "You need this field",
        "channel": "C########",
        "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
            }
        ]
    }
    

    Create a JSON structure including those two fields and your attachments variable, and you should be good to go.