-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kyle Milloy
committed
Aug 30, 2019
0 parents
commit c05d0e8
Showing
6 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/vendor | ||
/.idea | ||
composer.phar | ||
composer.lock | ||
.DS_Store | ||
Thumbs.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# AWS SNS SMS Notifications Channel for Laravel | ||
|
||
## Installation | ||
|
||
`composer require itsnubix/aws-sns-sms-channel` | ||
|
||
In your `config/services.php` file enter: | ||
|
||
```php | ||
'sns' => [ | ||
'key' => env('AWS_ACCESS_KEY_ID'), | ||
'secret' => env('AWS_SECRET_ACCESS_KEY'), | ||
'region' => env('SNS_DEFAULT_REGION'), | ||
], | ||
``` | ||
|
||
Be sure that the user who owns your access key and secret has at least the following policy on AWS IAM: | ||
|
||
```json | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Sid": "AllowSendingSMSMessages", | ||
"Effect": "Allow", | ||
"Action": [ | ||
"sns:Publish", | ||
"sns:SetSMSAttributes", | ||
"sns:CheckIfPhoneNumberIsOptedOut" | ||
], | ||
"Resource": ["*"] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
Now in your notifications you can do the following: | ||
|
||
```php | ||
<?php | ||
namespace App\Notifications; | ||
|
||
use App\Models\Order; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Notifications\Notification; | ||
use Nubix\Notifications\Messages\SmsMessage; | ||
|
||
class SendHelloText extends Notification | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* Get the notification's delivery channels. | ||
* | ||
* @param mixed $notifiable | ||
* | ||
* @return array | ||
*/ | ||
public function via($notifiable) | ||
{ | ||
return ['sms']; | ||
} | ||
/** | ||
* Get the SMS representation of the notification. | ||
* | ||
* @param mixed $notifiable | ||
* | ||
* @return \App\Channels\Messages\SmsMessage | ||
*/ | ||
public function toSms($notifiable) | ||
{ | ||
return (new SmsMessage()) | ||
->content('Hello world'); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "itsnubix/aws-sns-sms-channel", | ||
"description": "AWS SNS SMS Notifications Channel for Laravel", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Kyle Milloy", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"require": { | ||
"php": "^7.1.3", | ||
"illuminate/notifications": "~5.8.0|^6.0|^7.0", | ||
"aws/aws-sdk-php": "^3.110" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Nubix\\Notifications\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Nubix\\Notifications\\SmsChannelServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Nubix\Notifications\Channels; | ||
|
||
use Aws\Sns\SnsClient; | ||
use Illuminate\Notifications\Notification; | ||
use Nubix\Notifications\Messages\SmsMessage; | ||
|
||
class SmsChannel | ||
{ | ||
/** | ||
* The SNS client instance. | ||
* | ||
* @var \Aws\Sns\SnsClient | ||
*/ | ||
protected $sns; | ||
|
||
public function __construct(SnsClient $sns) | ||
{ | ||
$this->sns = $sns; | ||
} | ||
|
||
/** | ||
* Send the given notification. | ||
* | ||
* @param mixed $notifiable | ||
* @param \Illuminate\Notifications\Notification $notification | ||
* | ||
* @return \Aws\Result | ||
*/ | ||
public function send($notifiable, Notification $notification) | ||
{ | ||
if (!$to = $notifiable->routeNotificationFor('sms', $notification)) { | ||
return; | ||
} | ||
|
||
if (!$result = $this->sns->checkIfPhoneNumberIsOptedOut(['phoneNumber' => $to])) { | ||
return; | ||
} | ||
|
||
if ($result['isOptedOut']) { | ||
return; | ||
} | ||
|
||
$message = $notification->toSms($notifiable); | ||
|
||
if (is_string($message)) { | ||
$message = new SmsMessage($message); | ||
} | ||
|
||
return $this->sns->publish([ | ||
'Message' => $message->content, | ||
'PhoneNumber' => $to, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Nubix\Notifications\Messages; | ||
|
||
class SmsMessage | ||
{ | ||
/** | ||
* The message content. | ||
* | ||
* @var string | ||
*/ | ||
public $content; | ||
|
||
/** | ||
* Create a new message instance. | ||
* | ||
* @param string|null $content | ||
*/ | ||
public function __construct($content = '') | ||
{ | ||
$this->content = $content; | ||
} | ||
|
||
/** | ||
* Set the message content. | ||
* | ||
* @param string $content | ||
* | ||
* @return $this | ||
*/ | ||
public function content(string $content) | ||
{ | ||
$this->content = trim($content); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Nubix\Notifications; | ||
|
||
use Aws\Sns\SnsClient; | ||
use Aws\Credentials\Credentials; | ||
use Illuminate\Support\ServiceProvider; | ||
use Illuminate\Notifications\ChannelManager; | ||
use Illuminate\Support\Facades\Notification; | ||
use Nubix\Notifications\Channels\SmsChannel; | ||
|
||
class SmsChannelServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register services. | ||
*/ | ||
public function register() | ||
{ | ||
Notification::resolved(function (ChannelManager $service) { | ||
$service->extend('sms', function ($app) { | ||
return new SmsChannel( | ||
new SnsClient([ | ||
'version' => '2010-03-31', | ||
'credentials' => new Credentials( | ||
$this->app['config']['services.sns.key'], | ||
$this->app['config']['services.sns.secret'] | ||
), | ||
'region' => $this->app['config']['services.sns.region'], | ||
]), | ||
$this->app['config']['services.sns.topic'] | ||
); | ||
}); | ||
}); | ||
} | ||
} |