A small package that is used to create blocks in Slack messaging app
Project description
Slack Message Blocks Simplified Package
Overview
This package provides a set of classes and functions for creating and managing Slack message blocks programmatically. It includes various block types such as headers, sections, images, dividers, and context blocks, which can be used to construct complex Slack messages. The package also provides utility functions for validating key-value pairs to ensure that all necessary data is present for constructing these blocks.
Installation
Prerequisites
- Python Version: Ensure you are using Python 3.7 or higher.
- Dependencies:
dataclasses
(built-in for Python 3.7+)typing
(for type hinting)python-dotenv
(for environment variable management)
Setup
-
Install Dependencies:
- Install the required Python packages using
pip
: ```bash pip install python-dotenv ```
- Install the required Python packages using
-
Slack Client:
- The package assumes the existence of a
SlackClient
class in a module namedslack_client
. This class should handle the interaction with Slack's API. Ensure you have implemented or imported this class.
- The package assumes the existence of a
-
Environment Setup:
- If you're using environment variables for Slack credentials or other settings, create a
.env
file in the root of your project:SLACK_API_TOKEN=xoxb-your-slack-token
- If you're using environment variables for Slack credentials or other settings, create a
Usage
Importing the Package
```python from your_package_name import BaseBlock, DividerBlock, HeaderBlock, ContextBlock, SectionBlock, ImageBlock, RichTextBlock, SlackBlock, check_type_keys ```
Creating a Simple Message Block
Here’s an example of how to create and send a simple message block using this package:
```python from slack_client import SlackClient from your_package_name import SlackBlock, HeaderBlock, DividerBlock, SectionBlock
Initialize the Slack client
client = SlackClient(api_token="xoxb-your-slack-token")
Create a header block
header = HeaderBlock(title="Welcome to the Channel!")
Create a section block
section = SectionBlock() section.change_value(type="plain_text", text="We're glad you're here!")
Create a divider block
divider = DividerBlock()
Create the Slack block and add the blocks
slack_message = SlackBlock(client=client) slack_message.add_blocks([header, section, divider])
Post the message to a channel
slack_message.post_message_block(channel_id="C1234567890") ```
Using check_type_keys
for Validation
The check_type_keys
function is used to validate that all necessary keys are present in your input data:
```python from your_package_name import check_type_keys
required_keys = { "image": ["image_url", "alt_text"], "plain_text": ["text"] }
received_values = { "type": "image", "image_url": "https://example.com/image.png" }
check_type_keys("type", required_keys, received_values) ```
Available Classes and Functions
Classes
- BaseBlock: Abstract base class for all Slack blocks.
- DividerBlock: Represents a Slack divider block.
- HeaderBlock: Represents a header block in a Slack message.
- ContextBlock: Represents a context block in a Slack message.
- SectionBlock: Represents a section block in a Slack message.
- ImageBlock: Represents an image block in a Slack message.
- RichTextBlock: Represents a rich text block in a Slack message.
- SlackBlock: Represents a complete message block to be sent via Slack.
Functions
- check_type_keys: Validates that necessary keys are present in the input data based on a base key's value.
Contributing
If you'd like to contribute to this project, please fork the repository and submit a pull request. All contributions are welcome!
License
This project is licensed under the MIT License.
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
Built Distribution
Hashes for slack_message_blocks_simplified-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e151d3eaa30d30d18f72a32a904a47ba2d6adec3ef05d608ef68ef73c6b90ced |
|
MD5 | 703b10198bb659bd4b4a65d6fa269bbd |
|
BLAKE2b-256 | 6233fa5bbe33b7014387c4bdc3e6af0fab0bb1b077f7e1ea28f30321d843b672 |
Hashes for slack_message_blocks_simplified-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b436e0c8c23a78893de2d338956c75625153c91d8b6402123d94ca493435ad3 |
|
MD5 | fa7e82dad02619c65653b3c4bfe609d9 |
|
BLAKE2b-256 | b9264d82cae455e73a264e36338803cdbfda699d8c6eabcff0ca955e8d6efd00 |