Skip to content

Commit 59cbaf9

Browse files
authored
Merge pull request #121 from Jorricks/mr/master/jorrick/add-function-to-show-message-in-slacks-block-kit-builder
✨ Add function to show message in slacks Block kit builder
2 parents 980c6c5 + b5b4348 commit 59cbaf9

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

blockkit/core.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from abc import ABC, abstractmethod
55
from datetime import date, datetime, time
66
from typing import Any, Final, Literal, Self, TypeAlias, get_args
7+
from urllib import parse
78
from zoneinfo import ZoneInfo
89

910
from blockkit.utils import is_md
@@ -741,6 +742,28 @@ def external_id(self, external_id: str | None) -> Self:
741742
)
742743

743744

745+
class BuilderUrlMixin:
746+
def builder_url(self) -> str:
747+
"""
748+
Returns a URL to preview this message in Slack's Block Kit Builder.
749+
"""
750+
751+
surface = self.build()
752+
filter_fields = {
753+
Message: ("blocks",),
754+
Modal: ("type", "title", "submit", "close", "blocks"),
755+
Home: ("type", "blocks"),
756+
}.get(type(self), None)
757+
758+
if filter_fields:
759+
surface = {k: v for k, v in surface.items() if k in filter_fields}
760+
761+
encoded_json = parse.quote(
762+
json.dumps(surface, ensure_ascii=False, separators=(",", ":")), safe=""
763+
)
764+
return f"https://app.slack.com/block-kit-builder#{encoded_json}"
765+
766+
744767
"""
745768
Composition objects
746769
"""
@@ -3192,7 +3215,7 @@ def author_name(self, author_name: str | None) -> Self:
31923215
)
31933216

31943217

3195-
class Message(Component):
3218+
class Message(Component, BuilderUrlMixin):
31963219
"""
31973220
Message surface
31983221
@@ -3242,7 +3265,12 @@ def mrkdwn(self, mrkdwn: bool | None = True) -> Self:
32423265

32433266

32443267
class Modal(
3245-
Component, BlocksMixin, PrivateMetadataMixin, CallbackIdMixin, ExternalIdMixin
3268+
Component,
3269+
BlocksMixin,
3270+
PrivateMetadataMixin,
3271+
CallbackIdMixin,
3272+
ExternalIdMixin,
3273+
BuilderUrlMixin,
32463274
):
32473275
"""
32483276
Modal surface
@@ -3320,7 +3348,12 @@ def submit_disabled(self, submit_disabled: bool | None = True) -> Self:
33203348

33213349

33223350
class Home(
3323-
Component, BlocksMixin, PrivateMetadataMixin, CallbackIdMixin, ExternalIdMixin
3351+
Component,
3352+
BlocksMixin,
3353+
PrivateMetadataMixin,
3354+
CallbackIdMixin,
3355+
ExternalIdMixin,
3356+
BuilderUrlMixin,
33243357
):
33253358
"""
33263359
App Home surface

docs/quickstart.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ message = (
2828

2929
That's it. Real Slack BlockKit JSON. Ready to send.
3030

31+
Wondering what it looks like now in Slack? You can check your exact message in the Slack block kit builder before sending it;
32+
```python
33+
message.get_block_kit_explorer_url()
34+
```
35+
3136
## Send it
3237

3338
```python

tests/test_core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,3 +3674,4 @@ def test_builds(self):
36743674
.external_id("alice_intro")
36753675
.build()
36763676
)
3677+
assert got == want

0 commit comments

Comments
 (0)