block_builder

Creating a Slack Builder Class


source

BlockBuilder

 BlockBuilder ()

Utility class for building Slack Block Kit elements. Provides methods to create various block types for Slack messages.

First, let’s start at the top of the message, which would be the message’s header:


source

BlockBuilder.create_header_block

 BlockBuilder.create_header_block (text:str)

*Create a header block for Slack messages.

Args: text: The text to display in the header

Returns: Slack block kit header object*

test_eq(BlockBuilder.create_header_block('My Text'),{'type': 'header',
 'text': {'type': 'plain_text', 'text': 'My Text', 'emoji': True}})

BlockBuilder.create_header_block('My Text')
{'type': 'header',
 'text': {'type': 'plain_text', 'text': 'My Text', 'emoji': True}}

That looks correct!

Now, what about making the various sections in our message? We basically just need another simple method that takes in the section text and outputs the right formating for the Slack API


source

BlockBuilder.create_section_block

 BlockBuilder.create_section_block (text:str,
                                    fields:Optional[List[Dict[str,Any]]]=N
                                    one)

*Create a section block for Slack messages.

Args: text: The text to display in the section fields: Optional list of field objects for the section

Returns: Slack block kit section object*

test_eq(BlockBuilder.create_section_block('My Section'),{'type': 'section', 'text': {'type': 'mrkdwn', 'text': 'My Section'}}
)

BlockBuilder.create_section_block('My Section')
{'type': 'section', 'text': {'type': 'mrkdwn', 'text': 'My Section'}}

OR, if we want the section to have some detial fields:

test_eq(BlockBuilder.create_section_block('My Section',['Some Field here']),
        {'type': 'section','text': {'type': 'mrkdwn', 'text': 'My Section'},'fields': ['Some Field here']})


BlockBuilder.create_section_block('My Section',['Some Field here'])
{'type': 'section',
 'text': {'type': 'mrkdwn', 'text': 'My Section'},
 'fields': ['Some Field here']}

Perfect! I guess we should should actually make a way to make fields then:


source

BlockBuilder.create_field

 BlockBuilder.create_field (title:str, value:str)

*Create a field for a section block.

Args: title: The title of the field (will be bolded) value: The value of the field

Returns: Field object for use in section blocks*

test_eq(BlockBuilder.create_field('The Top, Important Part','Less important Part'),
        {'type': 'mrkdwn', 'text': '*The Top, Important Part*\nLess important Part'})

BlockBuilder.create_field('The Top, Important Part','Less important Part')
{'type': 'mrkdwn', 'text': '*The Top, Important Part*\nLess important Part'}

That would be used for things like BlockBuilder.create_field(column_name,row_value) to add the column details to the message

Now that we can make an individual field, let’s make a way to take in multiple fields and turn them into a section since we’ll often want to show the values for multiple columns:


source

BlockBuilder.create_fields_section

 BlockBuilder.create_fields_section (fields_data:List[Tuple[str,str]],
                                     max_fields_per_section:int=10)

*Create one or more section blocks with fields.

Args: fields_data: List of (label, value) tuples max_fields_per_section: Maximum fields per section (Slack limit is 10)

Returns: List of section blocks*

Let’s pretend we are trying to make a section that has fiels for a center’s billing rate and company status since we send some alerts that do that:

BlockBuilder.create_fields_section([('Billing Rate','$13'),('Company Status','Advertiser')])
[{'type': 'section',
  'fields': [{'type': 'mrkdwn', 'text': '*Billing Rate*\n$13'},
   {'type': 'mrkdwn', 'text': '*Company Status*\nAdvertiser'}]}]

section_fields.png

Wow, that looks great!

Let’s also add a method for making context blocks since that is a subtle way to add text/information to a message.


source

BlockBuilder.create_context_block

 BlockBuilder.create_context_block (text:str)

*Create a context block for Slack messages.

Args: text: The text to show in the context block

Returns: Slack block kit context object*

test_eq(BlockBuilder.create_context_block('Here is some informaiton'),{'type': 'context','elements': [{'type': 'mrkdwn', 'text': 'Here is some informaiton'}]})

BlockBuilder.create_context_block('Here is some informaiton')
{'type': 'context',
 'elements': [{'type': 'mrkdwn', 'text': 'Here is some informaiton'}]}

We also probably want a way to make dividers since those are always helpful for layout messages:


source

BlockBuilder.create_divider

 BlockBuilder.create_divider ()

*Create a divider block for Slack messages.

Returns: Slack block kit divider object*

test_eq(BlockBuilder.create_divider(),{'type': 'divider'})

BlockBuilder.create_divider()
{'type': 'divider'}

Since would like to allow users to add metadata to messages (which is helpful for processing the messages by the slack bot), let’s also create a method for turning column values into message context:


source

BlockBuilder.create_metadata_context

 BlockBuilder.create_metadata_context
                                       (metadata_items:List[Tuple[str,Unio
                                       n[str,List,dict]]])

*Create a context block for metadata items.

Args: metadata_items: List of (label, value) tuples

Returns: Formatted metadata field*

test_eq(
    BlockBuilder.create_metadata_context([('Sheet Owner','Mary'),
                                  ('Sheet Editors',['Joe','John','Bill']),
                                  ('Budfet',{'Shared':False,'Uses Parent':False,'individual_budget':9000})]),
    {'metadata': 
        {'Sheet Owner': 'Mary',
        'Sheet Editors': ['Joe', 'John', 'Bill'],
        'Budfet': {
            'Shared': False,
            'Uses Parent': False,
            'individual_budget': 9000}}})


BlockBuilder.create_metadata_context([('Sheet Owner','Mary'),
                                  ('Sheet Editors',['Joe','John','Bill']),
                                  ('Budfet',{'Shared':False,'Uses Parent':False,'individual_budget':9000})])
{'metadata': {'Sheet Owner': 'Mary',
  'Sheet Editors': ['Joe', 'John', 'Bill'],
  'Budfet': {'Shared': False,
   'Uses Parent': False,
   'individual_budget': 9000}}}