Search code examples
pythonslackslack-api

Python - Send a list intro Slack message formatting in Python


So I have been playing around with python and Discord's webhook with Slacks message formatting which can be found here: Slack message formatting

However what I am trying to do is to have a multiply URL that can be sent to slack similar like:

enter image description here

and now when I have added all my URL to a list and trying to apply it to the formatting etc:

{
    "username": "Google website",
    "attachments": [
        {
            "author_name": "Google",
            "color": "#00ff00",
            "text": "^Press the link above!",
            "title": "www.google.se",
            "title_link": URLLIST
        }
    ]
}

It tells me that "Must be str, not a list"

And I have been stuck on this since there is not pretty good documentation about this, Anyone that could know how to do this?


Solution

  • I am guessing you are getting the error, because your URLLIST is not a string.

    Here are two solution that will work:

    Either you do multiple attachments, where every attachment is one link. Then title_link must be a URL string, not a list.

    Example:

    {
        "attachments": [
            {
                "fallback": "Required plain-text summary of the attachment.",            
                "title": "Slack API Documentation",
                "title_link": "https://api.slack.com/"            
            },
            {
                "fallback": "Required plain-text summary of the attachment.",            
                "title": "Slack API Documentation",
                "title_link": "https://api.slack.com/"            
            },
            {
                "fallback": "Required plain-text summary of the attachment.",            
                "title": "Slack API Documentation",
                "title_link": "https://api.slack.com/"            
            }
        ]
    }
    

    Message Builder Example

    Or you just explode your URL list into a text string (which I would do). Then you do not even need attachments.

    Example:

    {
        "text": "<https://www.google.com|8>\n<https://www.google.com|9>\n<https://www.google.com|10>\n"
    }
    

    Message Builder Example