### Install MsgOwl Laravel Notification Channel Source: https://github.com/msgowl/msgowl-laravel-notification-channel/blob/master/README.md Install the package using Composer. ```bash composer require msgowl/msgowl-laravel-notification-channel ``` -------------------------------- ### Handling Invalid MsgOwl Configuration with InvalidConfiguration Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt The `InvalidConfiguration` exception is thrown if the `msgowl` key is missing from `config/services.php`. This check can also be performed defensively in custom code. ```php use MsgOwl\MsgowlLaravelNotificationChannel\Exceptions\InvalidConfiguration; // Thrown automatically by MsgOwlServiceProvider::boot() if config is absent: // InvalidConfiguration::configurationNotSet(); // => "In order to send notification via MsgOwl you need to add credentials // in the `msgowl` key of `config.services`." // Defensive check in custom code: $config = config('services.msgowl'); if (is_null($config)) { throw InvalidConfiguration::configurationNotSet(); } ``` -------------------------------- ### Set MsgOwl Environment Variables Source: https://github.com/msgowl/msgowl-laravel-notification-channel/blob/master/README.md Define MsgOwl API Key, SenderID, and recipients in your .env file. ```dotenv // .env ... MSGOWL_SENDER_ID= MSGOWL_API_KEY= MSGOWL_RECIPIENTS= ], ... ``` -------------------------------- ### Configure MsgOwl Credentials Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt Register MsgOwl credentials in `config/services.php` and `.env`. The `sender_id`, `api_key`, and `recipients` keys are used as defaults when not explicitly set on a message. ```php // config/services.php 'msgowl' => [ 'sender_id' => env('MSGOWL_SENDER_ID'), // e.g., 'MyApp' 'api_key' => env('MSGOWL_API_KEY'), // e.g., 'abc123xyz...' 'recipients' => env('MSGOWL_RECIPIENTS'), // e.g., '9607777777' ], ``` ```dotenv # .env MSGOWL_SENDER_ID=MyApp MSGOWL_API_KEY=your-api-key-here MSGOWL_RECIPIENTS=9607777777 ``` -------------------------------- ### Configure MsgOwl Credentials in services.php Source: https://github.com/msgowl/msgowl-laravel-notification-channel/blob/master/README.md Add MsgOwl credentials to your Laravel application's services configuration file. ```php // config/services.php ... 'msgowl' => [ 'sender_id' => env('MSGOWL_SENDER_ID'), 'api_key' => env('MSGOWL_API_KEY'), 'recipients' => env('MSGOWL_RECIPIENTS'), ], ... ``` -------------------------------- ### Exception Handling — InvalidConfiguration Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt `InvalidConfiguration` is thrown during service container resolution if the `msgowl` key is missing from `config/services.php`. ```APIDOC ## Exception Handling — InvalidConfiguration ### Description `InvalidConfiguration` is thrown during service container resolution if the `msgowl` key is missing from `config/services.php`. ### Example Usage ```php use MsgOwl\MsgowlLaravelNotificationChannel\Exceptions\InvalidConfiguration; // Thrown automatically by MsgOwlServiceProvider::boot() if config is absent: // InvalidConfiguration::configurationNotSet(); // => "In order to send notification via MsgOwl you need to add credentials // in the `msgowl` key of `config.services`." // Defensive check in custom code: $config = config('services.msgowl'); if (is_null($config)) { throw InvalidConfiguration::configurationNotSet(); } ``` ``` -------------------------------- ### MsgOwlMessage: Build SMS Payloads Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt Use `MsgOwlMessage` to construct SMS payloads fluently. It supports setting the body, sender ID, and recipients. The message can be instantiated via a constructor or a static factory method. Whitespace in the body is trimmed. ```php use MsgOwl\MsgowlLaravelNotificationChannel\MsgOwlMessage; // Construct with body $message = new MsgOwlMessage('Your order #1234 has shipped.'); // Or use the static factory $message = MsgOwlMessage::create('Your order #1234 has shipped.'); // Set a custom sender ID (overrides config default) $message->setSenderId('MyShop'); // Set a single recipient $message->setRecipients('9607777777'); // Set multiple recipients (array is joined as comma-separated string internally) $message->setRecipients(['9607777777', '9608888888', '9609999999']); // Fluent chaining $message = MsgOwlMessage::create('Flash sale ends in 1 hour!') ->setSenderId('MyShop') ->setRecipients(['9607777777', '9608888888']); // Inspect the JSON payload that will be sent to the API echo $message->toJson(); // {"body":"Flash sale ends in 1 hour!","recipients":"9607777777,9608888888","sender_id":"MyShop"} ``` ```php use MsgOwl\MsgowlLaravelNotificationChannel\MsgOwlMessage; $message = new MsgOwlMessage(); $message->setBody(' Your verification code is 482910. '); // whitespace is trimmed // Equivalent chained usage $message = MsgOwlMessage::create() ->setBody('Your verification code is 482910.') ->setSenderId('AuthService') ->setRecipients('9607777777'); echo $message->toJson(); // {"body":"Your verification code is 482910.","recipients":"9607777777","sender_id":"AuthService"} ``` -------------------------------- ### MsgOwlChannel: Send Notifications via Laravel Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt Implement `toMsgOwl($notifiable)` in your notification class to return a `MsgOwlMessage` instance or a plain string. Recipients are resolved from `routeNotificationFor('MsgOwl')` on the notifiable model, then from the message, and finally from the config default. ```php use Illuminate\Notifications\Notification; use MsgOwl\MsgowlLaravelNotificationChannel\MsgOwlChannel; use MsgOwl\MsgowlLaravelNotificationChannel\MsgOwlMessage; class OrderShipped extends Notification { public function __construct(private string $orderNumber) {} // Register the MsgOwl channel public function via($notifiable): array { return [MsgOwlChannel::class]; } // Return a MsgOwlMessage instance public function toMsgOwl($notifiable): MsgOwlMessage { return MsgOwlMessage::create("Your order #{$this->orderNumber} has shipped.") ->setSenderId('MyShop'); // Recipients resolved from $notifiable->routeNotificationForMsgOwl() } } // Dispatch the notification $user->notify(new OrderShipped('9876')); ``` -------------------------------- ### Use MsgOwlChannel in Laravel Notification Source: https://github.com/msgowl/msgowl-laravel-notification-channel/blob/master/README.md Implement the MsgOwlChannel in your notification's via() method and define the toMsgOwl() method to send messages. ```php use MsgOwl\MsgowlLaravelNotificationChannel\MsgOwlChannel; use MsgOwl\MsgowlLaravelNotificationChannel\MsgOwlMessage; use Illuminate\Notifications\Notification; class UserApproved extends Notification { public function via($notifiable) { return [MsgOwlChannel::class]; } public function toMsgOwl($notifiable) { return (new MsgOwlMessage("You are approved by the system")); } } ``` -------------------------------- ### Sending Plain String Notifications Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt If `toMsgOwl()` returns a plain string, the channel automatically wraps it into a `MsgOwlMessage`. Default recipients and sender ID are used from configuration. ```php use Illuminate\Notifications\Notification; use MsgOwl\MsgowlLaravelNotificationChannel\MsgOwlChannel; class SimpleAlert extends Notification { public function via($notifiable): array { return [MsgOwlChannel::class]; } // Returning a plain string — channel wraps it in MsgOwlMessage::create() public function toMsgOwl($notifiable): string { return 'Alert: Your account password was changed.'; } } $user->notify(new SimpleAlert()); // Uses MSGOWL_SENDER_ID and MSGOWL_RECIPIENTS from .env as defaults ``` -------------------------------- ### MsgOwlMessage::setRecipients() Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt `setRecipients()` accepts a string, integer, or array of phone numbers. Arrays are implicitly joined into a comma-separated string before the API call. ```APIDOC ## MsgOwlMessage::setRecipients() ### Description `setRecipients()` accepts a string, integer, or array of phone numbers. Arrays are implicitly joined into a comma-separated string before the API call. ### Method Signature `setRecipients(string|int|array $recipients): self` ### Example Usage ```php use MsgOwl\MsgowlLaravelNotificationChannel\MsgOwlMessage; // Single recipient as string $msg = MsgOwlMessage::create('Staff meeting at 3pm.') ->setSenderId('HRDept') ->setRecipients('9601111111'); // Single recipient as integer $msg = MsgOwlMessage::create('Staff meeting at 3pm.') ->setRecipients(9601111111); // Multiple recipients as array $msg = MsgOwlMessage::create('System maintenance tonight at 11pm.') ->setSenderId('OpsTeam') ->setRecipients(['9601111111', '9602222222', '9603333333']); echo $msg->toJson(); // {"body":"System maintenance tonight at 11pm.","recipients":"9601111111,9602222222,9603333333","sender_id":"OpsTeam"} ``` ``` -------------------------------- ### Sending a Plain String Notification Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt If `toMsgOwl()` returns a plain string instead of a `MsgOwlMessage`, the channel automatically wraps it into a `MsgOwlMessage`. Recipients and sender ID fall back to config defaults. ```APIDOC ## Sending a Plain String Notification ### Description If `toMsgOwl()` returns a plain string instead of a `MsgOwlMessage`, the channel automatically wraps it into a `MsgOwlMessage`. Recipients and sender ID fall back to config defaults. ### Method Signature `toMsgOwl($notifiable): string` ### Example Usage ```php use Illuminate\Notifications\Notification; use MsgOwl\MsgowlLaravelNotificationChannel\MsgOwlChannel; class SimpleAlert extends Notification { public function via($notifiable): array { return [MsgOwlChannel::class]; } // Returning a plain string — channel wraps it in MsgOwlMessage::create() public function toMsgOwl($notifiable): string { return 'Alert: Your account password was changed.'; } } $user->notify(new SimpleAlert()); // Uses MSGOWL_SENDER_ID and MSGOWL_RECIPIENTS from .env as defaults ``` ``` -------------------------------- ### Define routeNotificationForMsgOwl Method Source: https://github.com/msgowl/msgowl-laravel-notification-channel/blob/master/README.md Implement routeNotificationForMsgOwl in your Notifiable model to specify recipient phone numbers. ```php public function routeNotificationForMsgOwl() : string { return $this->mobile; } ``` -------------------------------- ### Dynamic Recipient Routing with routeNotificationForMsgOwl() Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt Implement `routeNotificationForMsgOwl()` in your notifiable model to dynamically supply the recipient's phone number. This method overrides any recipients set on the message itself. ```php use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; // Return the user's phone number for MsgOwl SMS routing public function routeNotificationForMsgOwl(): string { return $this->mobile; // e.g., '9607777777' } } // Usage: the channel automatically calls routeNotificationForMsgOwl() $user = User::find(1); // $user->mobile = '9607777777' $user->notify(new OrderShipped('9876')); // SMS sent to '9607777777' ``` -------------------------------- ### Handling MsgOwl API Errors with CouldNotSendNotification Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt Catch `CouldNotSendNotification` exceptions thrown by `MsgOwlClient` for API errors. The channel can also fire Laravel's `NotificationFailed` event. ```php use Illuminate\Support\Facades\Event; use Illuminate\Notifications\Events\NotificationFailed; use MsgOwl\MsgowlLaravelNotificationChannel\Exceptions\CouldNotSendNotification; // Listen for failed MsgOwl notifications Event::listen(NotificationFailed::class, function (NotificationFailed $event) { if ($event->channel === 'msgowl-sms') { // $event->data contains the error message string \Log::error('MsgOwl SMS failed', [ 'notifiable' => $event->notifiable->id, 'error' => $event->data, ]); } }); // CouldNotSendNotification is generated like this internally: // CouldNotSendNotification::serviceRespondedWithAnError($exception); // => "MsgOwl service responded with an error '500: Internal Server Error'" ``` -------------------------------- ### Exception Handling — CouldNotSendNotification Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt `CouldNotSendNotification` is thrown by `MsgOwlClient` when the MsgOwl API returns an error. When an event `Dispatcher` is available on the channel, it also fires Laravel's `NotificationFailed` event instead of letting the exception propagate. ```APIDOC ## Exception Handling — CouldNotSendNotification ### Description `CouldNotSendNotification` is thrown by `MsgOwlClient` when the MsgOwl API returns an error. When an event `Dispatcher` is available on the channel, it also fires Laravel's `NotificationFailed` event instead of letting the exception propagate. ### Example Usage ```php use Illuminate\Support\Facades\Event; use Illuminate\Notifications\Events\NotificationFailed; use MsgOwl\MsgowlLaravelNotificationChannel\Exceptions\CouldNotSendNotification; // Listen for failed MsgOwl notifications Event::listen(NotificationFailed::class, function (NotificationFailed $event) { if ($event->channel === 'msgowl-sms') { // $event->data contains the error message string \Log::error('MsgOwl SMS failed', [ 'notifiable' => $event->notifiable->id, 'error' => $event->data, ]); } }); // CouldNotSendNotification is generated like this internally: // CouldNotSendNotification::serviceRespondedWithAnError($exception); // => "MsgOwl service responded with an error '500: Internal Server Error'" ``` ``` -------------------------------- ### Sending Notifications to Multiple Recipients with setRecipients() Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt Use `setRecipients()` to specify one or more phone numbers for the notification. It accepts a single string, integer, or an array of numbers. Arrays are automatically joined into a comma-separated string. ```php use MsgOwl\MsgowlLaravelNotificationChannel\MsgOwlMessage; // Single recipient as string $msg = MsgOwlMessage::create('Staff meeting at 3pm.') ->setSenderId('HRDept') ->setRecipients('9601111111'); // Single recipient as integer $msg = MsgOwlMessage::create('Staff meeting at 3pm.') ->setRecipients(9601111111); // Multiple recipients as array $msg = MsgOwlMessage::create('System maintenance tonight at 11pm.') ->setSenderId('OpsTeam') ->setRecipients(['9601111111', '9602222222', '9603333333']); echo $msg->toJson(); // {"body":"System maintenance tonight at 11pm.","recipients":"9601111111,9602222222,9603333333","sender_id":"OpsTeam"} ``` -------------------------------- ### Set MsgOwl Recipients in Message Source: https://github.com/msgowl/msgowl-laravel-notification-channel/blob/master/README.md Optionally set specific recipients for a MsgOwl message. ```php return (new MsgOwlMessage("You are approved by the system"))->setRecipients($recipients); ``` -------------------------------- ### routeNotificationForMsgOwl() Source: https://context7.com/msgowl/msgowl-laravel-notification-channel/llms.txt Add `routeNotificationForMsgOwl()` to your notifiable model to dynamically supply the recipient phone number. If this method returns a value, it overrides any recipients set on the message. ```APIDOC ## routeNotificationForMsgOwl() ### Description Add `routeNotificationForMsgOwl()` to your notifiable model (e.g., `User`) to dynamically supply the recipient phone number. If this method returns a value, it overrides any recipients set on the message. ### Method Signature `routeNotificationForMsgOwl(): string` ### Example Usage ```php use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; // Return the user's phone number for MsgOwl SMS routing public function routeNotificationForMsgOwl(): string { return $this->mobile; // e.g., '9607777777' } } // Usage: the channel automatically calls routeNotificationForMsgOwl() $user = User::find(1); // $user->mobile = '9607777777' $user->notify(new OrderShipped('9876')); // SMS sent to '9607777777' ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.