定制 fuwasegu/slack-php-block-kit-next 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fuwasegu/slack-php-block-kit-next

Composer 安装命令:

composer require fuwasegu/slack-php-block-kit-next

包简介

OOP interface for writing Slack Block Kit messages and modals

README 文档

README

Slack PHP BlockKit NEXT

👉 For formatting messages and modals for Slack using their Block Kit syntax via an OOP interface 👈

Originally by Jeremy Lindblom (@jeremeamia)

Forked and maintained by Fuwasegu (@fuwasegu)

Warning!

This project is a fork of this, but deviates from the original project.

Introduction

From Slack's Block Kit documentation:

Block Kit is a UI framework for Slack apps that offers a balance of control and flexibility when building experiences in messages and other surfaces.

Customize the order and appearance of information and guide users through your app's capabilities by composing, updating, sequencing, and stacking blocks — reusable components that work almost everywhere in Slack.

This library provides an OOP interface in PHP for composing messages/modals using Slack Block Kit. It also does the reverse, meaning you can "hydrate" message/modal JSON into an object hierarchy.

Block Kit Concepts

This library helps you build Slack messages programmatically and dynamically in your code, but you need to know how they work generally first. The library does try to prevent you from doing things you are not permitted to do in Block Kit, but it does not validate or guard against every single rule.

You may want to review the following concepts in the Slack documentation:

In general, we refer to all of the different things in Block Kit collectively as "elements".

Installation

Install easily via Composer:

composer require fuwasegu/slack-php-block-kit-next

Then include the Composer-generated autoloader in your project's initialization code.

Note: This library is built for PHP 7.3+.

Basic Usage

This library supports an intuitive and fluid syntax for composing Slack surfaces (e.g., messages, modals). The Kit class acts as a façade to the library, and let's you start new messages/modals.

<?php

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 includes a convenience parameter for pretty printing.
echo $msg->toJson(true);

Fluent Interface

When using the fluent interface, every method that sets a property or adds a sub-element returns the original element's object, so you can chain additional method calls.

$msg = Message::new()
    ->text('Don\'t you just love XKCD?');
    ->divider();

Methods with a new prefix will return the new element's object, so be careful with how you are using the fluent interface in those cases.

// 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!

Tapping

Tapping is a way to keep the fluent interface going, but makes sure the whole message is preserved.

// 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!

Preview in Block Kit Builder

Slack provides an interactive Block Kit Builder for composing/testing messages and other surfaces. This is a great way to play around with and learn the Block Kit format.

The Kit::preview method allows you to render your message/surface as a Block Kit Builder URL, so you can link to a preview or your message/surface in the browser via their interactive tool. This will help you see how it would be rendered in a Slack client.

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

Output

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

And here's the actual Block Kit Builder link.

It will show up in the Block Kit Builder looking something like this:

Screenshot of rendered message in Block Kit Builder

Surface Hydration

Some Slack application integrations (such as with Modals) require receiving the JSON of an existing surface and then modifying or replacing that surface with another. You can "hydrate" the JSON of a surface (or element) into its object representation using its fromArray method (or fromJson).

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

Message Formatting

The Formatter class exists to provide helpers for formatting "mrkdwn" text. These helpers can be used so that you don't have to have the Slack mrkdwn syntax memorized. Also, these functions will properly escape <, >, and & characters automatically, if it's needed.

Example:

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

Example Result:

{
  "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>."
      }
    }
  ]
}

Virtual Elements

In addition to the standard Block Kit elements, the following are virtual/custom elements composed of one or more blocks:

  • TwoColumnTable - Uses Sections with Fields to create a two-column table with an optional header.

Class Structure

The Kit façade provides ways to create surfaces. Surfaces contain one or more blocks. Blocks are the primary element of the Block Kit. Blocks contain other elements, including other blocks, inputs (interactive elements), and partials (element parts that are not uniquely identifiable).

UML diagram for slack-block-kit

See the YUML
[Kit]-creates>[Surface]
[Surface]^[Message]
[Surface]^[Modal]
[Surface]^[AppHome]
[Surface]^[Attachment]
[Element]^[Surface]
[Element]^[Block]
[Element]^[Input]
[Element]^[Partial]
[Surface]<>->[Block]
[Message]<>->[Attachment]
[Block]<>->[Input]
[Block]<>->[Partial]
[Input]-[note:Examples: Button
DatePicker {bg:cornsilk}]
[Partial]-[note: Examples: Text
Fields {bg:cornsilk}]
[Block]-[note: Examples: Section
Actions {bg:cornsilk}]

fuwasegu/slack-php-block-kit-next 适用场景与选型建议

fuwasegu/slack-php-block-kit-next 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.02k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 fuwasegu/slack-php-block-kit-next 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fuwasegu/slack-php-block-kit-next 我们能提供哪些服务?
定制开发 / 二次开发

基于 fuwasegu/slack-php-block-kit-next 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-19