nathanheffley / laravel-slack-blocks
Slack Blocks support for laravel notifications.
Fund package maintenance!
nathanheffley
Installs: 395 201
Dependents: 0
Suggesters: 0
Security: 0
Stars: 49
Watchers: 3
Forks: 11
Open Issues: 1
Requires
- php: ^7.1.3|^8.0
- guzzlehttp/guzzle: ^6.0|^7.0
- laravel/slack-notification-channel: ^2.0
Requires (Dev)
- illuminate/notifications: ~5.8.0|^6.0|^7.0|^8.0
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0|^8.0|^9.0
README
This package is an extension of the official laravel/slack-notification-channel
package.
Note
Thes core notifications package has finally built-in support for these rich Slack messages, and as such you should use the official package if you are able.
https://laravel.com/docs/10.x/notifications#formatting-slack-notifications
Usage
Instead of requiring the official package, you should require this one instead.
composer require nathanheffley/laravel-slack-blocks
Because this package is built on top of the official one, you'll have all the functionality found in the official docs.
You can follow those instructions with the slight adjustment of requiring the classes from NathanHeffley\LaravelSlackBlocks
instead of Illuminate\Notifications
.
Everything supported in the base Illuminate Notifications classes is supported in these extended classes.
If you want to add a block to your Slack message, you need to add the block in an attachment.
use NathanHeffley\LaravelSlackBlocks\Messages\SlackMessage;
// ...
public function toSlack($notifiable)
{
return (new SlackMessage)
->attachment(function ($attachment) {
$attachment->block(function ($block) {
$block
->type('section')
->text([
'type' => 'mrkdwn',
'text' => '*Hello World!*',
]);
});
});
}
To see all the possible fields you can add to a block, check out the official Slack Blocks documentation.
To help, some blocks have been given dedicated helper functions on the attachment model itself. Currently there are methods for adding dividers and images.
(new SlackMessage)->attachment(function ($attachment) {
$attachment->imageBlock('http://placekitten.com/300/200', 'A cute kitten');
$attachment->dividerBlock();
$attachment->imageBlock('http://placekitten.com/300/200', 'A cute kitten', 'This is a titled cat image');
});