### Example Block Kit Builder URL Output Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md This is an example of the URL string generated by the `Kit::preview` method. This URL can be shared or opened in a browser to view the rendered Slack message within the interactive Block Kit Builder tool. ```text https://app.slack.com/block-kit-builder#%7B"blocks":%5B%7B"type":"section"%2C"text":%7B"type":"mrkdwn"%2C"text":"Don%27t%20you%20just%20love%20XKCD%3F"%7D%7D%2C%7B"type":"divider"%7D%2C%7B"type":"image"%2C"title":%7B"type":"plain_text"%2C"text":"Team%20Chat"%7D%2C"image_url":"https:%5C%2F%5C%2Fimgs.xkcd.com%5C%2Fcomics%5C%2Fteam_chat.png"%2C"alt_text":"Comic%20about%20the%20stubbornness%20of%20some%20people%20switching%20chat%20clients"%7D%5D%7D ``` -------------------------------- ### Install Slack Block Kit PHP Library via Composer Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md This command installs the `slack-php/slack-block-kit` library using Composer, PHP's dependency manager. It's the essential first step to integrate the library into a PHP project, ensuring all necessary dependencies are resolved. ```bash composer require slack-php/slack-block-kit ``` -------------------------------- ### Create Slack Messages with Kit Façade (PHP) Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md The `Kit` class provides a façade for the Slack Block Kit library, offering factory methods for components. This example demonstrates building a message with blocks using named parameters, reducing the number of required imports. ```php ephemeral() ->blocks( Section::new('Don\'t you just love XKCD?'), Divider::new(), BlockImage::new() ->title('Team Chat') ->imageUrl('https://imgs.xkcd.com/comics/team_chat.png') ->altText('Comic about the stubbornness of some people switching chat clients') ), ); ``` -------------------------------- ### Example JSON Output for Formatted Slack Message Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md This JSON snippet shows the expected output structure when using the `Md` class for message formatting. It demonstrates how the `mrkdwn` text field is populated with Slack's specific formatting syntax, including user mentions, channel links, and date/time formatting. ```json { "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "Hello, ! On , <@U12345678> will be hosting an AMA in the <#C12345678> channel at ." } } ] } ``` -------------------------------- ### Set Component Properties to Null (PHP) Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md This example shows how to explicitly set a component property to `null`. This action effectively removes the property's value, which is useful for clearing existing data or ensuring a property is not included in the final output. ```php $section->accessory(null); ``` -------------------------------- ### Formatting Slack Mrkdwn Text with Md Class in PHP Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md This example illustrates how to use the `Md` class from the Slack Block Kit library to format 'mrkdwn' text. It simplifies the process of creating Slack-compatible markdown by providing helper functions and automatically escaping special characters like `<`, `>`, and `&`. ```php // Note: $event is meant to represent some kind of DTO from your own application. $md = Kit::md(); $msg = Kit::message( blocks: [ Kit::section( text: $md->sub( 'Hello, {audience}! On {date}, {host} will be hosting an AMA in the {channel} channel at {time}.', [ 'audience' => $md->atHere(), 'date' => $md->date($event->timestamp), 'host' => $md->user($event->hostId), 'channel' => $md->channel($event->channelId), 'time' => $md->time($event->timestamp), ] ) ) ] ); ``` -------------------------------- ### Construct, Validate, and Output Slack Block Kit Message in PHP Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md This PHP example demonstrates the core functionality of the Slack Block Kit library. It illustrates how to programmatically construct a Slack message using various Block Kit components, validate its structure for compliance, and convert the resulting object into JSON or an array suitable for Slack API interactions. ```php validate(); // OUTPUT // To convert to JSON (e.g., to send to Slack API, webhook, or response_url), use PHP\'s `json_encode()` function. echo json_encode($msg); // OR you can use the surfaces\'s `toJson()` method, which also includes a convenience parameter for pretty printing. echo $msg->toJson(true); // OR you can just convert to an array and do something else with the data. print_r($msg->toArray()); ``` -------------------------------- ### Generate Block Kit Builder Preview URL (PHP) Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md Slack's Block Kit Builder is an interactive tool for composing and testing messages. This code shows how to use the `Kit::preview` method to generate a URL that opens your constructed message directly in the Block Kit Builder, facilitating easy preview and debugging. ```php $msg = Kit::message( ephemeral: true, blocks: [ Kit::section('Don\'t you just love XKCD?'), Kit::divider(), Kit::blockImage( title: 'Team Chat', imageUrl: 'https://imgs.xkcd.com/comics/team_chat.png', altText: 'Comic about the stubbornness of some people switching chat clients', ), ] ); echo Kit::preview($msg); ``` -------------------------------- ### Build Slack Messages with Fluent Interface using Kit Façade (PHP) Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md This snippet illustrates the fluent interface pattern, allowing component properties to be set via chainable setter methods. It shows how to construct a Slack message using the `Kit` façade with this fluent style instead of named parameters. ```php $msg = Kit::message() ->ephemeral() ->blocks( Kit::section('Don\'t you just love XKCD?'), Kit::divider(), Kit::blockImage() ->title('Team Chat') ->imageUrl('https://imgs.xkcd.com/comics/team_chat.png') ->altText('Comic about the stubbornness of some people switching chat clients') ), ); ``` -------------------------------- ### Slack Block Kit Component Class Hierarchy API Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md This documentation outlines the core class structure and relationships within the Slack Block Kit library. It defines `Component` types like `Surface`, `Block`, `Element`, and `Part`, explains the role of the `Kit` façade, and describes how components are nested to define appearance and behavior. ```APIDOC Class Structure: - Surfaces, Blocks, Elements, and Parts are all types of Block Kit Components. - The Kit façade can be used as a factory to create any Component. - Components contains properties and sub-Components that define their appearance and behavior. - Parts are repeated patterns that form the properties of other Components. - Surfaces contain one or more Blocks. - Blocks contain one or more Elements. - Most Elements are interactive in some way. - Many Elements are also Inputs that receive user input. UML Diagram (YUML): [Kit]-creates>[Component] [Surface]^[Message] [Surface]^[Modal] [Surface]^[AppHome] [Surface]^[Attachment] [Component]^[Surface] [Component]^[Block] [Component]^[Element] [Component]^[Part] [Component]<>->[Part] [Surface]<>->[Block] [Message]<>->[Attachment] [Block]<>->[Element] [Element]^[Input] [Element]-[note:Examples: Button, OverflowMenu {bg:cornsilk}] [Input]-[note:Examples: Select Menu, DatePicker {bg:cornsilk}] [Part]-[note: Examples: Text, Fields {bg:cornsilk}] [Block]-[note: Examples: Section, Actions {bg:cornsilk}] ``` -------------------------------- ### Use Conditional Items in Block Lists (PHP) Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md This snippet demonstrates how to safely include conditional items within lists of blocks, elements, or options. Null values are permitted in these lists, allowing for flexible message construction based on runtime conditions without causing errors. ```php $msg = Kit::message( blocks: [ Kit::section('Don\'t you just love XKCD?'), ($includeDivider) ? Kit::divider() : null, Kit::blockImage( title: 'Team Chat', imageUrl: 'https://imgs.xkcd.com/comics/team_chat.png', altText: 'Comic about the stubbornness of some people switching chat clients', ), ] ); ``` -------------------------------- ### Hydrating Slack Block Kit Components from JSON in PHP Source: https://github.com/slack-php/slack-php-block-kit/blob/main/README.md This snippet demonstrates how to convert Slack Block Kit JSON into PHP object representations. It shows two methods: `fromArray` for hydrating from a PHP array (decoded JSON) and `fromJson` for hydrating directly from a JSON string. This is useful for modifying or replacing existing Slack surfaces. ```php $messageJson = <<< { "blocks": [ { "type": "section", "block_id": "block1", "text": { "type": "mrkdwn", "text": "*foo bar*" } } } } ; // Use fromArray to hydrate the message from parsed JSON data. $decodedMessageJson = json_decode($messageJson, true); $message = Message::fromArray($decodedMessageJson); // OR... use fromJson to hydrate from a JSON string. $message = Message::fromJson($messageJson); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.