andriichuk / laravel-atompark-sms-channel
This is my package laravel-atompark-sms-channel
Package info
github.com/andriichuk/laravel-atompark-sms-channel
pkg:composer/andriichuk/laravel-atompark-sms-channel
Fund package maintenance!
Requires
- php: ^8.3
- guzzlehttp/guzzle: ^7.9
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
README
This package makes it easy to send SMS notifications using AtomPark from your Laravel application, using Laravel's built-in notification system.
Sending an SMS to a user becomes as simple as using:
$user->notify(new YourNotification());
Contents
- Installation
- Setting up the AtomPark service
- Usage
- Sending text messages
- Available message methods
- Sending text messages
- Testing
- Changelog
- Contributing
- Security
- License
Installation
You can install the package via composer:
composer require andriichuk/laravel-atompark-sms-channel
The service provider will be auto-discovered by Laravel.
Setting up the AtomPark service
Add your AtomPark SMS credentials to the services.php config file:
// config/services.php return [ // ... 'atompark' => [ 'sms' => [ 'sender' => env('ATOMPARK_SMS_SENDER'), 'public_key' => env('ATOMPARK_SMS_PUBLIC_KEY'), 'private_key' => env('ATOMPARK_SMS_PRIVATE_KEY'), ], ], ];
Then add the corresponding environment variables to your .env:
ATOMPARK_SMS_SENDER="Your Sender Name" ATOMPARK_SMS_PUBLIC_KEY="your-public-key" ATOMPARK_SMS_PRIVATE_KEY="your-private-key"
Usage
Notifiable model
In your notifiable model (typically User), add the routeNotificationForAtomPark method that returns a full mobile number including country code:
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; public function routeNotificationForAtomPark(): string { return $this->phone; // e.g. +380991112233 } }
Notification class
Within your notification, add the AtomPark channel to the via method and implement toAtomPark to build the SMS message:
use Andriichuk\AtomParkSmsChannel\AtomParkChannel; use Andriichuk\AtomParkSmsChannel\Sms; use Illuminate\Notifications\Notification; class Invitation extends Notification { public function via($notifiable): array { return ['atompark']; // or: return [AtomParkChannel::class]; } public function toAtomPark($notifiable): Sms { return new Sms( text: 'You have been invited!', phone: $notifiable->routeNotificationForAtomPark(), lifetime: 1, // 0 = maximum, 1/6/12/24 hours ); } }
Now you can send an SMS notification to a user:
$user->notify(new Invitation());
Anonymous notifications
You can also send SMS messages to phone numbers that are not associated with a notifiable model:
use Illuminate\Support\Facades\Notification; Notification::route('atompark', '+380991112233') ->notify(new Invitation());
Your toAtomPark method will receive an AnonymousNotifiable instance, and you can resolve the phone number using:
public function toAtomPark($notifiable): Sms { $phone = method_exists($notifiable, 'routeNotificationFor') ? $notifiable->routeNotificationFor('atompark') : (string) $notifiable; return new Sms( text: 'You have been invited!', phone: $phone, ); }
Available message options
The Sms value object supports:
text(string) – the message body.phone(string) – recipient phone number including country code.lifetime(int) – message lifetime in hours (0= maximum,1,6,12,24).
Testing
Run the test suite with:
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.