PHP code example of fuwasegu / slack-php-block-kit-next
1. Go to this page and download the library: Download fuwasegu/slack-php-block-kit-next 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/ */
fuwasegu / slack-php-block-kit-next example snippets
use SlackPhp\BlockKit\Kit;
use SlackPhp\BlockKit\Surfaces\Message;
// ...
// You can start a message from the `Kit` class.
$msg = Kit::newMessage();
// OR via the surface class's "new" method.
$msg = Message::new();
// Then you can add blocks using the surface's available methods.
$msg->text('Don\'t you just love XKCD?');
$msg->divider();
$msg->newImage()
->title('Team Chat')
->url('https://imgs.xkcd.com/comics/team_chat.png')
->altText('Comic about the stubbornness of some people switching chat clients');
// To convert to JSON (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
$msg = Message::new()
->text('Don\'t you just love XKCD?');
->divider();
// Correctly renders the whole message.
$msg = Message::new()
->text('Don\'t you just love XKCD?')
->divider();
$msg->newImage()
->title('Team Chat')
->url('https://imgs.xkcd.com/comics/team_chat.png')
->altText('Comic about the stubbornness of some people switching chat clients');
echo json_encode($msg);
// YAY!
// INCORRECT: Renders just the image, because only that element gets stored in the variable.
$msg = Message::new()
->text('Don\'t you just love XKCD?')
->divider()
->newImage()
->title('Team Chat')
->url('https://imgs.xkcd.com/comics/team_chat.png')
->altText('Comic about the stubbornness of some people switching chat clients');
echo json_encode($msg);
// WHOOPS!
// Correctly renders the whole message, by using tap()
$msg = Message::new()
->text('Don\'t you just love XKCD?')
->divider()
->tap(function (Message $msg) {
$msg->newImage()
->title('Team Chat')
->url('https://imgs.xkcd.com/comics/team_chat.png')
->altText('Comic about the stubbornness of some people switching chat clients');
});
echo json_encode($msg);
// YAY!
$msg = Kit::newMessage()
->text('Don\'t you just love XKCD?')
->divider()
->tap(function (Message $msg) {
$msg->newImage()
->title('Team Chat')
->url('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.
$fmt = Kit::formatter();
$msg = Kit::newMessage()->text($fmt->sub(
'Hello, {audience}! On {date}, {host} will be hosting an AMA in the {channel} channel at {time}.',
[
'audience' => $fmt->atHere(),
'date' => $fmt->date($event->timestamp),
'host' => $fmt->user($event->hostId),
'channel' => $fmt->channel($event->channelId),
'time' => $fmt->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>."
}
}
]
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.