cian / slack
一个用于向 Slack 发送消息的 PHP 包。
1.2.0
2020-11-09 16:06 UTC
Requires
- php: >=7.0
- guzzlehttp/guzzle: >=6.0
- illuminate/support: ^5|^6|^7|^8
Requires (Dev)
- mockery/mockery: ^1
- phpunit/phpunit: ^5
This package is auto-updated.
Last update: 2024-09-10 00:22:02 UTC
README
要求
- PHP 7.0+
安装
composer require cian/slack
Laravel
php artisan vendor:publish --provider="Cian\Slack\LaravelServiceProvider"
如果你的 Laravel 版本 <= 5.4
,别忘了添加服务提供者。
// /config/app.php [ "providers" => [ // other providers ... Cian\Slack\LaravelServiceProvider::class ] ]
Slack 方法
Slack 有许多 有用的方法。
可用方法
- users.list
- users.lookupByEmail
此库中还将添加更多方法。
users.list
use Cian\Slack\SlackMethod; $token = 'your-app-token'; $response = (new SlackMethod) ->setToken($token) ->usersList();
users.lookupByEmail
use Cian\Slack\SlackMethod; $token = 'your-app-token'; $response = (new SlackMethod) ->setToken($token) ->usersLookupByEmail($email);
注意,如果你从 LaravelServiceProvider 获取 SlackMethod,InteractiveMessage,
你不需要在 API 调用之前调用setToken
。
IncomingWebhook
这是一个发送基本 incoming webhook 的示例。
对于更复杂的情况,你需要使用 BlockBuilder
或 AttachmentBuilder
。
use Cian\Slack\IncomingWebhook; $url = 'https://hooks.slack.com/services/path/to/your/incoming-webhook/url'; (new IncomingWebhook)->send($message, $url);
交互式消息
要使用交互式消息,你需要设置你的应用 OAuth & Permissions
。之后,你可以发送如下消息。
use Cian\Slack\InteractiveMessage; $token = 'your-app-token'; // $channel can be channel_name, channel_id, user_slack_id // but Slack suggests not to use channel_name. $channel = 'development'; $message = 'Hello Interactive Message!'; (new InteractiveMessage([ 'token' => $token, 'channel' => $channel ]))->send($message); // or (new InteractiveMessage) ->setToken($token) ->to($channel) ->send($message);
块
Slack 建议使用 块
而不是 附件
因为 块
比 附件
更灵活。
use Cian\Slack\IncomingWebhook; use Cian\Slack\Builders\BlockBuilder; $url = 'https://hooks.slack.com/services/path/to/your/incoming-webhook/url'; $builder = (new BlockBuilder) ->section('*A Title Here*') ->section('body content ...') ->divider() ->section('😗😗😗'); (new IncomingWebhook)->send($builder, $url)
附件
即使 Slack 建议使用 块
而不是 附件
,
它也不会删除 附件
。
附件
有很多字段,但它们都是遗留的。
查看 Slack 附件文档以获取更多信息
使用附件的最佳方式是只保留两个字段,blocks
和 color
。
use Cian\Slack\IncomingWebhook; use Cian\Slack\Builders\BlockBuilder; use Cian\Slack\Builders\AttachmentBuilder; $blockBuilder = (new BlockBuilder)->section('How are you?'); // when you provide block builder as the first argument // the second argument color will be applied // the color can be 'good', 'warning', 'danger' or any valid hex color code. $attachments = (new AttachmentBuilder($blockBuilder, '#ff0000')); $url = 'https://hooks.slack.com/services/path/to/your/incoming-webhook/url'; (new IncomingWebhook) ->send($attachments, $url);
假设你仍然需要遗留字段,
你可以像下面这样做。
use Cian\Slack\IncomingWebhook; use Cian\Slack\Builders\AttachmentBuilder; $attachments = (new AttachmentBuilder)->add([ 'text' => '😗😗😗', 'fallback' => 'fall back text...', 'footer' => 'footer text...', 'color' => 'danger' // ... more legacy fields ]); $url = 'https://hooks.slack.com/services/path/to/your/incoming-webhook/url'; (new IncomingWebhook) ->send($attachments, $url);
假设你面临一个非常复杂的情况,
你需要同时使用块和附件。
use Cian\Slack\Message; use Cian\Slack\IncomingWebhook; use Cian\Slack\Builders\BlockBuilder; use Cian\Slack\Builders\AttachmentBuilder; $titleBlocker = (new BlockBuilder) ->section('Title row 😗😗😗'); $bodyBlocker = (new BlockBuilder) ->section('body content ...') ->divider(); $attachmenter = (new AttachmentBuilder($bodyBlocker, '#ff00ff')); $message = new Message($titleBlocker); $message->setAttachments($attachmenter); $url = 'https://hooks.slack.com/services/path/to/your/incoming-webhook/url'; (new IncomingWebhook) ->send($message, $url);
尽管我在文档示例中使用了 IncomingWebhook
,
但它可以被 InteractiveMessage
替换。
交互式组件
按钮
use Cian\Slack\IncomingWebhook; use Cian\Slack\Builders\BlockBuilder; use Cian\Slack\Builders\ElementBuilder; $text = 'Approve'; $actionId = 'approve_request'; $value = ['foo' => 'bar']; // optional default '' $style = 'primary'; // optional default 'primary' $type = 'plain_text'; // optional default `plain_text` $button = ElementBuilder::makeButton($text, $actionId, $value, $style, $type); // or $button = (new ElementBuilder)->button(/** same as makeButton */); $blocker = (new BlockBuilder) ->section('*Can I buy a toy ?*') ->divider() ->actions([$button]); $url = 'https://hooks.slack.com/services/path/to/your/incoming-webhook/url'; (new IncomingWebhook)->to($url)->send($blocker); // or (new IncomingWebhook)->send($blocker, $url);