deegitalbe / laravel-trustup-io-slack-notifications
Basic versioned php package.
Installs: 2 922
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/deegitalbe/laravel-trustup-io-slack-notifications
Requires
Requires (Dev)
- larastan/larastan: ^1.0
- laravel/pint: ^1.25
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.5
README
This package enhances laravel slack notifications, allowing to send direct messages.
Installation
Require package
composer require deegitalbe/laravel-trustup-io-slack-notifications
Env variables
SLACK_API_TOKEN=
Usage
Configure your models
use Illuminate\Database\Eloquent\Model; use Deegitalbe\LaravelTrustupIoSlackNotifications\Traits\Slack\SlackNotifiable; use Deegitalbe\LaravelTrustupIoSlackNotifications\Contracts\Slack\SlackNotifiableContract; use Illuminate\Notifications\Notification; class User extends Model implements SlackNotifiableContract { use SlackNotifiable; }
Configure your notification
Extending slack notification
use Illuminate\Notifications\Messages\SlackMessage; use Deegitalbe\LaravelTrustupIoSlackNotifications\SlackNotification; use Deegitalbe\LaravelTrustupIoSlackNotifications\Enum\SlackChannel; class OrderReceived extends SlackNotification { public function slackChannel($notifiable): string|SlackChannel { return SlackChannel::PRODUCTS; } public function slackMessage(SlackMessage $message, $notifiable): SlackMessage { return $message->content("A new order has been made."); } }
Implementing contract and using trait
use Illuminate\Notifications\Messages\SlackMessage; use Deegitalbe\LaravelTrustupIoSlackNotifications\Enum\SlackChannel; use Deegitalbe\LaravelTrustupIoSlackNotifications\Traits\Slack\IsSlackNotification; use Deegitalbe\LaravelTrustupIoSlackNotifications\Contracts\Slack\SlackNotificationContract; class OrderReceived extends Notification implements SlackNotificationContract { use IsSlackNotification; public function slackChannel($notifiable): string|SlackChannel { return $notifiable->getSlackId(); } public function slackMessage(SlackMessage $message, $notifiable): SlackMessage { return $message->content("A new order has been made."); } }
Send notification to slack channel
// Create notification <?php namespace Deegitalbe\LaravelTrustupIoSlackNotifications\Tests\Utils\Notifications; use Deegitalbe\LaravelTrustupIoSlackNotifications\SlackChannelNotification; use Illuminate\Notifications\Messages\SlackMessage; class TestingNotification extends SlackChannelNotification { public function slackMessage(SlackMessage $message, $notifiable): SlackMessage { return $message->attachment(function ($attachment) { $attachment->color('#36a64f') ->author('🚨 New Activation Request') ->title("Tenant #test") ->fields([ 'Enterprise' => "test", 'Account Type' => "test", 'Admin First Name' => "test", 'Admin Last Name' => "test", 'VAT Number' => "test", 'Created At' => "test", ]) ->footer('TrustUp') ->footerIcon("test") ->timestamp(now()); }); } } // Send notification SlackChannel::TECH_DEV->notify($notification);