montopolis/montopolis-slack

此包已被弃用且不再维护。未建议替代包。
关于此包最新版本(2.0.0)没有可用的许可信息。

在Montopolis应用和第三方Slack客户端库之间提供一层抽象层。

2.0.0 2022-12-19 10:23 UTC

This package is auto-updated.

Last update: 2024-05-19 13:21:16 UTC


README

为Slack的chat.postMessage API提供一个一致、可靠的包装器,可以在所有Montopolis应用中使用。

设置Slack应用

在使用库之前,您需要设置一个OAuth启用的Slack应用。您可以在这里进行设置。

  1. 点击“OAuth & Permissions”
  2. 选择作用域:“作为___应用(chat:write:bot)发送消息”
  3. 复制页面顶部的OAuth访问令牌。以下指令将使用此令牌。

通用用法

<?php

// For convenience, it's recommended that you wrap the `Fluent` helper in a global function: 
 
function slack(): \Montopolis\Slack\Fluent 
{
    $config = new \Montopolis\Slack\Infrastructure\ArraySlackConfigurationRepository([
        'slack' => [
            'token' => '___oauth-token-from-above___', 
            'default_channel' => 'general',
            'fallback_to_default' => true
        ],
    ]);
    $transformer = new \Montopolis\Slack\Application\MessageTransformer();
    $client = new \Montopolis\Slack\Infrastructure\HttpSlackClient($config, $transformer);
    return new \Montopolis\Slack\Fluent($client);
}

// It can then be used as such:

slack()
    ->channel('support')
    ->text('This is the Slack Message')
    ->send();

Laravel用法

与Laravel的唯一区别是我们通常会依靠应用容器为我们解决依赖关系。

<?php

    // In AppServiceProvider.php:...
    public function register()
    {
        $this->app->bind(\Montopolis\Slack\Fluent::class, function ($app) {
            
            // This assumes config/services.php has a `slack` key containing `token` and `default_channel`:
            $config = new \Montopolis\Slack\Infrastructure\ArraySlackConfigurationRepository(config('services'));
            
            $transformer = new \Montopolis\Slack\Application\MessageTransformer();
            $client = new \Montopolis\Slack\Infrastructure\HttpSlackClient($config, $transformer);
            return new \Montopolis\Slack\Fluent($client);
        });
    }
    // etc...
    
    // In helpers.php:...
    function slack(): \Montopolis\Slack\Fluent 
    {
        return app()->make(\Montopolis\Slack\Fluent::class);
    }
    // etc...
    
    // In application:
    slack()
        ->channel('support')
        ->text('This is sent from a Laravel app')
        ->send();

发送块

您可以使用Block Kit Builder来模板/布局您的块。它应作为PHP数组(不是 JSON)发送,如下所示

<?php

    slack()
        ->channel('support')
        ->blocks([
            [
                "type" => "section",
                "text" => [
                    "type" => "mrkdwn",
                    "text" => "Hello, Assistant to the Regional Manager Dwight! *Michael Scott* wants to know where you'd like to take the Paper Company investors to dinner tonight.\n\n *Please select a restaurant:*"
                ],
            ],
            [
                "type" => "divider",
            ],
            [
                "type" => "section",
                "text" => [
                    "type" => "mrkdwn",
                    "text" => "*Farmhouse Thai Cuisine*\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here"
                ],
                "accessory" => [
                    "type" => "image",
                    "image_url" => "https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg",
                    "alt_text" => "alt text for image"
                ],
            ],
            [
                "type" => "actions",
                "elements" => [
                    [
                        "type" => "button",
                        "text" => [
                            "type" => "plain_text",
                            "text" => "Farmhouse",
                            "emoji" => true,
                        ],
                        "value" => "click_me_123",
                    ],
                ]
            ],
        ]);

运行测试

./vendor/bin/phpunit tests/
PHPUnit 8.3.3 by Sebastian Bergmann and contributors.

............                                                      12 / 12 (100%)

Time: 105 ms, Memory: 6.00 MB

OK (12 tests, 12 assertions)

许可

MIT许可证(MIT)。有关更多信息,请参阅许可文件