beyondcode / slack-notification-channel
Slack Notification Channel for Laravel using API tokens.
Installs: 494 988
Dependents: 0
Suggesters: 0
Security: 0
Stars: 86
Watchers: 6
Forks: 15
Open Issues: 2
Requires
- php: ^7.3.0|^8.0|^8.1
- guzzlehttp/guzzle: ^6.0|^7.0
- illuminate/notifications: ^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- mockery/mockery: ^1.3
- phpunit/phpunit: ^8.0|^9.0|^10.0
README
This is the Laravel Slack notification channel, but instead of using incoming webhooks, this channel makes use of OAuth access tokens. It also allows replies to thread messages.
If you want to learn how to create reusable PHP packages yourself, take a look at my upcoming PHP Package Development video course.
Usage
Install the package via composer:
composer require beyondcode/slack-notification-channel
The service provider gets registered automatically and you can use this package as a replacement of the core Laravel Slack notification channel.
Notification Routing
Since this notification channel makes use of Slack API tokens instead of incoming webhook URLs, you need to return an array containing the API token and an optional channel.
This channel will be used, if it is not provided in the SlackMessage
that you send:
public function routeNotificationForSlack() { return [ 'token' => 'xoxp-slack-token', 'channel' => '#general' ]; }
Replying to Message Threads
Assuming you want to keep track of orders and have your team/bot respond to a single thread of per order placed, this channel allows you to retrieve the API response from the chat.postMessage method inside your notifications. With this you could post messages on order paid, shipped, closed, etc. events to the same thread.
In your order placed event you can have
public function toSlack($notifiable) { return (new SlackMessage) ->content('A new order has been placed'); } public function response($response) { $response = $response->getBody()->getContents(); $this->order->data('slack.thread_ts', json_decode($response, true)['ts']); }
And in your order paid event you can have
public function toSlack($notifiable) { $order = $this->order; return (new SlackMessage) ->success() ->content('Order paid') ->threadTimestamp($order->data('slack.thread_ts')) ->attachment(function ($attachment) use ($order) { $attachment->title("Order $order->reference has been paid for.") ->content('Should now be processed.') ->action('View Order', route('orders', $order->reference)); }); }
Customizing the channel name
Laravel ships with a slack notification channel which uses web hooks. This packages overwrites that default slack channel.
Should you want to use Laravel's default Slack channel and this one inside one app, you'll need to use a different channel name.
You can set the channel name using
SlackApiChannel::$channelName = 'alternativeSlackChannel'
Make sure you use the right method name on your notifications.
class AlternativeSlackChannelNameNotification extends Notification { public function toAlternativeSlackChannel($notifiable) { // ... } }
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.