Sending Messages
slackblocks
is designed primarily for use with either the slack-sdk
or (legacy) slackclient
Python packages. Usage of slackblocks
remains identical regardless of which Slack client library you're using.
While there's nothing stopping you from sending the rendered messages directly with curl
or requests
, we recommend using the **
(dictionary unpacking)operator to unpack slackblocks
Messages
directly into the Slack client
's chat_postMessage
function.
An example of this is provided below along with the JSON result of rendering the message, an equivalent curl
command, and finally the result of the message as it appears in the Slack user interface.
Sending a Message with the (Modern) slack-sdk
Library
from os import environ
from slack_sdk import WebClient
from slackblocks import Message, SectionBlock
client = WebClient(token=environ["SLACK_API_TOKEN"])
block = SectionBlock("Hello, world!")
message = Message(channel="#general", blocks=block)
response = client.chat_postMessage(**message)
{
"channel": "#general",
"mrkdwn": true,
"blocks": [
{
"type": "section",
"block_id": "992ceb6b-9ad4-496b-b8e6-1bd8a632e8b3",
"text": {
"type": "mrkdwn",
"text": "Hello, world!"
}
}
]
}
block_id
field is a pseudorandomly generated UUID. You can pass a value to Block
constructors should you desire deterministic Blocks
.
curl -H "Content-type: application/json" \
--data '{"channel":"#general","blocks":[{"type":"section", "block_id": "992ceb6b-9ad4-496b-b8e6-1bd8a632e8b3", "text":{"type":"mrkdwn","text":"Hello, world"}}]}' \
-H "Authorization: Bearer ${SLACK_API_TOKEN}" \
-X POST https://slack.com/api/chat.postMessage
Sending a Message with the (Legacy) slackclient
Library
from os import environ
from slack import WebClient
from slackblocks import Message, SectionBlock
client = WebClient(token=environ["SLACK_API_TOKEN"])
block = SectionBlock("Hello, world!")
message = Message(channel="#general", blocks=block)
response = client.chat_postMessage(**message)
{
"channel": "#general",
"mrkdwn": true,
"blocks": [
{
"type": "section",
"block_id": "992ceb6b-9ad4-496b-b8e6-1bd8a632e8b3",
"text": {
"type": "mrkdwn",
"text": "Hello, world!"
}
}
]
}
block_id
field is a pseudorandomly generated UUID. You can pass a value to Block
constructors should you desire deterministic Blocks
.
curl -H "Content-type: application/json" \
--data '{"channel":"#general","blocks":[{"type":"section", "block_id": "992ceb6b-9ad4-496b-b8e6-1bd8a632e8b3", "text":{"type":"mrkdwn","text":"Hello, world"}}]}' \
-H "Authorization: Bearer ${SLACK_API_TOKEN}" \
-X POST https://slack.com/api/chat.postMessage