rulecom / notifier
Send notification via Rule and other providers
Installs: 29 538
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 9
Forks: 0
Open Issues: 0
Requires
- php: ~5.5|~7.0|~8.1
- guzzlehttp/guzzle: ~6.2|^7.0
- illuminate/support: ~5.1|~6.2|~7.2|^8.38|^9.48
- monolog/monolog: ^1.21|^2.0
Requires (Dev)
- phpunit/phpunit: ~4.0||~5.0
- scrutinizer/ocular: ~1.1
- squizlabs/php_codesniffer: ~2.3
This package is auto-updated.
Last update: 2024-10-29 05:09:19 UTC
README
Send notifications via Rule (email/text message) and Slack. Inspired by Laravels notification system and can be used with the Laravel framework or completely indipendent.
Install
Via Composer
$ composer require rulecom/notifier
Usage
To send notification you need to create notification objects. These objects are responsible for telling the Notifier via which channels the notification message should be sent through and what each corresponding channel message should contain.
use RuleCom\Notifier\Channels\Email; use RuleCom\Notifier\Channels\Slack; class UserHasRegistered { /** * Here we specify through which channels we want to * send our notification. */ public function via() { return ['email', 'slack']; } /** * Each via method needs a correspondng "to" method. */ public function toEmail() { // Specify what the email message should contain. } /** * Each via method needs a correspondng "to" method. */ public function toSlack() { // Specify what the Slack message should contain. } } // To send the notification to all specified channels: $notifier = new RuleCom\Notifier\Notifier(); $notifier->send(new UserHasRegistered());
Channels
Currently this package supports the following channel providers:
Email via (Rule):
public function toEmail() { return (new RuleCom\Notifier\Channels\Email(new GuzzleHttp\Client())) ->apikey('YOUR-RULE-API-KEY') // If using Laravel you can set this in config/rule-notifier.php ->subject('Hello, world!') ->from([ 'name' => 'John Doe', 'email' => 'john@doe.com' ]) ->to([ 'name' => 'Jane Doe', 'email' => 'jane@doe.com' ]) ->content([ 'html' => '<h1>Notification sent via Rule!</h1>', 'html' => 'Notification sent via Rule!' ]); }
Slack:
public function toSlack() { return (new RuleCom\Notifier\Channels\Slack(new GuzzleHttp\Client())) ->endpoint('YOUR-SLACK-INCOMING-WEBHOOK') // If using Laravel you can set this in config/rule-notifier.php ->channel('#notification') // Here you can override the channel specified in Slack, or send DM by passing @username ->message('Hello, world!'); }
Usage with Laravel
This package can be easily integrated with laravel, with the following benefits.
- No need to pass in channel dependecies on your own.
- Ability to specify configurations such as, api key for Rule and webhook for Slack.
- In your
config/app.php
add the following service provider
RuleCom\Notifier\LaravelServiceProvider::class
- Publish the config:
php artisan vendor:publish
// Without Laravel you will have to pass the channel dependency on your own: (new RuleCom\Notifier\Channels\Slack(new GuzzleHttp\Client())) // With Laravel you can resolve the channels with dependencies through the ioc container: app(RuleCom\Notifier\Channels\Slack::class)
Debugging
If you need to debug a channel you may set it to debug mode. When a channel is in debug mode it will log the notification instead of dispatching the it to given channel.
To enable debug:
- Inject
Monolog\Logger
into the channel. - Call the
debug
method and pass in a path to your logfile.
return (new RuleCom\Notifier\Channels\Slack(new GuzzleHttp\Client(), new Monolog\Logger('Notification logger'))) ->debug('path/to/file.log') // If using Laravel you can set both debug mode and log path in config/rule-notifier.php ->endpoint('YOUR-SLACK-INCOMING-WEBHOOK') // If using Laravel you can set this in config/rule-notifier.php ->channel('#notification') // Here you can override the channel specified in Slack, or send DM by passing @username ->message('Hello, world!');
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CONDUCT for details.
Security
If you discover any security related issues, please email matthis.stenius@rule.se instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.