Skip to main content

Python's Slack Blocks Builder aims to provide an easy-to-use interface for creating complex Slack messages using Blocks, improving on Slack Bolt's native functionality.

Project description

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: str
initial_date: Optional[date] = None
placeholder: str = "Select a date"
Date fields, ask user for date
StaticSelectField label: str
options=None
placeholder: 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 valid trigger_id and a view payload to the built-in client’s views.open method.

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slack_ui-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

slack_ui-0.1.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file slack_ui-0.1.1.tar.gz.

File metadata

  • Download URL: slack_ui-0.1.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.6 Linux/6.5.0-27-generic

File hashes

Hashes for slack_ui-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fa5260fc6d408d59849858bb8c76b4854af965528cc954ec81a74e57ddeb1d2d
MD5 19a3605ac5867b5e3a67f6af24a7fd3c
BLAKE2b-256 7a38c74dc9202b26aa3f4310035e6c887fb6a00e8032e335ffd36683a3cb8215

See more details on using hashes here.

File details

Details for the file slack_ui-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: slack_ui-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.6 Linux/6.5.0-27-generic

File hashes

Hashes for slack_ui-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c6b0d5bf9f429e33fe8cb5abf4a139a845d449ad399cf902cac71dd183464874
MD5 51cd1e33704e1b736f7007112d0b1235
BLAKE2b-256 73d8b9cc27903d0489a6456f03fca64e1cbc4a8f5255f717d5b1a4f2946dbb32

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page