Package: slack-blockkit
Slack Blockkit Builder is a simple utility for developing code blocks forSlack’s Block Kit. It provides
Pythonic-style blocks to allow for dynamic generation of block kits where copy-and-paste just won’t do it.Visit thedocumentationfor more examples and method/class docs.InstallationInstallation is as easy as:pipinstallslack_blockkitIt is currently tested on Python3+, but can be used for Python 2.7.UsageCategoriesSlack’s Block Kitcomes with 4 different block categories:Block elements- The standard element of a block.Composition objects- Composition objects can be used inside of block elements and certain message payload fields. They are simply common JSON object patterns that you’ll encounter frequently when building blocks or composing messages.Interactive components- Interactive components are a subset of Block Kit elements that add interactivity to various app surfaces.View payloads- Views are app-customized visual areas within modals and Home tabs.WarningSome interactive components are not implemented in version 0.0.4Example UsageYou can access the different components by importing them from their respective package. For example,Block elementscan be accessed by importingfrom slack_blockkit.block_element import .... A common block element is theTextObject:fromslack_blockkit.block_elementimportTextObjecttext_object=TextObject(btype="mrkdwn",text="This is a text block")Blocks are inserted into a list as dicts. Each component comes with arender()method which will return adict:text_object.render()# {"type": "mrkdwn", "text": "This is a text block"}Notebtypein synonymous with Slack’stype.typeis reserved in Python.Message BlocksMessage payloads to Slack take alistof dictionaries. Theslack_blockkit.utilsmodule has aget_blocksmethod that takes Block objects and returns the formatted list:fromslack_blockkit.utilsimportget_blocksfromslack_blockkit.text_objectimportTextObjectfromslack_blockkit.layout_blockimportDividerBlock,ImageBlockblocks=get_blocks(TextObject(btype="mrkdwn",text="Welcome to CodeDevils!"),DividerBlock(),ImageBlock(image_url="https://codedevils.org/static/img/logo",alt_text="Logo"))Which makesblocks:{"type":"mrkdwn","text":"Welcome to CodeDevils!"},{"type":"divider"},{"type":"image","image_url":"https://codedevils.org/static/img/logo","alt_text":"Logo"}Online Message BlocksAlternatively you can use thetest_blocks_onlineif you want to test your blocks before
publishing them. The utility takes the blocks you input as arguments and creates a link that
takes you toSlack’s block kit builder. An example is:fromslack_blockkit.layout_blockimportSectionBlockfromslack_blockkit.composition_objectimportPlainTextObjectfromslack_blockkit.utilsimporttest_blocks_onlinesection1=SectionBlock(text=PlainTextObject(text="Section 1"))section2=SectionBlock(text=PlainTextObject(text="Section 2"))test_blocks_online(section1,section2)This will print out to the console:Copyandpastethefollowingurlintoyourbrowser:https://app.slack.com/block-kit-builder/#{"blocks":[{"block_id":"96dc84a2-d517-4a75-ab83-193770df62cc","text":{"text":"Section 1","emoji":false,"type":"plain_text"},"type":"section"},{"block_id":"182c3fc6-3d7b-464e-8fb9-0fb832e8cd02","text":{"text":"Section 2","emoji":false,"type":"plain_text"},"type":"section"}]}Copy and pasting this into your browser will open upSlack’s block kit builderwith the blocks
you created.UtilsCommon composite blocks are implemented for you inslack_blockkit.utilsand include:get_task_block- Returns a task block, which is comprised of a paragraph of text followed by an information link at the bottom.get_text_block_with_image- Returns a text block with an image to the right of it.get_text_block_with_accessory- Returns a text block with an accessory.get_information_block- Returns an information block, which is a section with an info icon followed by linked text.get_checkmark-Nota block, but rather an emoji string for a checkmark. PassTruefor a checked mark andFalsefor unchecked.