jocoonopa / laravel-slack
Laravel 应有的 Slack 通知。
Requires
- php: >=7.1.3
- guzzlehttp/guzzle: ^6.3 || ^7.0
- illuminate/notifications: ^5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- illuminate/support: ^5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- laravel/slack-notification-channel: ^3.1
Requires (Dev)
- orchestra/testbench: *
- phpunit/phpunit: ^7.0 || ^8.5.28 || ^9.0 || ^10.0
This package is auto-updated.
Last update: 2024-09-16 17:34:38 UTC
README
关于 Laravel Slack
Laravel 应有的 Slack 通知。简单、快速、简单,并且 高度可测试。由于它使用按需通知,因此需要 Laravel 5.5 或更高版本。
安装
在 composer.json 中要求此包并更新您的依赖项
composer require gpressutto5/laravel-slack
由于此包支持 Laravel 的包自动发现,因此您不需要手动注册 ServiceProvider。
之后,发布配置文件
php artisan vendor:publish --provider="Pressutto\LaravelSlack\ServiceProvider"
您需要为您的 Slack 团队配置一个 "Incoming Webhook" 集成。
配置
在发布的配置文件 config/laravel-slack.php
中,您可以更改 Webhook URL、默认频道、应用程序名称和应用程序图像等选项。
出于安全原因,您不应提交 Webhook URL,因此此包默认将使用环境变量 SLACK_WEBHOOK_URL
。您只需将其添加到 .env
文件中。如下所示
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX
用法
您可以发送简单的 Slack 消息,如下所示
- 向频道发送消息
\Slack::to('#finance')->send('Hey, finance channel! A new order was created just now!');
- 向用户发送消息
\Slack::to('@joe')->send("Hey Joe! It looks like you've forgotten your password! Use this token to recover it: as34bhdfh");
- 向多个用户发送消息
\Slack::to(['@zoe', '@amy', '@mia'])->send('I swear, honey, you are the only one... :heart:'); // ↑ look at this array ↑
- 混合使用
\Slack::to('#universe', '@god', '#scientists')->send(':thinking_face:'); // ↑ what? I don't need that array? ↑
- 无接收者
\Slack::send('Default message to the default channel, set on config/laravel-slack.php.');
- 发送 SlackMessage 对象
class HelloMessage extends SlackMessage { public $content = "Hey bob, I'm a sending a custom SlackMessage"; public $channel = '@bob'; } \Slack::send(new SlackMessage());
-
向用户发送
只要对象具有
slack_channel
属性,您就可以将其用作接收者。如果您正在使用模型,只需创建列slack_channel
并将@username
或#channel
名称存储在其中。如果已经存储但列不同,则可以创建方法getSlackChannelAttribute
。
class User extends Model { public function getSlackChannelAttribute(): string { return $this->attributes['my_custom_slack_channel_column']; } } \Slack::to(User::where('verified', true))->send('Sending message to all verified users!');
- 通过指定 webhook 发送消息
\Slack::to('#finance')->webhook('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX')->send('Hey, finance channel! A new order was created just now!');
测试
在测试时,您可以通过调用 Slack::fake()
轻松模拟 Slack 服务,它将返回一个 SlackFake
对象,该对象不会实际发送任何消息,并将它们保存到数组中。您可以通过调用 Slack::sentMessages()
来获取此数组。
此类还有一些辅助方法,您可以在测试中使用
- 断言至少发送了一条包含内容 'fake' 的消息
Slack::assertSent(function (SlackMessage $message) { return $message->content === 'fake'; });
- 断言至少发送了两条内容超过 5 个字符的字符串的消息
Slack::assertSent(function (SlackMessage $message) { return strlen($message->content) >= 100; }, 2);
- 断言恰好发送了包含内容 'test' 的五条消息
Slack::assertSent(function (SlackMessage $message) { return strpos($message->content, 'test') !== false; }, 5, true);
- 断言恰好发送了三条消息
Slack::assertSentCount(3);
由于此包使用 illuminate/notifications
发送通知,因此您可以模拟通知服务而不是 Slack 服务,并在测试中使用 NotificationFake
类。请参阅 此处。