PHP code example of slack-php / slack-block-kit

1. Go to this page and download the library: Download slack-php/slack-block-kit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

slack-php / slack-block-kit example snippets




use SlackPhp\BlockKit\Surfaces\Message;
use SlackPhp\BlockKit\Blocks\Section;
use SlackPhp\BlockKit\Blocks\Divider;
use SlackPhp\BlockKit\Blocks\BlockImage;

// ...

// CONSTRUCTION
$msg = new Message(
    ephemeral: true,
    blocks: [
        new Section('Don\'t you just love XKCD?'),
        new Divider(),
        new BlockImage(
            title: 'Team Chat',
            imageUrl: 'https://imgs.xkcd.com/comics/team_chat.png',
            altText: 'Comic about the stubbornness of some people switching chat clients',
        ),
    ]
);

// VALIDATION
// Throws a ValidationException if any of the components are invalid or missing 



use SlackPhp\BlockKit\Kit;

// ...

$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',
        ),
    ]
);

$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')
        ),
    );

$msg = Message::new()
    ->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')
        ),
    );

$section->accessory(null);

$msg = Kit::message(
    blocks: [
        Kit::section('Don\'t you just love XKCD?'),
        ($d.com/comics/team_chat.png',
            altText: 'Comic about the stubbornness of some people switching chat clients',
        ),
    ]
);

$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);

$messageJson = <<<JSON
{
    "blocks": [
        {
            "type": "section",
            "block_id": "block1",
            "text": {
                "type": "mrkdwn",
                "text": "*foo bar*"
            }
        }
    }
}
JSON;

// 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);

// 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),
                ]
            )
        )
    ]
);
json
{
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "Hello, <!here>! On <!date^1608322949^{date}|2020-12-18T20:22:29+00:00>, <@U12345678> will be hosting an AMA in the <#C12345678> channel at <!date^1608322949^{time}|2020-12-18T20:22:29+00:00>."
      }
    }
  ]
}