DEV Community

Fomalhaut Weisszwerg
Fomalhaut Weisszwerg

Posted on

How to post bulleted lists using Slack Webhook URL.

TL;DR

The "Rich text block" is your friend.

import os

from slack_sdk import WebhookClient

if __name__ == "__main__":
    api_client = WebhookClient(url=os.environ["SLACK_WEBHOOK_URL"])

    unordered_list_elements: list[dict] = []
    for fragment in ["foo", "bar", "baz"]:
        unordered_list_elements.append(
            {
                "type": "rich_text_section",
                "elements": [{"type": "text", "text": fragment}],
            }
        )

    api_client.send(
        blocks=[
            {
                "type": "rich_text",
                "elements": [
                    {
                        "type": "rich_text_list",
                        "style": "bullet",
                        "indent": 0,
                        "elements": unordered_list_elements,
                    }
                ],
            }
        ]
    )
Enter fullscreen mode Exit fullscreen mode

The official Markdown example does not work

The official document provides a Markdown example:

{
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "• Detective Chimp\n• Bouncing Boy\n• Aqualad"
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

But the example does not work properly.

There seems to be a bug in Markdown processing. So you need to use the other way to post a bulleted list via Webhook URL.

"Rich text block"

Rich text block is one of workarounds to the bug.

The basic format of the block is:

[
    {
        "type": "rich_text",
        "elements": [
            {
                "type": "rich_text_list",
                "style": "bullet",
                "elements": [rich_text_sections],
            }
        ],
    }
]
Enter fullscreen mode Exit fullscreen mode

Note that the type of outermost block must always be set to "rich_text". And a block of bulleted list need to be declared as the inner block.

Finally, define elements of the bulleted list as a list of rich_text_section. The format of rich_text_section is following:

{
    "type": "rich_text_section",
    "elements": [
        {
            "type": "text",
            "text": value_of_elements
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

If you want to mention someone, set value of "type" "user" instead of "text" such like:

{
    "type": "rich_text_section",
    "elements": [
        {
            "type": "user",
            "user_id": target_user_id,
        }
    ],
}
Enter fullscreen mode Exit fullscreen mode

Similarly, for email address, set "type" "link" like following:

{
    "type": "rich_text_section",
    "elements": [
        {
            "type": "link",
            "url": f"mailto:{mail_address}",
            "text": mail_address,
        }
    ],
}
Enter fullscreen mode Exit fullscreen mode

Now it is able to post to your Slack workspace using slack_sdk.WebhookClient.send method!

Top comments (0)