Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklambourne committed Nov 19, 2024
1 parent 1c37da3 commit 0ef6281
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,29 @@ Will produce the following JSON string:
```
Which can be sent as payload to the Slack message API HTTP endpoints.

Of more practical uses is the ability to unpack the objects directly into
the Python Slack Client in order to send messages:
Of more practical use is the ability to unpack the objects directly into
the [(Legacy) Python Slack Client](https://pypi.org/project/slackclient/) in order to send messages:

```python
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)
```

Or the modern Python [Slack SDK](https://pypi.org/project/slack-sdk/):
```python
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)
Expand Down
47 changes: 47 additions & 0 deletions docs_src/usage/sending_messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,53 @@ While there's nothing stopping you from sending the rendered messages directly w

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 `slack-sdk` Library
=== "Python (`slackblocks`)"
```python
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)
```

=== "JSON Message"
```json
{
"channel": "#general",
"mrkdwn": true,
"blocks": [
{
"type": "section",
"block_id": "992ceb6b-9ad4-496b-b8e6-1bd8a632e8b3",
"text": {
"type": "mrkdwn",
"text": "Hello, world!"
}
}
]
}
```
* Note that the `block_id` field is a pseudorandomly generated UUID. You can pass a value to `Block` constructors should you desire deterministic `Blocks`.

=== "Equivalent `curl` Command"
```bash
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
```

=== "Slack UI Output"
![Hello World Slack Image](../img/hello_world.png)


### Sending a Message with the (Legacy) `slackclient` Library
=== "Python (`slackblocks`)"
```python
from os import environ
Expand Down

0 comments on commit 0ef6281

Please sign in to comment.