Python's Slack Blocks Builder
The Slack Bot (Slack Bolt) lacks a user-friendly way to utilize Slack Blocks out of the box. But, using this library will afford you the convenience you need to construct fancy messages on Slack without having to resort to using dictionaries.
Support for Forms - package allow you to easier create a form (more details).
Getting Started
You can install this library using your preferred package manager:
- For Poetry:
poetry add slack_ui - For PIP:
pip install slack_ui
Now, you can use kit e.g.:
import slack_ui
message = slack_ui.views.Message()
message.add_blocks(
[
slack_ui.HeaderBlock(":bar_chart: Summary of Week"),
slack_ui.DividerBlock(),
slack_ui.TextBlock(
"What happened at this week?\n"
"What is Top 10 of cats?"
),
]
)
message.add_block(
slack_ui.ContextBlock(
[
slack_ui.TextBlock("col 1"),
slack_ui.TextBlock("col 2"),
slack_ui.TextBlock("col 2"),
]
)
)
bot_app.client.chat_postMessage(
...,
blocks=message.slack_view(),
)
Blocks
| Name | Parameters | Docs reference | Note |
|---|---|---|---|
HeaderBlock |
text: str |
docs | support for emoji |
TextBlock |
text: str |
docs | support for emoji and markdown |
ContextBlock |
context: List[TextBlock] |
docs | columns divided |
SectionBlock |
text: Optional[str] fields: Optional[List[str]] |
docs | |
DividerBlock |
docs | divided line similar to hr in html |
Forms
You can defined class which will be converted to slack form view.
Class have to inheritance class Form and define a slack_blocks.forms.Field.
Example:
import slack_ui
class HolidayKind(slack_ui.forms.SelectFormEnum):
HOLIDAYS = "holidays_leave", "Resting"
SICK = "sick_leave", "Sick"
class HolidayRequestForm(slack_ui.forms.Form):
start_at = slack_ui.forms.DateField(label="Date start")
end_at = slack_ui.forms.DateField(label="Date end")
kind_of = slack_ui.forms.StaticSelectField(
label="Kind of",
options=HolidayKind,
)
def handle_holidays_request(ack, shortcut, client, body):
"""Handle button action and show form modal"""
form = HolidayRequestForm()
form_modal = slack_ui.views.FormModal(
"Holidays request", # Title of form modal
"callback_id", # id of defined callback action
form
)
(...)
client.views_open(
trigger_id=body["trigger_id"],
view=form_modal.slack_view(),
)
(...)
Form Inputs
| Name | Attributes | Note |
|---|---|---|
TextField |
label: str |
Text field, ask user for some text input |
DateField |
label: strinitial_date: Optional[date] = Noneplaceholder: str = "Select a date" |
Date fields, ask user for date |
StaticSelectField |
label: stroptions=Noneplaceholder: str = "Select an item" |
Select field, ask user for choose option |
Views
Package provide views: Modal, FormModal, Message.
You can use each after import from slack_blocks.views import Message, Modal, FormModal.
Message
Simple type, message is common unit in this kit.
When you want to send message = use message.
example:
import slack_ui
message = slack_ui.views.Message()
message.add_blocks(
[
slack_ui.HeaderBlock(":bar_chart: Summary of Week"),
slack_ui.TextBlock("What happened at this week?"),
]
)
message.slack_view()
Modal
Slack bolt documentation "Opening modals"
Modals are focused surfaces that allow you to collect user data and display dynamic information.
You can open a modal by passing a validtrigger_idand a view payload to the built-in client’sviews.openmethod.
example:
from slack_ui.views import Modal
import slack_ui
modal = Modal("Modal title", "callback_id_0001")
modal.add_block(slack_ui.TextBlock("Some text"))
modal.slack_view()
FormModal
This is specified type of modal where inside is generated a form view.
The form have to be instance of class base on slack_blocks.forms.Form
example:
from slack_ui.views import FormModal
form = DefinedForm()
modal = FormModal("Modal title", "callback_id_0001", form)
modal.slack_view()
Release files for slack_ui 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| slack_ui-0.1.1.tar.gz | 5.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| slack_ui-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.7 kB
Release files / slack_ui-0.1.1.tar.gz
| Download URL | slack_ui-0.1.1.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fa5260fc6d408d59849858bb8c76b4854af965528cc954ec81a74e57ddeb1d2d
|
|
BLAKE2b-256 checksum How to use checksums |
7a38c74dc9202b26aa3f4310035e6c887fb6a00e8032e335ffd36683a3cb8215
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.6.1 CPython/3.10.6 Linux/6.5.0-27-generic
|
Release files / slack_ui-0.1.1-py3-none-any.whl
| Download URL | slack_ui-0.1.1-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c6b0d5bf9f429e33fe8cb5abf4a139a845d449ad399cf902cac71dd183464874
|
|
BLAKE2b-256 checksum How to use checksums |
73d8b9cc27903d0489a6456f03fca64e1cbc4a8f5255f717d5b1a4f2946dbb32
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.6.1 CPython/3.10.6 Linux/6.5.0-27-generic
|